I’m a bit new to Unity and C# scripting, but I’m working on an AR project for a client so they can showcase their products. They need to be able to see when the AR object (their product) is scaled to the real world size (100%) in the scene (through the camera). I’m trying to display the object’s transform.localScale as a percentage (via TextMesh, TMPro, etc) above the object like this (screenshot using the iPhone Quick Look AR feature):
I’m kind of just stuck at being able to get the transform.localScale to display as a string (like (1,1,1)), but not converting that to a percentage like in the screenshot. Any help with this would be appreciated.
first you need to clearly define what 100% means. that’s a unitless scalar value, which is always given in proportion to something tangible. once you know what 100% means in real world units, then you simply multiply your object’s scale with the percentage, or vice versa, you take your 100% scale and divide it by your object’s scale to get the percentage.
this is the general idea, if you have two things, you can easily obtain the third.

I can’t tell what exactly is bothering you, so if you have two scales that are uniform (that means one of them is a scalar multiple of the other), you just do
float percent = scaleActual.x / scaleReferent.x; // or y, or z
likewise, to get to the actual scale with the aid of percent
Vector3 scaleActual = percent * scaleReferent;
to get a humanized string out of percent, you do
string label = $"{(percent * 100f).ToString("F2")} %";
However, you still need to define what scaleReferent equals to, upfront. Is it (1,1,1) or (2,2,2), nobody knows but you.
1 Like
Hey, thanks for the reply. I’m sorry, I should have clarified in the original post. When the object is scaled as (1,1,1) it is 100% world scale (I’ve tested it out already), but I need an annotation to show that it is 100% so when the client is using it onsite, they know that the size is correct (but they need to be able to scale it to different sizes for displaying on table, for example). What I’d like is for the text to read 100% when the transform.localScale of the object is (1,1,1). I can display the “(1,1,1)” as a string using TextMesh, but I can’t figure out how to represent the (1,1,1) as a string that reads 100%.
(1,1,1) is not a useful information, that’s an absolute vector, not a scalar percentage.
you don’t need all three axes, you just need a scalar relationship between the two scales.
therefore, assuming that the change in scales is uniform
float percent = scaleActual.x / scaleReferent.x; // or y, or z
string label = $"{(percent * 100f).ToString("F2")} %";
Debug.Log(label);
the other way to write this would be
float percent = scaleActual.x / scaleReferent.x; // or y, or z
string label = string.Format("{0:F2} %", percent * 100f);
Debug.Log(label);
1 Like
In general, any kind of percentage is ALWAYS a ratio between two values.
Whenever you encounter a need to have a percentage value, it is always currentValue / someReferentValue.
In this way you will always get a number that is somewhere in between 0 and 1.
This is what percentage is, but we like to annotate them in units of hundredths (parts per hundred).
Permilles, for example, are annotated in parts per thousand.
1 Like
Thank you a million times! You’ve solved my problem and now I have my code functioning correctly. I do understand how percentages work, I just don’t have my “developer hat” completely on just yet (thinking about how to solve problems in code), but you explained it perfectly and now I’m on the right track. I was completely unaware of the implementation of percentages to string, but now I’ve got it.
Below is an example of how I implemented the code (not the project I’m working on but and example from a sandbox project):
public class ScaleAnnotation : MonoBehaviour
{
public TextMesh scaleText;
private Vector3 referentScale;
// Start is called before the first frame update
public void Start()
{
referentScale = new Vector3(1, 1, 1);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
ShowScale();
}
public void ShowScale()
{
Vector3 actualScale = gameObject.transform.localScale;
float percent = actualScale.x / referentScale.x;
string label = $"{(percent * 100f).ToString("F0")}%";
scaleText.text = label;
}
}