Unity - 在检查器中自定义结构图
Unity - Custom drawing of a struct in the inspector
我有一个自定义结构,代码如下:
[Serializable]
public struct HexPoint : IEquatable<HexPoint>
{
public readonly int x;
public readonly int y;
public readonly int z;
// Some custom methods for initializations and operators
}
如果我将 x、y 和 z 变量设置为非只读,它们将在 unity inspector 中正常显示。但是,我有一些规则需要他们满足(实际上是x+y+z=0),所以我添加了readonly以防止人们乱用它。
但作为只读变量,它们不会显示(因为它们无法修改)! :(
我想知道它们是否是我在统一检查器中显示它们的一种方式,类似于 PropertyDrawer。我知道我可以将结构切换为 class,因为 PropertyDrawer 是为 classes 保留的,但我想将其保留为结构。
那么,有没有办法显示这些值?并最终使用自定义初始化程序修改它们?
非常感谢!
readonly
使它们也 non-serialized
-> 不显示在检查器中
注意 PropertyDrawer
不 仅限于 class
类型但也可用于 struct
类型。
其实不需要CustomPropertyDrawer
。
您可以将 public 设为只读 properties to access private fields and to display them in the Inspector use [SerializeField]
,这使得它们只能通过 Inspector 进行编辑,而不能通过其他 类.
[Serializable]
public struct HexPoint : IEquatable<HexPoint>
{
// Those are not displayed in the inspector,
// readonly and accessible by other classes
public int x { get { return _x; } }
public int y { get { return _y; } }
public int z { get { return _z; } }
// if you prefer you can also use the expression body style instead
//public int x => _x;
//public int y => _y;
//public int z => _z;
// Those are displayed and editable in the Inspector
// but private and therefor not changeable by other classes
[SerializeField] private int _x;
[SerializeField] private int _y;
[SerializeField] private int _z;
public bool Equals(HexPoint other)
{
return _x == other._x && _y == other._y && _z == other._z;
}
public override bool Equals(object obj)
{
return obj is HexPoint other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = _x;
hashCode = (hashCode * 397) ^ _y;
hashCode = (hashCode * 397) ^ _z;
return hashCode;
}
}
}
如果您真的想使用 PropertyDrawer
来另外禁止在检查器中编辑这些值,但仍然保存并查看它们,您可以添加一个,例如
[Serializable]
public struct HexPoint : IEquatable<HexPoint>
{
// Those are not displayed in the inspector,
// readonly and accessible by other classes
public int x { get { return _x; } }
public int y { get { return _y; } }
public int z { get { return _z; } }
// if you prefer you can also use the expression body style instead
//public int x => _x;
//public int y => _y;
//public int z => _z;
// Those are displayed and editable in the Inspector
// but private and therefor not changeable by other classes
[SerializeField] private int _x;
[SerializeField] private int _y;
[SerializeField] private int _z;
public bool Equals(HexPoint other)
{
return _x == other._x && _y == other._y && _z == other._z;
}
public override bool Equals(object obj)
{
return obj is HexPoint other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = _x;
hashCode = (hashCode * 397) ^ _y;
hashCode = (hashCode * 397) ^ _z;
return hashCode;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(HexPoint))]
public class HexPointDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1 : 2);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Find the SerializedProperties by name
var x = property.FindPropertyRelative(nameof(_x));
var y = property.FindPropertyRelative(nameof(_y));
var z = property.FindPropertyRelative(nameof(_z));
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
{
// Makes the fields disabled / grayed out
EditorGUI.BeginDisabledGroup(true);
{
// In your case the best option would be a Vector3Field which handles the correct drawing
EditorGUI.Vector3IntField(position, label, new Vector3Int(x.intValue, y.intValue, z.intValue));
}
EditorGUI.EndDisabledGroup();
}
EditorGUI.EndProperty();
}
}
#endif
}
提示 用于在更改后检查值 MonoBehaviour.OnValidate 可能会让您感兴趣
我有一个自定义结构,代码如下:
[Serializable]
public struct HexPoint : IEquatable<HexPoint>
{
public readonly int x;
public readonly int y;
public readonly int z;
// Some custom methods for initializations and operators
}
如果我将 x、y 和 z 变量设置为非只读,它们将在 unity inspector 中正常显示。但是,我有一些规则需要他们满足(实际上是x+y+z=0),所以我添加了readonly以防止人们乱用它。
但作为只读变量,它们不会显示(因为它们无法修改)! :(
我想知道它们是否是我在统一检查器中显示它们的一种方式,类似于 PropertyDrawer。我知道我可以将结构切换为 class,因为 PropertyDrawer 是为 classes 保留的,但我想将其保留为结构。
那么,有没有办法显示这些值?并最终使用自定义初始化程序修改它们?
非常感谢!
readonly
使它们也 non-serialized
-> 不显示在检查器中
注意 PropertyDrawer
不 仅限于 class
类型但也可用于 struct
类型。
其实不需要CustomPropertyDrawer
。
您可以将 public 设为只读 properties to access private fields and to display them in the Inspector use [SerializeField]
,这使得它们只能通过 Inspector 进行编辑,而不能通过其他 类.
[Serializable]
public struct HexPoint : IEquatable<HexPoint>
{
// Those are not displayed in the inspector,
// readonly and accessible by other classes
public int x { get { return _x; } }
public int y { get { return _y; } }
public int z { get { return _z; } }
// if you prefer you can also use the expression body style instead
//public int x => _x;
//public int y => _y;
//public int z => _z;
// Those are displayed and editable in the Inspector
// but private and therefor not changeable by other classes
[SerializeField] private int _x;
[SerializeField] private int _y;
[SerializeField] private int _z;
public bool Equals(HexPoint other)
{
return _x == other._x && _y == other._y && _z == other._z;
}
public override bool Equals(object obj)
{
return obj is HexPoint other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = _x;
hashCode = (hashCode * 397) ^ _y;
hashCode = (hashCode * 397) ^ _z;
return hashCode;
}
}
}
如果您真的想使用 PropertyDrawer
来另外禁止在检查器中编辑这些值,但仍然保存并查看它们,您可以添加一个,例如
[Serializable]
public struct HexPoint : IEquatable<HexPoint>
{
// Those are not displayed in the inspector,
// readonly and accessible by other classes
public int x { get { return _x; } }
public int y { get { return _y; } }
public int z { get { return _z; } }
// if you prefer you can also use the expression body style instead
//public int x => _x;
//public int y => _y;
//public int z => _z;
// Those are displayed and editable in the Inspector
// but private and therefor not changeable by other classes
[SerializeField] private int _x;
[SerializeField] private int _y;
[SerializeField] private int _z;
public bool Equals(HexPoint other)
{
return _x == other._x && _y == other._y && _z == other._z;
}
public override bool Equals(object obj)
{
return obj is HexPoint other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = _x;
hashCode = (hashCode * 397) ^ _y;
hashCode = (hashCode * 397) ^ _z;
return hashCode;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(HexPoint))]
public class HexPointDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1 : 2);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Find the SerializedProperties by name
var x = property.FindPropertyRelative(nameof(_x));
var y = property.FindPropertyRelative(nameof(_y));
var z = property.FindPropertyRelative(nameof(_z));
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
{
// Makes the fields disabled / grayed out
EditorGUI.BeginDisabledGroup(true);
{
// In your case the best option would be a Vector3Field which handles the correct drawing
EditorGUI.Vector3IntField(position, label, new Vector3Int(x.intValue, y.intValue, z.intValue));
}
EditorGUI.EndDisabledGroup();
}
EditorGUI.EndProperty();
}
}
#endif
}
提示 用于在更改后检查值 MonoBehaviour.OnValidate 可能会让您感兴趣