前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity Inspector面板常用的属性

Unity Inspector面板常用的属性

作者头像
meteoric
发布2018-11-20 10:37:48
2.9K0
发布2018-11-20 10:37:48
举报
文章被收录于专栏:游戏杂谈游戏杂谈

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

1、属性只读;

代码语言:javascript
复制
#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]

代码语言:javascript
复制
[ReadOnly]
[SerializeField]
 private string ABC = "abc";

效果如下:

image
image

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

代码语言:javascript
复制
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]

代码语言:javascript
复制
using UnityEngine;
using System.Collections;

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档