当前位置:   article > 正文

Unity Editor 扩展PropertyDrawer (属性的 Inspector )

editorgui.beginproperty

  1. using UnityEngine;
  2. [System.Serializable]
  3. public struct RangeFloat {
  4. public float min;
  5. public float max;
  6. public RangeFloat (float minValue, float maxValue) {
  7. min = minValue;
  8. max = maxValue;
  9. }
  10. public float Lerp (float t) {
  11. return Mathf.Lerp(min, max, t);
  12. }
  13. }

以下代码放置在名为 Editor 的文件夹下

  1. #pragma warning disable 0649
  2. using UnityEditor;
  3. using UnityEngine;
  4. [CustomPropertyDrawer(typeof(RangeFloat))]
  5. public class RangeFloatDrawer : PropertyDrawer {
  6. public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
  7. // 只能在 position 指定的范围内绘制
  8. EditorGUI.BeginProperty(position, label, property);
  9. {
  10. float x, w;
  11. int elementCount = 3;
  12. for (int i = 0; i < elementCount; i++) {
  13. w = position.width / elementCount;
  14. x = position.x + w * i;
  15. switch (i) {
  16. case 0:
  17. EditorGUIUtility.labelWidth = 25;
  18. EditorGUI.LabelField(new Rect(x, position.y, w, position.height), property.displayName);
  19. break;
  20. case 1:
  21. EditorGUIUtility.labelWidth = 25;
  22. EditorGUI.PropertyField(new Rect(x, position.y, w, position.height), property.FindPropertyRelative("min"));
  23. break;
  24. case 2:
  25. EditorGUIUtility.labelWidth = 25;
  26. EditorGUI.PropertyField(new Rect(x, position.y, w, position.height), property.FindPropertyRelative("max"));
  27. break;
  28. }
  29. }
  30. }
  31. EditorGUI.EndProperty();
  32. EditorGUIUtility.labelWidth = 0; // 将其设置为 0 会重置为默认值
  33. }
  34. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/100979?site
推荐阅读
相关标签
  

闽ICP备14008679号