While in the Editor, it’s possible to adjust the scale of 3D Gizmos in the Gizmos dropdown menu. Does anyone know of a way to reference the value of that scale through code?
Well, the icon size seems to be defined in an internal class called “AnnotationUtility”. It has a static property called “iconSize” which is a float. The actual implementation is defined external.
Since the AnnotationUtility class is declared “internal” you can’t simply access it, but you can use reflection:
using UnityEditor;
using UnityEngine;
using System.Linq;
public static class AnnotationUtiltyWrapper
{
static System.Type m_AnnotationUtilityType;
static System.Reflection.PropertyInfo m_IconSize;
static AnnotationUtiltyWrapper()
{
m_AnnotationUtilityType = typeof(Editor).Assembly.GetTypes().Where(t=>t.Name == "AnnotationUtility").FirstOrDefault();
if (m_AnnotationUtilityType == null)
{
Debug.LogWarning("The internal type 'AnnotationUtility' could not be found. Maybe something changed inside Unity");
return;
}
m_IconSize = m_AnnotationUtilityType.GetProperty("iconSize", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
if (m_IconSize == null)
{
Debug.LogWarning("The internal class 'AnnotationUtility' doesn't have a property called 'iconSize'");
}
}
public static float IconSize
{
get { return (m_IconSize == null) ? 1f : (float)m_IconSize.GetValue(null, null); }
set { if (m_IconSize != null) m_IconSize.SetValue(null, value, null); }
}
public static float IconSizeLinear
{
get { return ConvertTexelWorldSizeTo01(IconSize); }
set { IconSize = Convert01ToTexelWorldSize(value); }
}
public static float Convert01ToTexelWorldSize(float value01)
{
if (value01 <= 0f)
{
return 0f;
}
return Mathf.Pow(10f, -3f + 3f * value01);
}
public static float ConvertTexelWorldSizeTo01(float texelWorldSize)
{
if (texelWorldSize == -1f)
{
return 1f;
}
if (texelWorldSize == 0f)
{
return 0f;
}
return (Mathf.Log10(texelWorldSize) - -3f) / 3f;
}
}
Note: the actual size isn’t a linear but a logarithmic scale. The GUI code uses “ConvertTexelWorldSizeTo01” before passing the value to the slider and the resulting value is passed through “Convert01ToTexelWorldSize” before assigned back to the iconSize variable.
I’ve simply created a second property (IconSizeLinear) which reads (and writes) the icon size linearly. Though the actual size can be read via IconSize. IconSize is also in the range 0 to 1 but not linear.