Unity3d中的属性(Attributes)整理

1.MenuItem 添加菜单栏选项,可在unity的菜单栏中选择,选择后执行相应方法,下面的例子是一个设置label控件字体颜色的方法,老版本的NGUI上用的,新版都自带了。(OnGUI()是继承了EditorWindow 的类里会被引擎自动调用的方法)

public class SetLabelColor : EditorWindow 
{
    [MenuItem("tools/SetColor")]
    static void SetColor() 
    {
        EditorWindow.GetWindow<SetLabelColor>();
    }

    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        Color = EditorGUILayout.TextField("SetColor:", Color);
        if (GUILayout.Button("Set"))
        {
            Label label = Selection.activeGameObject.GetComponent<Label>();
            if (label != null)
            {
                float r = ((float)Convert.ToInt32(mainColor.Substring(0, 2), 16)) / 255.0f;
                float g = ((float)Convert.ToInt32(mainColor.Substring(2, 2), 16)) / 255.0f;
                float b = ((float)Convert.ToInt32(mainColor.Substring(4, 2), 16)) / 255.0f;
                label.color = new Color(r, g, b, 1);
            }
        }
        EditorGUILayout.EndHorizontal();
    }
}

2.AddComponentMenu 添加组件菜单
在类之前声明这个属性,此类便可以出现在”Componet”菜单下的任何位置,要重启U3D才能显示。

[AddComponentMenu("MyType")]
public class MyType: MonoBehaviour
{}

3.ContextMenu
这个属性是在监视器的脚本中加一个触发事件,监视器中的小拉菜单中(齿轮图标)。在方法前加上这个属性,点击菜单中的相应按钮,就会执行该方法。和上面两个属性用法差不多,只是放的入口不同。

public class Attributes : MonoBehaviour {
    [ContextMenu("Hello World!")]
    void HelloWorld()    
    {
        Debug.Log("Hello World!");
    }   
}

4.ExecuteInEditMode
在编辑器中运行,用来调试用的,该属性有三种函数的调用方式:
a- “Update()” is only called when something in the scene changed.
b- “OnGUI()” is called when the Game View recieves an Event.
c- “OnRenderObject()” and the other rendering callback functions are called on every repaint of the Scene View or Game View.

[ExecuteInEditMode]
public class Test: MonoBehaviour
{
    void Update()
    {
        //DoSomething;
    }
}

5.RequireComponent
加入一个组建之前必须存在另一个相应的组建,若没有则自动创建,这个属性是用来避免出错的,在需要依赖其他组件的类里最好加上。

[RequireComponent (typeof (Rigidbody))]
public class RequireComponentTest : MonoBehaviour {
    void FixedUpdate()  {
        rigidbody.AddForce(Vector3.up);
    }
}

6.Serializable 可序列化
这个属性可以让子类(继承类)的变量属性显示在检视面板中,也能序列化它。可以用来储存预设中编辑的属性,JS版本不需要这个属性。

//SerializableTest.cs
[System.Serializable]
public class SerializableTest
{
    public int p = 5;
    public Color c = Color.white;
}

//SerializableTest2.cs
public class SerializableTest2 : MonoBehaviour
{
    public SerializableTest test;
}

7.NonSerialized 不被序列化
不被序列化该变量,且不显示在检视面板中。

public class Test {
    [System.NonSerialized]
    public int i_Helloword = 5;
}

8.HideInInspector 在检视面板中隐藏

public class HideInspectorTest : MonoBehaviour 
{
    [HideInInspector]
    public GameObject m_Target;
    void Awake()
    {
        m_Target = transform.Find("test").gameObject;
    }
}

9.ImageEffectOpaque
使 OnRenderImage 透明呈现,渲染优化的时候用的。

[ImageEffectOpaque]
void OnRenderImage(RenderTexture source, RenderTexture destination)
{}

10.MonoPInvokeCallbackAttribute
C#(托管代码) 中注册方法可以从C++(非托管代码) 调用。

11.DLLImport
C++(非托管代码) 的方法可从c#调用。

[DllImport("DLLName")]
 private static extern void MethodName();

12.自定义属性,可传递需要的值,下面的例子是一个UI窗口的基类方法,使用属性传递UI预设的地址,也可以传递其他的窗口信息,这里只做演示,实际上通过配表读取比较好(或者用unity的ScriptableObject,通过AssetDatabase.CreateAsset (ScriptableObject.CreateInstance (), pathName)创建),这里的做法不支持热更新。

//Attribute.cs
public class UIAttribute : Attribute {
    public UIWndAttributes(string path) { this.path = path; }
    public string path;
}

//WndBase.cs
public class WndBase  {
    public GameObject wnd { get; protected set; }
    public UIPanel panel { get; protected set; }
    public EUILayer layer { get; protected set; }
    public void Create(EUILayer layer, GameObject wnd,)
    {
        var uiAttributes = (UIAttribute)this.GetType().GetCustomAttributes(typeof(UIAttribute), true).FirstOrDefault();

        this.wnd = wnd;
        this.layer = layer;
        panel = wnd.GetComponent<UIPanel>();
    }
}

//TestUIClass.cs
[UIAttribute("Scripts/UIManager/Test"]
public class TestClass1 : WndBase
{
    protected override void Open(object data)
    {
        base.Open(data);
        AnimationHelper.BoxPlayForward(wnd);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值