UNITY编辑器扩展
- UNITY编辑器扩展
- Inspector标签
- Multiline / TextArea
- Range
- ContextMenuItem
- ColorUsage颜色拾取
- Header标题
- Space 留白
- Tooltip 说明
- HideInInspector隐藏部分
- RequireComponent
- DisallowMultipleComponent
- Serialize
- FormerlySerializedAs
- AddComponentMenu
- 享受游戏开发
- ExecuteInEditMode
- ContextMenu
- SelectionBase
UNITY编辑器扩展
Inspector标签
Multiline / TextArea
Textfile默认情况下是只有一行的,但同时也能变成复数行显示的TextArea。Multiline和TextArea功能大致上相同,不过Multiline有着[无法依据宽度自动换行]和[不能使用scroll表示]的限制
Range
ContextMenuItem
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[ContextMenuItem (“Random”, “RandomNumber”)]
[ContextMenuItem (“Reset”, “ResetNumber”)]
public int number;
void RandomNumber ()
{
number = Random.Range (0, 100);
}
void ResetNumber ()
{
number = 0;
}
}
ColorUsage颜色拾取
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;
}
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 留白
Tooltip 说明
HideInInspector隐藏部分
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public string str1;
[HideInInspector]
public string str2;
}
RequireComponent
判断必要相关组件
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class NewBehaviourScript : MonoBehaviour
{
Animator animator;
void Awake ()
{
animator = GetComponent<Animator> ();
}
}
DisallowMultipleComponent
禁止添加相同组件
Serialize
在Inspector 中的属性都同时具有Serialize功能(序列化的意思是说再次读取Unity时序列化的变量是有值的,不需要你再次去赋值,因为它已经被保存下来)。
[HideInInspector]表示将原本显示在面板上的序列化值隐藏起来。
[SerializeField]表示将原本不会被序列化的私有变量和保护变量变成可以被序列化的,那么它们在下次读取的值就是你上次赋值的值。
1、如果a是公有的序列化变量。
(1)如果你想要在面板中看到变量a,那么用:
public int a;
(2)如果你不想在面板中看到变量a,那么用:
[HideInInspector]
public int a;
这样a可以在程序中被代码赋值,但不会在面板中看到,也不能手动设置赋值。
2、如果a是私有的序列化变量,你想在面板中读取并赋值,那么用:
[SerializeField]
private int a;
3、如果a是私有的序列化变量,你想在面板中读取,但是不赋值,那么用:
[HideInInspector][SerializedField]
private int a;
public int b
{
get{return a;}
}
然后在Editor中显示,EditorGUILayout.LabelField(“value”,game.b.ToString());
4、如果a是私有序列化变量,你不想在面板中做任何操作(不想看到,也不想写),但是想要在程序中给它赋值,那么用。
[HideInInspector][SerializedField]
private int a;
public int b
{
FormerlySerializedAs
这个属性是在修改变量名的时候,将数据从旧的变量名复制到新的变量名里面去。
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[SerializeField]
string hoge;
}
将变量名由hoge改成fuga
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[SerializeField]
string fuga;
}
AddComponentMenu
在Component菜单中添加项目
using UnityEngine;
[AddComponentMenu("MyUI/Tween Color")]
public class TweenColor : MonoBehaviour
{
}
享受游戏开发
ExecuteInEditMode
这个属性可以使即使没有运行游戏,继承了MonoBehaviour的Component中的主要函数也能够运行。调用的时机是GameObject被更新的时候。双击场景资源,引擎就会加载场景,此时不但Awake、Start函数以及Inspector上的参数会发生变化,并且也会调用Update。另外,OnGUI也会运行,因此在编辑器上会周期性的绘制GUI,使得GUI在编辑器上保持一直显示。
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
在SceneView中选中Object的时候,需要实现[高亮被选中的物体][决定GameObject的选择顺序]之类的功能的时候使用。
通常,在SceneView中选中GameObejct的时候,是GameObject的根物体会高亮显示。
using UnityEngine;[SelectionBase]
public class NewBehaviourScript : MonoBehaviour
{
}