协程可以通过关闭它所在的物体来停止SetActive(false)或调用Destroy(example)其中example是一个MonoBehaviour实例会立即触发OnDisable并处理协程从而有效地阻止它调用enable不会停止协程。。一、属性分为Editor属性和Engine属性Editor属性需要放在Editor文件夹下Engine属性不需要。1.1 Engine属性AddComponentMenuAddComponentMenu属性允许您将脚本放置在“组件”菜单中的任何位置而不仅仅是“组件-脚本”菜单。using UnityEngine; [AddComponentMenu(Transform/Follow Transform)] public class FollowTransform : MonoBehaviour{ }AssemblyIsEditorAssembly程序集中任何含有此属性的类都被当作是编辑器类。AssemblyIsEditorAssemblyConstructor.BeforeRenderOrder在调用Application.onBeforeRender事件时根据添加了该属性的order来确定方法执行的顺序从低到高执行如果没加该属性默认order是0。ColorUsage Attribute为颜色增加一个颜色选择器有两个参数第一个是否显示alpha值第二个是否显示HDR颜色。ColorUsageAttributeAttribute for Color fields. Used for configuring the GUI for the color.ContextMenu为脚本右上角的齿轮添加一个运行方法的快捷键对场景内配置数据比较有用。using UnityEngine; public class ContextTesting : MonoBehaviour { /// Add a context menu named Do Something in the inspector /// of the attached script. [ContextMenu(Do Something)] void DoSomething() { Debug.Log(Perform operation); } }ContextMenuItem给这个属性右键添加一个方法即在属性面板上右击playerBiography会出现Reset按钮点击该按钮执行ResetBiography方法。using UnityEngine; public class Example : MonoBehaviour { [ContextMenuItem(Reset, ResetBiography)] [Multiline(8)] string playerBiography ; void ResetBiography() { playerBiography ; } }CreateAssetMenu把继承自ScriptableObject的类创建成一个“.asset”资源文件默认在Assets/Create/你脚本的名字的子菜单下。PropertiesfileName新创建实例的名字menuName在Assets/Create菜单下的名字order在Assets/Create菜单中的顺序ExecuteAlways不管是在编辑器状态还是在运行状态该脚本始终在运行。当您希望脚本作为编辑器工具的一部分执行某些事情时可以使用[ExecuteAlways]属性。A MonoBehaviour要确保其中没有编写游戏逻辑只是在编辑器模式下运行否则可能会造成错误。可以通过Application.IsPlaying来确定游戏是否在运行状态。using UnityEngine; [ExecuteAlways] public class ExampleClass : MonoBehaviour { void Start() { if (Application.IsPlaying(gameObject)) { // Play logic } else { // Editor logic } } }ExecuteInEditMode让脚本在编辑器状态下也能运行。using UnityEngine; [ExecuteInEditMode] public class PrintAwake : MonoBehaviour { void Awake() { Debug.Log(Editor causes this Awake); } void Update() { Debug.Log(Editor causes this Update); } }GradientUsage Attribute返回一个颜色的渐变编辑器参数是是否打开HDR。Header添加一个标头使用DecoratorDrawer。using UnityEngine; public class Example : MonoBehaviour { [Header(Health Settings)] public int health 0; public int maxHealth 100; [Header(Shield Settings)] public int shield 0; public int maxShield 0; }HelpURLAttribute为一个类提供一个方法说明按住CTRL键可转到链接点击下面的图表就会转到说明。using UnityEngine; using UnityEditor; [HelpURL(http://example.com/docs/MyComponent.html)] public class MyComponent { }HideInInspector属性在属性面板上隐藏。public class Example : MonoBehaviour { [HideInInspector] public int p 5; }ImageEffectAfterScale添加这个属性的图片会在Dynamic Resolution stage之后渲染。如果您希望在动态分辨率缩小后应用图像效果请添加此属性。效果将以全分辨率呈现这对于在某种程度上依赖于屏幕宽度和高度的特定大小的效果非常重要。没看出来啥效果可能就是分辨率变小了图片的分辨率并不会改变这样更清晰。ImageEffectAllowedInSceneView在Scene view camera之后渲染。ImageEffectOpaque图片将在透明和非透明之间物体渲染即opaque geometry but before transparent geometry。这允许广泛使用深度缓冲SSAO等来影响不透明像素的效果。此属性可用于减少具有后期处理的场景中的可视工件数量。InspectorName使用这个属性改变在属性面板中的名字但是要加在枚举类型上。using UnityEngine; public enum ModelImporterIndexFormat { Auto 0, [InspectorName(16 bits)] UInt16 1, [InspectorName(32 bits)] UInt32 2, }Min限定一个float或者int类型的最小值。Multiline声明一个可以多行编辑的字符串。Range给float或int变量一个范围用slider表示。RequireComponentRequireComponent会自动添加需要的组件。using UnityEngine; // PlayerScript requires the GameObject to have a Rigidbody component [RequireComponent(typeof(Rigidbody))] public class PlayerScript : MonoBehaviour { Rigidbody rb; void Start() { rb GetComponentlt;Rigidbodygt;(); } void FixedUpdate() { rb.AddForce(Vector3.up); } }RuntimeInitializeOnLoadMethod在场景加载之后运行该类的静态方法。标记有[RuntimeInitializeOnLoadMethod]的方法在加载场景之后、在Awake调用之后执行。Note:但是标记有[RuntimeInitializeOnLoadMethod]的方法的执行顺序不确定。using UnityEngine; class MyClass { [RuntimeInitializeOnLoadMethod] static void OnRuntimeMethodLoad() { Debug.Log(After Scene is loaded and game is running); } [RuntimeInitializeOnLoadMethod] static void OnSecondRuntimeMethodLoad() { Debug.Log(SecondMethod After Scene is loaded and game is running.); } }SelectionBase把这个属性添加到一个类上面并把该脚本添加到一个物体上面。当你在场景中选中该物体的子物体时会快速定位到它身上。就比如你点击一个prefab的子物体它会自动定位到根节点一样单击一次会定位到挂有该脚本的物体单击两次会定位到你点击的物体。SerializeField强制Unity序列化一个私有字段。当Unity序列化你的脚本时它只序列化公共字段。如果要序列化私有字段添加SerializeField属性。Unity序列化所有的脚本重新加载新的程序集并从序列化的版本中重新创建脚本组件。这种序列化是通过一个内部的Unity序列化系统完成的而不是.NET的序列化功能。序列化系统可以做到以下几点- 可以序列化public非静态字段或者serializable类型- 可以通过SerializeField属性序列化私有的非静态类型- 不能序列化静态字段这也就是为何静态字段不能在属性面板上显示出来的原因不管是不是公有的或私有的- 不能序列化属性Unity可以序列化以下类型的字段- 继承自UnityEngine.Object类的类型比如GameObject、Component、MonoBehaviour、Texture2D、AnimationClip- 所有基础类型int、string、float、bool- 一些内置类型Vector2、Vector3、Vector4、Quaternion、Matrix4x4、Color、Rect、LayerMask- serializable type类型的数组- serializable type类型的列表- Enums枚举- Structs结构体For more information on serialization, see Script Serialization.Note:如果您将一个元素放入一个列表或数组两次当列表被序列化时您将得到该元素的两个副本而不是一个副本在新列表中出现两次。Note:如果你想要序列化自定义的结构体Struct field需要给结构体加上[System.Serializable]属性。Hint:Unity不能序列化字典Dictionary但是你可以通过两个list分别存储keys和values然后再在Awake()里面把它们存储到字典里面。他没有解决什么时候需要修改字典并将其“保存”回来的问题但是在许多其他情况下这是一个方便的技巧。using UnityEngine; public class SomePerson : MonoBehaviour { //This field gets serialized because it is public. public string firstName John; //This field does not get serialized because it is private. private int age 40; //This field gets serialized even though it is private //because it has the SerializeField attribute applied. [SerializeField] private bool hasHealthPotion true; void Start() { if (hasHealthPotion) Debug.Log(Persons first name: firstName Persons age: age); } }SharedBetweenAnimators指定StateMachineBehaviour只实例化一次并在所有Animator实例之间共享此属性减少每个控制器实例的内存占用。你来决定哪个StateMachineBehaviour使用这个属性attribute。请注意如果您的StateMachineBehaviour更改了某个成员变量它将影响使用它的所有其他Animator实例。See Also: StateMachineBehaviour classusing UnityEngine; [SharedBetweenAnimators] public class AttackBehaviour : StateMachineBehaviour { public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Debug.Log(OnStateEnter); } }Space使用DecoratorDrawer在属性面板上添加一些空格。using UnityEngine; public class Example : MonoBehaviour { int health 0; int maxHealth 100; [Space(10)] // 10 pixels of spacing here. int shield 0; int maxShield 0; }TextArea实现一个高度可调可以滚动的字符串。你可以声明区域框显示的最小和最大的字符串的行数即minimum and maximum。文字区域会根据文字的内容自动扩展输入框的大小超出的部分会有一个滚动条。Note:最大行是指文本区域显示字符串的行数。用户输入的行数没有上限。using UnityEngine; public class TextAreaExample : MonoBehaviour { [TextArea] public string MyTextArea; }TooltipAttribute为一个属性显示一个说明当鼠标停留在上面的时候即tooltip for a field in the Inspector window。Tooltip hovering over the class it was added to.using UnityEngine; public class Example : MonoBehaviour { [Tooltip(Health value between 0 and 100.)] int health 0; }