I would like to ask a question about the scaling of objects to the size of the screen.
But the question is not a problem that can be solved by setting the "canvas scaler" to "scale with screen size."
It's about how to program the code.
As shown in the picture, the function I want to achieve is to make the hexagonal graphics change position with the player's operating angle.
And my code has successfully achieved this effect! However, I ran into a problem because the value of "rotationRadius" did not change and remained fixed at the previously set 320 f.
So when the screen gets smaller, the hexagonal pointer will move away from the ruler.
When the screen is enlarged, the hexagonal pointer will be in the arc ruler.
I want the "Hexagon Indicator" to move closely to the arc ruler. This hexagon must not deviate from the arc ruler under different screen sizes!
But I don't know how to do that.
Perhaps I can multiply the 320 f I set by some of the scale parameters.
But that's the problem. What is the calculation formula of this parameter?
Of course, in addition to multiplying the parameters, there should be other more appropriate methods, such as using anchor , but I really don't know how to do?
Could you please assist me in rewriting the following program code?
I drawing the green yellow and red meter graphic by the code below. And th e picture show the process what i do in unity.
And the "angle" image is which I draw in AI(illustrator) then I put into unity Asset.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Exercise_Angle : MonoBehaviour
{
[SerializeField]
Transform rotationCenter;
[SerializeField]
public float rotationRadius = 320f;
[SerializeField]
private float posX, posY, angle = 0f;
[SerializeField]
private Image FillByHoldingAngle_Red;
[SerializeField]
private Image FillByHoldingAngle_Yellow;
[SerializeField]
float Max_Red_Angle, Max_Yellow_Angle, Max_Green_Angle,current_Angle;
public int Exercise_type;// flexion:1, abduction:2, extension:3, external rotation: 4, internal rotation: 5
public TMP_Text Angle;
public float targetWidth;
/*void Awake()
{
targetWidth = (float)Screen.width;
float factor = Screen.width / 800;
rotationRadius = 320f * factor;
}
*/
void Start()
{
if (Exercise_type == 1) //flexion:1
{
Max_Green_Angle = 180;
Max_Yellow_Angle = Daterecord.flexion_SettingAngle;
Max_Red_Angle = Daterecord.flexion_SettingAngle - 15;
}
if (Exercise_type == 2) //abduction:2
{
Max_Green_Angle = 180;
Max_Yellow_Angle = Daterecord.abduction_SettingAngle;
Max_Red_Angle = Daterecord.abduction_SettingAngle - 15;
}
if (Exercise_type == 3) //extension:3
{
Max_Green_Angle = 60;
Max_Yellow_Angle = Daterecord.extension_SettingAngle;
Max_Red_Angle = Daterecord.extension_SettingAngle - 15;
}
if (Exercise_type == 4) //external rotation: 4
{
Max_Green_Angle = 90;
Max_Yellow_Angle = Daterecord.external_rotation_SettingAngle;
Max_Red_Angle = Daterecord.external_rotation_SettingAngle - 15;
}
if (Exercise_type == 5) //internal rotation: 5
{
Max_Green_Angle = 90;
Max_Yellow_Angle = Daterecord.internal_rotation_SettingAngle;
Max_Red_Angle = Daterecord.internal_rotation_SettingAngle - 15;
}
else //internal rotation: 5
{
Max_Green_Angle = 180;
Max_Yellow_Angle = 100;
Max_Red_Angle = Max_Yellow_Angle - 20;
}
}
// Update is called once per frame
void Update()
{
FillByHoldingAngle_Red.fillAmount = Max_Red_Angle / Max_Green_Angle;
FillByHoldingAngle_Yellow.fillAmount = Max_Yellow_Angle / Max_Green_Angle;
Angle.text = Max_Yellow_Angle+ "°";
//////////////////////////////////////////////////////////////////////////////////////////////
posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius/2 ;
transform.position = new Vector3(posX, posY, 0);
//angle = angle + 0;
//angle = angle + Time.deltaTime;
angle = 3.14f+((current_Angle/ Max_Green_Angle) * 3.14f);
if (angle >= 6.28f)
angle = 3.14f;
}
}
`




