Unity代码在Inspector上显示的一些小应用

xiaoxiao2025-08-30  10

找到一些小东西,记录一下,以后有备无患。

[HideInInspector] [SerializeField]

[ReadOnly] [readonly]原文链接:http://blog.csdn.net/cartzhang/article/details/53888588

1.引用 using UnityEditor,然后加上下面的代码。

using System.Collections; using System.Collections.Generic; using UnityEngine; #if using UnityEditor; #endif public class ReadOnlyAttribute : PropertyAttribute { } [CustomPropertyDrawer(typeof(ReadOnlyAttribute))] public class ReadOnlyDrawer : PropertyDrawer { public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return EditorGUI.GetPropertyHeight(property, label, true); } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { GUI.enabled = false; EditorGUI.PropertyField(position, property, label, true); GUI.enabled = true; } }

2.写字段的时候加上[ReadOnly]

[ReadOnly] public GameObject C;

Ok,都是些小东西,有备无患吧。

转载请注明原文地址: https://www.6miu.com/read-5035436.html

最新回复(0)