Unity编辑器之自定义Property Attribute

一、前言

unity 本身自带很多的attribute, 如,Rang, Min,AttributeUsage,Header,ToolTip 等。但是开发过程中往往需要程序根据工程需要或者个人喜好去做一些自定义的attribute。比如,想要对某些属性加颜色突出显示,又或者突然间嫌弃Rang的滑条模式等。

二、Property Attribute书写格式

unity自定义attribute继承于UnityEngine.PropertyAttribute类,如下图:

  1. AttributeUsage: C#脚本自带的attribute, .net官网AttributeUsage介绍
  2. MRang: 自定义的attribute,它继承了PropertyAttribute类;
  3. public readonly int min: 变量 min, 自定义attribute下边界;
  4. public readonly int max: 变量 max, 自定义attribute上边界;

三、自定义Property Attribute的使用


如上图,我们期望把m_walkanble这个变量限制在 [0,100] 这个区间内。
到这里,其实我们已经完成了对MRang的使用,但是在Unity编辑器中我们会发现压根没效果。具体原因是,在上诉步骤中我们实际上只进行了约束的设置和存储

四、自定义Property Attribute约束的实现Property Drawer

为了实现MRang对变量m_walkanble的实际约束。我们需要创建一个新的脚本,它继承于UnityEditor.PropertyDrawer这个类,这个新的脚本负责约束的具体实现。这个脚本一定要放在Editor目录下

  1. CustomPropertyDrawer: Unity自带attribute,在此处指定要处理的MRangUnity官网CustomPropertyDrawer介绍
  2. MRangeDrawer: 负责处理Inspector面板内被MRang修饰的变量;
  3. PropertyDrawerUnity官网PropertyDrawer介绍

在这个脚本里, 我们重写了OnGUI这个方法,在这个方法里我们获取了之前设置的边界参数minmax以及当前的变量数值property.intValue,我们用Mathf.Clampproperty.intValue进行了裁剪并将结果覆盖原来的值。

五、其他

  1. 自定义变量增添名称展示



    最后的编辑器Inspector面板结果:
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Unity编辑器中显示自定义结构体,需要使用UnityPropertyDrawer功能。您可以通过创建一个继承自PropertyDrawer的类并使用CustomPropertyDrawer特性来实现。 您可以在代码中这样实现: ``` using UnityEngine; using UnityEditor; [CustomPropertyDrawer(typeof(YourCustomStruct))] public class YourCustomStructDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Your custom GUI code here } } ``` 这样,在Unity编辑器中,您就可以看到您自定义的结构体以符合您的需求的方式显示了。 ### 回答2: 在Unity编辑器中显示自定义struct可以通过创建自定义的Editor类来实现。以下是一个基本的示例: 首先,我们需要创建一个名为"CustomStruct"的自定义struct,包含我们希望显示的成员变量和方法。 ```csharp [System.Serializable] public struct CustomStruct { public int intValue; public float floatValue; public string stringValue; public void CustomMethod() { // 自定义方法的实现 } } ``` 然后,我们创建一个名为"CustomStructEditor"的自定义Editor类,并继承自UnityEditor类库中的Editor类。 ```csharp using UnityEditor; using UnityEngine; [CustomEditor(typeof(MonoBehaviour))] public class CustomStructEditor : Editor { private SerializedProperty intValueProp; private SerializedProperty floatValueProp; private SerializedProperty stringValueProp; private void OnEnable() { intValueProp = serializedObject.FindProperty("intValue"); floatValueProp = serializedObject.FindProperty("floatValue"); stringValueProp = serializedObject.FindProperty("stringValue"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.PropertyField(intValueProp); EditorGUILayout.PropertyField(floatValueProp); EditorGUILayout.PropertyField(stringValueProp); serializedObject.ApplyModifiedProperties(); } } ``` 最后,将该自定义Editor类与含有自定义struct的MonoBehaviour脚本关联起来。例如,我们可以将CustomStructEditor类与一个名为"CustomBehaviour"的脚本关联: ```csharp [CustomEditor(typeof(CustomBehaviour))] public class CustomBehaviourEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); CustomBehaviour customBehaviour = (CustomBehaviour)target; EditorGUILayout.Space(); if (GUILayout.Button("Call Custom Method")) { customBehaviour.customStruct.CustomMethod(); } } } ``` 这样,在Unity编辑器中,我们可以像显示其他属性一样显示我们自定义的struct,同时还可以调用自定义方法。 希望这个简单的示例可以帮到您。 ### 回答3: 在Unity编辑器中显示自定义的struct(结构体)数据类型,需要通过自定义Unity的自定义编辑器(Custom Editor)来实现。 首先,我们需要在Unity项目中创建一个C#脚本,并在脚本中定义我们需要显示的struct数据类型。例如,我们可以创建一个名为"CustomStruct.cs"的脚本,在其中定义了一个名为"CustomStruct"的struct数据类型。 接下来,我们可以创建一个名为"CustomStructEditor.cs"的自定义编辑器脚本。在该脚本中,我们可以使用Unity的GUI系统来自定义显示struct数据类型的方式。我们可以使用GUILayout或者EditorGUILayout等GUI布局函数来创建自定义的Inspector面板。 在CustomStructEditor.cs中,我们可以重写OnInspectorGUI函数,并在该函数中编写我们希望在Inspector面板中显示的内容。我们可以使用SerializedProperty来访问和修改struct中的成员变量。 例如,我们可以使用GUILayout.Label函数来显示一些文本,使用SerializedProperty.FindPropertyRelative函数来获取struct中的成员变量,并使用 EditorGUILayout.PropertyField 函数来显示每个成员变量。 最后,在CustomStruct.cs脚本的声明前面添加"[CustomEditor(typeof(CustomStructEditor))]"来告诉Unity,我们希望使用我们自定义编辑器来显示CustomStruct。 总结,通过使用自定义编辑器,我们可以在Unity编辑器中显示自定义的struct数据类型。自定义编辑器使我们能够按照我们的需求来显示和修改struct的成员变量,使开发工作更加高效和灵活。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值