Unity3d中的属性(Attributes)

Attributes属性属于U3D的RunTimeClass,所以加上以下的命名空间是必须的了。

using UnityEngine;

using System.Collections;

0.Property Attributes变量属性(放在方括号内变量申明前)(常用)

1/ Range(min,max)

如: [SerializeField, Range(0, 5)]

int count;

如: [SerializeField, Range(0,5)]

int[] counts;

2/ Multiline 多行

如: [Multiline]

public string b;

3/ TextArea 文本区域

可以设置的最大值和最小值的行数

如: [TextArea]

public string c;

4/ ContextMenu上下文菜单

如: [ContextMenu("Hello World!")]

void HelloWorld()

{

Debug.Log("Hello World!");

}

5/ ContextMenuItem(右击变量调用方法)

如:[ContextMenuItem("Resetvalue","resetValue")]

public int e=100;

public void resetValue(){

e=50;

     }

6/ Head("This is a variable") 头显示

7/ Space(5)

设置字段与字段之间的空间

8/ [Tooltip("this is a nasha!")]

如果鼠标光标是在字段上,显示的说明文本。

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!");

}

}

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;

}

8.SerializeField 序列化域(强制序列化)

这里写得比较清楚,可以将私有变量序列化,将U3D的内建变量序列化等。

Unity属性(Attributes)的更多相关文章

  1. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

  2. IOS开发UI基础文本属性Attributes

    文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...

  3. Unity属性的封装、继承、方法隐藏

    (一)Unity属性封装.继承.方法隐藏的学习和总结 一.属性的封装 1.属性封装的定义:通过对属性的读和写来保护类中的域. 2.格式例子: private string departname; // ...

  4. iOS- 详解文本属性Attributes(转)

    iOS- 详解文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont s ...

  5. (转)Unity3d中的属性(Attributes)整理

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

  6. Unity3d中的属性(Attributes)整理

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

  7. (转载)Unity3d中的属性(Attributes)整理

    附加: float字段检视面板修改:[Range(1,10)] 对属性进行分组:[Header("xxx")] 工具栏中调用方法,类文件需在Editor文件夹中:[MenuIte( ...

  8. zencart批量插入TEXT文本属性attributes

    有时候上传的产品与多级分类比较多,在后台添加文本属性如Name,Number等需要顾客自定义的内容就比较费神了.现在只需将以下代码保存为insert_attributes.php,变量$options ...

  9. 文本属性Attributes

    1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...

随机推荐

  1. fancybox关闭弹出窗口parent.$.fancybox.close();

    fancybox弹出窗口右上角会自带一个关闭窗口,并且点击遮罩层也会关闭fancybox 有时我们不需要这样进行关闭,隐藏关闭窗口,并且遮罩层不可点击 在弹出窗口页面加一链接进行关闭使用parent. ...

  2. CF 332A Down the Hatch! 超级水题。。不过题目太长了

    A. Down the Hatch! time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 由IEnumerable和IEnumerator的延伸

    相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是 ...

  4. 为Exchange 2007 SCC 启用 SCR 副本-供需要的人使用!

    SCC 已经部署完整,接下来我们必须防范本地站点如果出现了完全的损坏怎么办? Exchange 2007 SP1 提供了另外一种高可用的方式,就是基于我们的SCR 模型,SCR 模型是基于SCC 本地 ...

  5. 模块化与MVC

    [javascript激增的思考02]模块化与MVC 前言 之前我们遇到了这么一个项目,也就是我们昨天提到的,有很多的小窗口的,昨天说的太抽象了,今天我们再来理一理什么是小窗口(后面点说下),当时由于 ...

  6. Android打开系统设置

    今天在做项目过程中,遇到一个问题:用户体验某个功能时需要查看用户是否已经打开了GPS定位服务,若没有则要求进入定位服务设置界面. 下面就直接贴出代码 以下代码是放在了Button的监听事件里,只贴出重 ...

  7. Android屏幕适配问题详解

    上篇-Android本地化资源目录详解 :http://www.cnblogs.com/steffen/p/3833048.html 单位: px(像素):屏幕上的点. in(英寸):长度单位. mm ...

  8. 第2章 开始入手 —— 01 创建第一个 Android 应用程序

    创建一个新的 Android 项目 操作步骤: (1) 选择 File | New | Android Application Project ,弹出 New Android Application ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由

    本章的重点是对产品信息增加排序和分页的功能,以及使用ASP.NET Routing特性添加更加友好的URL支持. 注意:如果你想按照本章的代码编写示例,你必须完成第四章或者直接从www.apress. ...

  10. C#_单例模式

    单例:在程序的整个进程中只会被实例化一次 如:User user =new User();实例化一个User();的时候new User()是调用的 User类的 默认的公有构造函数:public U ...