当前位置:   article > 正文

unity 编辑器之自定义脚本界面_unity createpropertygui

unity createpropertygui

自定义脚本界面有两种方法,一种是继承PropertyDrawer,一种是继承editor

下面介绍继承自PropertyDrawer

PropertyDrawer有两种用途,一种是自定义绘制带有Serializable属性的类,一种是自定义继承自PropertyAttribute的类

1.绘制带有Serializable属性的类在重新绘制该类的时候,绘制类要继承自PropertyDrawer,且加上CustomPropertyDrawer属性

  1. using System;
  2. using UnityEngine;
  3. public enum IngredientUnit { Spoon, Cup, Bowl, Piece }
  4. // 自定义绘制类,没有继承mono
  5. [Serializable]
  6. public class Ingredient
  7. {
  8. public string name;
  9. public int amount = 1;
  10. public IngredientUnit unit;
  11. }
  12. //继承mono,默认绘制
  13. public class Recipe : MonoBehaviour
  14. {
  15. public Ingredient potionResult;
  16. public Ingredient[] potionIngredients;
  17. }
  18. using UnityEditor;
  19. using UnityEditor.UIElements;
  20. using UnityEngine.UIElements;
  21. // IngredientDrawerUIE
  22. [CustomPropertyDrawer(typeof(Ingredient))]
  23. public class IngredientDrawerUIE : PropertyDrawer
  24. {
  25. public override VisualElement CreatePropertyGUI(SerializedProperty property)
  26. {
  27. // Create property container element.
  28. var container = new VisualElement();
  29. // Create property fields.
  30. var amountField = new PropertyField(property.FindPropertyRelative("amount"));
  31. var unitField = new PropertyField(property.FindPropertyRelative("unit"));
  32. var nameField = new PropertyField(property.FindPropertyRelative("name"), "Fancy Name");
  33. // Add fields to the container.
  34. container.Add(amountField);
  35. container.Add(unitField);
  36. container.Add(nameField);
  37. return container;
  38. }
  39. }

2.绘制继承自PropertyAttribute类的属性

比如系统自带的属性 Range

下面我们自己实现一个和Range属性一样的属性

  1. using UnityEngine;
  2. public class RangeAttribute : PropertyAttribute
  3. {
  4. public float min;
  5. public float max;
  6. public RangeAttribute(float min, float max)
  7. {
  8. this.min = min;
  9. this.max = max;
  10. }
  11. }
  12. [CustomPropertyDrawer(typeof(RangeAttribute))]
  13. public class RangeDrawer : PropertyDrawer
  14. {
  15. // Draw the property inside the given rect
  16. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  17. {
  18. // First get the attribute since it contains the range for the slider
  19. RangeAttribute range = attribute as RangeAttribute;
  20. // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
  21. if (property.propertyType == SerializedPropertyType.Float)
  22. EditorGUI.Slider(position, property, range.min, range.max, label);
  23. else if (property.propertyType == SerializedPropertyType.Integer)
  24. EditorGUI.IntSlider(position, property, Convert.ToInt32(range.min), Convert.ToInt32(range.max), label);
  25. else
  26. EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
  27. }
  28. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/585601
推荐阅读
相关标签
  

闽ICP备14008679号