1 改變Inspector的外觀
范圍
對于數(shù)值變量扔涧,可以通過添加Range屬性來限定面板中數(shù)值的范圍:
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;
}
多行/文本區(qū)
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
[Multiline(5)]
public string multiline;
[TextArea(3, 5)]
public string textArea;
}
2 增加一個功能
ContextMenuItem
ContextMenuItem可以讓我們讓我們右鍵點擊面板中特定屬性而選擇自定義的一些函數(shù)操作
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,用以區(qū)塊之間的區(qū)分
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的填充數(shù)據(jù)會丟失综慎,但因為我們使用了FormerlySerializedAs,所以原來hoge序列化的數(shù)據(jù)就會被賦值給fuga勤庐。
簡化游戲開發(fā)
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