Unity3D 的一些Attribute (一些是5.0新的API)

[TextArea(int min,int max)] //使用文本域 在 inspector里面  能自动换行

 public string a="sdf";


[Tooltip(string str)] //使用工具提示

 public string a="sdf";


[Range(0,2)]//可以限制 属性的值

 public float/int a=0;


AssemblyIsEditorAssembly

Assembly level attribute. Any classes in an assembly with this attribute will be considered to be Editor Classes.
汇编级属性。任何类在组装这个属性将被认为是编辑器类。


DisallowMultipleComponent

Prevents MonoBehaviour of same type (or subtype) to be added more than once to a GameObject.

防止重复添加相同的类(或子类);


[Header("标题。。")]//就是用来加一段文字的,,,是显示在属性之前的

 [Header("Health Settings")]
    public int health = 0;

Multiline

[multiline(int lines)]//多行  -------和 textArea差不多   差别就在于  这个不能自动换行      textArea能自动换行

Attribute to make a string be edited with a multi-line textfield.


[NotConverted]//防止被覆盖
[NotRenamed]//防止被重命名
		public class Dictionary
		{
			public object this[object key]
			{
				get { throw new System.NotSupportedException(); }
				set { throw new System.NotSupportedException(); }
			}
		}

RuntimeInitializeOnLoadMethod  //在方法中添加

Namespace: UnityEngine

Description

Allow an runtime class method to be initialized when Unity game loads runtime without action from the user.

允许运行时类方法统一游戏时要初始化加载运行时从用户没有行动。

using UnityEngine;

class MyClass
{
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad ()
    {
        Debug.Log("Game loaded and is running");
    }
}

[Space(10)]//在属性中添加间距



SharedBetweenAnimatorsAttribute

Namespace: UnityEngine

Description

SharedBetweenAnimatorsAttribute is an attribute that specify that this StateMachineBehaviour should be instantiate only once and shared among all Animator instance. This attribute reduce the memory footprint for each controller instance.

It's up to the programmer to choose which StateMachineBehaviour could use this attribute. Be aware that if your StateMachineBehaviour change some member variable it will affect all other Animator instance using it. See Also: StateMachineBehaviour class.

using UnityEngine;
        
[SharedBetweenAnimators]
public class AttackBehaviour : StateMachineBehaviour
{
	public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		Debug.Log("OnStateEnter");
	}
}

UnityAPICompatibilityVersionAttribute

Namespace: UnityEngine

Description

Declares an assembly to be compatible (API wise) with a specific Unity API. Used by internal tools to avoid processing the assembly in order to decide whether assemblies may be using old Unity API.

声明了一个装配(API兼容wise)与特定的统一的API。内部使用的工具,以避免加工组装,以决定是否组件可以使用旧的统一API。



、、---------------------------------    下面的来自于 http://blog.sina.com.cn/s/blog_5b6cb9500101857b.html

ttributes属性属于U3D的RunTimeClass,所以加上以下的命名空间是必须的了。其它倒没什么需要注意的。本文将所有运行属性过一遍罢了。
using UnityEngine;
using System.Collections;

1.AddComponentMenu 添加组件菜单
这函数只是起方便用,原本的脚本(组建)都会在“Component/Script”菜单下,在类之前声明一下这个,它便可以出现在"Componet"菜单下的任何位置。说明指的是要重启U3D才能显示,不过测试貌似直接可以显示。

[AddComponentMenu("MyPhysic/PhysicType")]
public class PhysicType: MonoBehaviour
{
}

2.ContextMenu 上下文菜单
这个译名我觉得很不自然,其实上下文算是啥东西……这个函数是在Inspector的脚本中加一个触发事件,就是删除脚本重置脚本按钮的那个小拉菜单中,具体很难说清位置,所以我截了个图。
public class Attributes : MonoBehaviour {
    [ContextMenu("Hello World!")]
    void HelloWorld()    
    {
        Debug.Log("Hello World!");
    }    
}
Unity3d中的属性(Attributes)整理

3.ExecuteInEditMode 在Editor模式下运行
跟名字一样,在编辑器中运行。不过有三种函数的调用方式。
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 ExecuteInEditModeTest: MonoBehaviour
{
    private Vector3 vec_Rotation = new Vector3(0.0f, 0.5f, 0.0f);
    //Rotate all the time
    void OnRenderObject()
    {
        transform.Rotate(vec_Rotation);
    }
}

4.HideInInspector 在检视面板中隐藏
public class HideInspectorTest : MonoBehaviour 
{
     
    [HideInInspector]

    public Transform m_Target;
    void Start()
    {
        m_Target = GameObject.Find("test").transform;
    }

}

5.RequireComponent 必须要有相应的组建
加入一个组建之前必须存在另一个相应的组建,若没有则自动创建。这个在项目中非常有必要用到,尤其是项目人员比较多的时候(大于三四个)。
[RequireComponent (typeof (Rigidbody))]
public class RequireComponentTest : MonoBehaviour {
    void FixedUpdate()   {
        rigidbody.AddForce(Vector3.up);
    }
}

6.NonSerialized 不被序列化
不被序列化该变量,且不显示在检视面板中。
public class Test {
    [System.NonSerialized]
    public int i_Helloword = 5;
}

7.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;

}

Unity3d中的属性(Attributes)整理

8.SerializeField 序列化域(强制序列化)
这里写得比较清楚,可以将私有变量序列化,将U3D的内建变量序列化等。
http://game.ceeger.com/Script/Attributes/SerializeField.html

下面的ATTRIBUTE属性估计是没什么人用的了,我也没怎么用过。

1.ImageEffectOpaque 不透明图像效果优先
Any Image Effect with this attribute will be rendered after opaque geometry but before transparent geometry.
This allows for effects which extensively use the depth buffer (SSAO ect) to only affect opaque pixels. This Attribute can be used to reduce the amount of visual artifacts in a scene with post processing.
没用过这玩意,不过应该很少用得到,优化加速渲染。

2.ImageEffectTransformsToLDR 
也没用过这玩意,LDR应该是某种加载方式。高动态光照渲染(High-Dynamic Range,简称HDR)。
When using HDR rendering it can sometime be desirable to switch to LDR rendering during ImageEffect rendering.
Using this Attribute on an image effect will cause the destination buffer to be an LDR buffer, and switch the rest of the Image Effect pipeline into LDR mode. It is the responsibility of the Image Effect that this Attribute is associated to ensure that the output is in the LDR range.

3.NotConvertedAttribute 不转换属性
我觉得这个应该是没有人用的……打包资源的时候,不将成员或类型转换到相应平台,不是很理解这是干嘛的。
Instructs the build pipeline not to convert a type or member to the target platform.


4.NotFlashValidatedAttribute 不允许转换到FLASH平台
又是个没人用的东西吧?
Instructs the build pipeline not to try and validate a type or member for the flash platform.

5.NotRenamedAttribute 不允许属性更名
……

6.PropertyAttribute 财产属性?搞不懂 是 属性类的抽象类   没有用
也不知道用来做啥的。

7.PRC
这个貌似跟NETWORK相关,U3D的NETWORK渣渣,不管了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值