[Header("这个是inspector 上的信息头")]
[Range(-2,2)] //给个scrollBar
[Tooltip("提示的文字")] //这个属性可以为变量上生成一条tip,当鼠标指针移动到Inspector上时候显示。
[Multiline(5)] //text 的区域,最多保留5行
[TextArea(2,4)]//text 的区域,最多显示4行最少2行
[ContextMenu ("Do Something")]//在脚本上右键做一些事
ExecuteInEditMode //默认状态下,MonoBehavior中的Start,Update,OnGUI等方法,需要在Play的状态下才会被执行。
//这个属性让Class在Editor模式(非Play模式)下也能执行。
HideInInspector //在变量上使用这个属性,可以让public的变量在Inspector上隐藏,也就是无法在Editor中进行编辑。
ImageEffectOpaque //在OnRenderImage上使用,可以让渲染顺序在非透明物体之后,透明物体之前。
RPC //在方法上添加该属性,可以网络通信中对该方法进行RPC调用。
DisallowMultipleComponent //对一个MonoBehaviour的子类使用这个属性,那么在同一个GameObject上面,最多只能添加一个该Class的实例。
//尝试添加多个的时候,会出现Invalid operation 的错误Can't add "那个脚本" to MidAnchor because a "那个脚本" is already added to the game object!
ContextMenuItemAttribute //这个属性是Unity4.5之后提供的新功能,可以在Inspector上面对变量追加一个右键菜单,并执行指定的函数。
RequireComponent //在Class上使用,添加对另一个Component的依赖。当该Class被添加到一个GameObject上的时候,如果这个GameObject不含有依赖的Component,会自动添加该Component。
//且该Componet不可被移除。
RuntimeInitializeOnLoadMethodAttribute //此属性仅在Unity5上可用。在游戏启动时,会自动调用添加了该属性的方法。
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
Debug.Log("Game loaded and is running");
}
SelectionBaseAttribute //当一个GameObject含有使用了该属性的Component的时候,在SceneView中选择该GameObject,Hierarchy上面会自动选中该GameObject的Parent。
SerializeField //在变量上使用该属性,可以强制该变量进行序列化。即可以在Editor上对变量的值进行编辑,即使变量是private的也可以。在UI开发中经常可见到对private的组件进行强制序列化的用法。
SharedBetweenAnimatorsAttribute //用于StateMachineBehaviour上,不同的Animator将共享这一个StateMachineBehaviour的实例,可以减少内存占用。
[Space(100)] //两个之间的空隙
SpaceAttribute //使用该属性可以在Inspector上增加一些空位。
FormerlySerializedAsAttribute //该属性可以令变量以另外的名称进行序列化,并且在变量自身修改名称的时候,不会丢失之前的序列化的值。
using UnityEngine;
using UnityEngine.Serialization;
public class MyClass : MonoBehaviour {
[FormerlySerializedAs("myValue")]
private string m_MyValue;
public string myValue
{
get { return m_MyValue; }
set { m_MyValue = value; }
}
}
UnityEngine.Editor
该package为Editor开发专用
CallbackOrderAttribute
定义Callback的顺序
CanEditMultipleObjects
Editor同时编辑多个Component的功能
CustomEditor
声明一个Class为自定义Editor的Class
CustomPreviewAttribute
将一个class标记为指定类型的自定义预览
Unity4.5以后提供的新功能
例子:
[CustomPreview(typeof(GameObject))]
public class MyPreview : ObjectPreview
{
public override bool HasPreviewGUI()
{
return true;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
GUI.Label(r, target.name + " is being previewed");
}
}
CustomPropertyDrawer
标记自定义PropertyDrawer时候使用。
当自己创建一个PropertyDrawer或者DecoratorDrawer的时候,使用该属性来标记。 TODO: 如何创建属于自己的Attribute
DrawGizmo
可以在Scene视图中显示自定义的Gizmo
下面的例子,是在Scene视图中,当挂有MyScript的GameObject被选中,且距离相机距离超过10的时候,便显示自定义的Gizmo。
Gizmo的图片需要放入Assets/Gizmo目录中。
例子:
using UnityEngine;
using UnityEditor;
public class MyScript : MonoBehaviour {
}
public class MyScriptGizmoDrawer {
[DrawGizmo (GizmoType.Selected | GizmoType.Active)]
static void DrawGizmoForMyScript (MyScript scr, GizmoType gizmoType) {
Vector3 position = scr.transform.position;
if(Vector3.Distance(position, Camera.current.transform.position) > 10f)
Gizmos.DrawIcon (position, "300px-Gizmo.png");
}
}
InitializeOnLoadAttribute
在Class上使用,可以在Unity启动的时候,运行Editor脚本。
需要该Class拥有静态的构造函数。
做一个创建一个空的gameobject的例子。
例子:
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
class MyClass
{
static MyClass ()
{
EditorApplication.update += Update;
Debug.Log("Up and running");
}
static void Update ()
{
Debug.Log("Updating");
}
}
InitializeOnLoadMethodAttribute
在Method上使用,是InitializeOnLoad的Method版本。
Method必须是static的。
MenuItem
在方法上使用,可以在Editor中创建一个菜单项,点击后执行该方法,可以利用该属性做很多扩展功能。 需要方法为static。
例子:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class TestMenuItem : MonoBehaviour {
[MenuItem ("MyMenu/Create GameObject")]
public static void CreateGameObject() {
new GameObject("lvmingbei's GameObject");
}
}
PreferenceItem
使用该属性可以定制Unity的Preference界面。
在这里就使用官方的例子: (Edit --> Preferences)
using UnityEngine;
using UnityEditor;
using System.Collections;
public class OurPreferences {
// Have we loaded the prefs yet
private static bool prefsLoaded = false;
// The Preferences
public static bool boolPreference = false;
// Add preferences section named "My Preferences" to the Preferences Window
[PreferenceItem ("My Preferences")]
public static void PreferencesGUI () {
// Load the preferences
if (!prefsLoaded) {
boolPreference = EditorPrefs.GetBool ("BoolPreferenceKey", false);
prefsLoaded = true;
}
// Preferences GUI
boolPreference = EditorGUILayout.Toggle ("Bool Preference", boolPreference);
// Save the preferences
if (GUI.changed)
EditorPrefs.SetBool ("BoolPreferenceKey", boolPreference);
}
}
UnityEditor.Callbacks
这个package中是三个Callback的属性,都需要方法为static的。
OnOpenAssetAttribute
在打开一个Asset后被调用。
例子:
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
public class MyAssetHandler {
[OnOpenAssetAttribute(1)]
public static bool step1(int instanceID, int line) {
string name = EditorUtility.InstanceIDToObject(instanceID).name;
Debug.Log("Open Asset step: 1 ("+name+")");
return false; // we did not handle the open
}
// step2 has an attribute with index 2, so will be called after step1
[OnOpenAssetAttribute(2)]
public static bool step2(int instanceID, int line) {
Debug.Log("Open Asset step: 2 ("+instanceID+")");
return false; // we did not handle the open
}
}
PostProcessBuildAttribute
该属性是在build完成后,被调用的callback。
同时具有多个的时候,可以指定先后顺序。
例子:
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
public class MyBuildPostprocessor {
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
Debug.Log( pathToBuiltProject );
}
}