首页 > 编程语言 > 详细

Unity Inspector面板常用的属性

时间:2018-06-26 14:20:06      阅读:291      评论:0      收藏:0      [点我收藏+]

在扩展Unity的时候,往往会用到一些属性,这里将常用的列一下。

1、属性只读;

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;


public class ReadOnlyAttribute : PropertyAttribute
{

}

#if UNITY_EDITOR
[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;
    }
}
#endif
 
 
[ReadOnly]
 public string PLUGIN = "";

 

2、私有变量在 Inspector 显示出来  [SerializeField]

[ReadOnly]
[SerializeField]
 private string ABC = "abc";

 

效果如下:

技术分享图片

 

3、为属性添加头部说明 [HeaderAttribute]

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [Header("Health Settings")]
    public int health = 0;
    public int maxHealth = 100;
    [Header("Shield Settings")]
    public int shield = 0;
    public int maxShield = 0;
}

 

 

4、隐藏属性 [HideInInspector]

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [HideInInspector]
    public int p = 5;
}

 

其它还有诸如 HelpURL 等,详情可参考 官方帮忙文档 https://docs.unity3d.com/ScriptReference/HeaderAttribute.html

Unity Inspector面板常用的属性

原文:https://www.cnblogs.com/meteoric_cry/p/9228784.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!