1 改变Inspector的外观
范围
对于数值变量,可以通过添加Range属性来限定面板中数值的范围:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
[Range(1, 10)]
public int num1;
[Range(1, 10)]
public float num2;
[Range(1, 10)]
public long num3;
[Range(1, 10)]
public double num4;
}
多行/文本区
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
[Multiline(5)]
public string multiline;
[TextArea(3, 5)]
public string textArea;
}
2 增加一个功能
ContextMenuItem
ContextMenuItem可以让我们让我们右键点击面板中特定属性而选择自定义的一些函数操作
ColorUsage
ColorUsage是一个颜色选择器,可以更改HDR拾色器,启用/禁止Alpha
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public Color color1;
[ColorUsage (false)]
public Color color2;
[ColorUsage (true, true, 0, 8, 0.125f, 3)]
public Color color3;
}
3 调整Inspector外观布局
Header
只是用来显示一个Header,用以区块之间的区分
using UnityEngine;
using System;
public class NewBehaviourScript : MonoBehaviour
{
[Header("Player Settings")]
public Player player;
[Serializable]
public class Player
{
public string name;
[Range(1,100)]
public int hp;
}
[Header("Game Settings")]
public Color background;
}
Space
绘制空格
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[Space(16)]
public string str1;
[Space(48)]
public string str2;
}
Tooltip
作提示用
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[Tooltip("これはツールチップです")]
public long tooltip;
}
HideInInspector
在Inspector中隐藏public变量
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public string str1;
[HideInInspector]
public string str2;
}
便利Inspector
RequireComponent
要求必须拥有某一组件,如果没有则自动添加
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class NewBehaviourScript : MonoBehaviour
{
Animator animator;
void Awake ()
{
animator = GetComponent<Animator> ();
}
}
DisallowMultipleComponent
不允许同时拥有两个相同的组件
FormerlySerializedAs
原来是这样的
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[SerializeField]
string hoge;
}
后来修改代码成了
using UnityEngine;
using UnityEngine.Serialization;
public class NewBehaviourScript : MonoBehaviour
{
[SerializeField]
[FormerlySerializedAs("hoge")]
string fuga;
}
照理说原来面板上hoge的填充数据会丢失,但因为我们使用了FormerlySerializedAs,所以原来hoge序列化的数据就会被赋值给fuga。
简化游戏开发
ExecuteInEditMode
不运行游戏,仅仅在Editor下也会运行MonoBehaviour
using UnityEngine;
[ExecuteInEditMode]
public class NewBehaviourScript : MonoBehaviour
{
[Range(0,10)]
public int number;
void Awake ()
{
Debug.Log ("Awake");
}
void Start ()
{
Debug.Log ("Start");
}
void Update ()
{
Debug.Log ("Update");
}
}
ContextMenu
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[Range (0, 10)]
public int number;
[ContextMenu ("RandomNumber")]
void RandomNumber ()
{
number = Random.Range (0, 100);
}
[ContextMenu ("ResetNumber")]
void ResetNumber ()
{
number = 0;
}
}
SelectionBase
using UnityEngine;
[SelectionBase]
public class NewBehaviourScript : MonoBehaviour
{
}
本文转自:http://blog.csdn.net/rickshaozhiheng/article/details/52670771