当前位置:   article > 正文

unity--特性(二)_unity contextmenuitem

unity contextmenuitem

一、HelpURLAttribute
从字面意思理解,是查看帮助时,跳转到指定的页面。

如下图:

 

image.png

对应着蓝色小书的图标,点击以后会跳转到配置的URL。

二、RangeAttribute:限定int或float的取值范围。

Attribute used to make a float or int variable in a script be restricted to a specific range.
When this attribute is used, the float or int will be shown as a slider in the Inspector instead of the default number field.

当在int或float上应用RangeAttribute特性时,在Inspector面板中,显示的将是一个slider滑动条,而不是默认的数值字段。

  1. [Range(0.1f,0.9f)]
  2. float ratio;

三、RequireComponentAttribute

自动添加所要依赖的组件,如将一个Script做为一个GameObject的组件,而这个Script需要访问Rigidbody组件,
通过应用该属性,可以自动的添加Rigidbody组件到当前的GameObject中,避免设置错误。

*前提是:如果当前的Script已经添加到了GameObject,这时候你再应用RequireComponent特性是无效的,
你必须删除 再重新添加,才会检测。

  1. using UnityEngine;
  2. // PlayerScript requires the GameObject to have a Rigidbody component
  3. [RequireComponent(typeof(Rigidbody))]
  4. public class PlayerScript : MonoBehaviour
  5. {
  6. Rigidbody rb;
  7. void Start()
  8. {
  9. rb = GetComponent<Rigidbody>();
  10. }
  11. void FixedUpdate()
  12. {
  13. rb.AddForce(Vector3.up);
  14. }
  15. }

四、TooltipAttribute
在Inspector面板中,为一个字段Field指定一个提示。

image.png

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ExampleClass : MonoBehaviour {
  4. [Tooltip("Health value between 0 and 100.")]
  5. public int health = 0;
  6. }

五、HideInInspectorAttribute

让一个可被序列化的字段,不要显示在Inspector面板中,防止修改。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ExampleClass : MonoBehaviour {
  4. [HideInInspector]
  5. public int p = 5;
  6. }

六、ExecuteInEditMode
让MonoBehaviour脚本的所有实例,在编辑模式下可运行。
这些函数的调用并不会像在PlayMode下那样:

The functions are not called constantly like they are in play mode.

  • Update is only called when something in the scene changed.
  • OnGUI is called when the Game View recieves an Event.
  • OnRenderObject and the other rendering callback functions are called on every repaint of the Scene View or Game View.

只有窗口在发生改变 ,接触新的事件,重绘后才会调用。

*默认MonoBehaviour的脚本只能在运行模式下执行。

  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. public class PrintAwake : MonoBehaviour
  4. {
  5. void Awake()
  6. {
  7. Debug.Log("Editor causes this Awake");
  8. }
  9. void Update()
  10. {
  11. Debug.Log("Editor causes this Update");
  12. }
  13. }

七、DisallowMultipleComponent

禁止一个组件被重复的添加多次。

*如果当前GameObject已经存在了多个相同的组件,是不会有影响的,但应用了特性以后,就无法再次添加。

  1. [DisallowMultipleComponent]
  2. public class testEdit : MonoBehaviour {...}

八、DelayedAttribute

在运行时,我们修改Inspector面板中的字段,会即时返回新的值,应用Delayed特性,只有在用户按下回车Enter或是
焦点离开时才会返回新值,通过用于调试阶段。

*只能应用于字段,不可在类或其它目标元素上使用。

九、SpaceAttribute
间隔距离,在Inspector中,可以设置元素与元素之间的间隔。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class testEdit : MonoBehaviour {
  5. public int a = 10;
  6. [Space(50)]
  7. public int b = 11;
  8. }

image.png

十、TextAreaAttribute
可以在一个高自由度并且可滚动的文本区域里编辑string字符串,如果字符串比较长,比较适用。

参数:
minLines:文本区域最小行数
maxLines:文本区域最大行数,超过最大行数,会出现滚动条。

  1. [TextArea(1,5)]
  2. public string abc;

十一、MultilineAttribute
在一个支持多行的文本区域内编辑string字符串,他和TextAreaAttribute不同,MultilineAttribute的TextArea没有滚动条。

  1. [Multiline(1,5)]
  2. public string abc;

十二、ContextMenu
向Inspector面板中脚本Script的上下文菜单(快捷,右键),添加一条指令,当选择该命令时,函数会执行。
*只能用于非静态函数

  1. public class ContextTesting : MonoBehaviour {
  2. /// Add a context menu named "Do Something" in the inspector
  3. /// of the attached script.
  4. [ContextMenu ("Do Something")]
  5. void DoSomething () {
  6. Debug.Log ("Perform operation");
  7. }
  8. }

image.png

预定义的一些方法,如Reset,是可以进行重载的。

十三、ContextMenuItemAttribute
在Inspector面板中,为字段field添加一个快捷的菜单。

  1. [Multiline][ContextMenuItem("Reset", "ResetString")]
  2. public string abc;
  3. public void ResetString()
  4. {
  5. abc = "";
  6. }

image.png

十四、CreateAssetMenuAttribute

快速的创建ScriptableObject派生类的实例,并存储成以“.asset"结尾的文件,ScriptableObject的派生类可以存储为外部的文件,图形化编辑对象数据,一些静态的数据,动态的加载,ScriptableObject是一种解决方案,具体见另一篇文章的说明:
https://www.jianshu.com/p/da578e55ca47

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [CreateAssetMenu(fileName = "xxxx",menuName = "xxx/xxx")]
  5. public class testEdit : ScriptableObject {
  6. public int a = 10;
  7. public int b = 11;
  8. public int c = 12;
  9. [Multiline][ContextMenuItem("Reset", "ResetString")]
  10. public string abc;
  11. }

说明:
fileName:生成asset文件的文件名。
menuName:在Assets/Create上子菜单的名字。

十五、ColorUsageAttribute

颜色选择器,color picker,只能应用在Color字段上。
默认参数为是否显示alpha,具体使用看下官方文档的参数描述,这里不加代码了

image.png

十六、AddComponentMenu
把添加Script的操作放在Component菜单下,来替代Component/Scripts,因为里面的脚本可能非常多,基本上没有实用价值
AddComponentMenu的方便之处在于如果你当前场景内,有多个GameObjects需要添加同一个脚本,那么使用同时选中
这些地象,并打开Component菜单选中要添加的脚本就可以一次性添加了。

  1. [AddComponentMenu("Example/testEdits")]
  2. public class testEdit : MonoBehaviour {

十七、AssemblyIsEditorAssembly

添加该特性的任意类,都会被视为Editor编辑器类。只有用于Editor模式下。

十八、PreferBinarySerialization
只能用在ScriptableObject的派生类上(使用在其它类型上面会被忽略),修改序列化的模式为二进制,而不是YAML, 当你的资源比较大的时候,保存成二进制,生成的数据会更加的紧凑,从而提高程序的读写性能。

  1. [CreateAssetMenu]
  2. [PreferBinarySerialization]
  3. public class testEdit : ScriptableObject {
  4. public Color a;
  5. public int b = 11;
  6. public int c = 12;
  7. [Multiline][ContextMenuItem("Reset", "ResetString")]
  8. public string abc;
  9. }

用记事本打开生成后的asset,会发现都是二进制的数据。

十九、RuntimeInitializeOnLoadMethodAttribute
在运行时,当前类初始化完成,自动调用被该特性应用的静态函数,这和static静态构造函数还不一样,static静态构造函数是在所有的方法之前运行的,而RuntimeInitializeOnLoadMethod特性的方法是Awake方法之后执行的(如果是MonoBehaviour派生类)。

如果一个类中有多个静态方法使用了RuntimeInitializeOnLoadMethod特性,执行顺序是不固定的。

  1. [RuntimeInitializeOnLoadMethod]
  2. static void OnRuntimeMethodLoad()
  3. {
  4. Debug.Log("After scene is loaded and game is running");
  5. }
  6. [RuntimeInitializeOnLoadMethod]
  7. static void OnSecondRuntimeMethodLoad()
  8. {
  9. Debug.Log("SecondMethod After scene is loaded and game is running.");
  10. }

二十、SelectionBaseAttribute
设置基础选中对象,应用该标识一个对象为选中对象,当我们在scene view中选择一个objects的时候,u3d会返回给我们是适合的对象,比如你选中的对象是prefab的一部分,默认会返回节点的根对象,默认根对象被设置成了基础选中对象,你可以修改他,让其它的对象成为基础选中对象,比如根对象可能就是一个空的GameObject,而我们要实际查看编辑的对象是子节点,这样我们可以将子节点中添加的脚本应用SelectionBase特性。

image.png

image.png

我将脚本加到Camera_Offset后,成为了默认的选中对象,这样每次我在场景中选中时,Camera_Offset会被选择,并高亮显示。

二十一、SerializeField
强制去序列化一个私有的字段field.默认情况下,当u3d在序列化脚本的时候,只会序列化public的字段,这是u3d内部的实现的序列化,并不是 .NET's serialization的实现。
另一点,私有字段,你不希望派生类访问,但你希望在Inspector中可以进行配置,也可以应用SerializeField来解决。

  1. using UnityEngine;
  2. public class SomePerson : MonoBehaviour
  3. {
  4. //This field gets serialized because it is public.
  5. public string name = "John";
  6. //This field does not get serialized because it is private.
  7. private int age = 40;
  8. //This field gets serialized even though it is private
  9. //because it has the SerializeField attribute applied.
  10. [SerializeField]
  11. private bool hasHealthPotion = true;
  12. void Update()
  13. {
  14. }
  15. }

二十二、SharedBetweenAnimatorsAttribute
状态机组件,没有在项目中使用过,mark下,在简书里新建一篇叫StateMachineBehaviour的文章,实际测试案例后,回来补充。

二十三、HeaderAttribute
在Inspector面板中,为field字段添加头信息,增强描述。
The header is done using a DecoratorDrawer.

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ExampleClass : MonoBehaviour {
  4. [Header("Health Settings")]
  5. public int health = 0;
  6. public int maxHealth = 100;
  7. [Header("Shield Settings")]
  8. public int shield = 0;
  9. public int maxShield = 0;
  10. }

二十四、 GUITargetAttribute
设置OnGUI方法在哪一个Display下显示,默认是所有的Display均显示.

  1. [GUITarget(0,1,new int[]{2,3,4})]
  2. void OnGUI()
  3. {
  4. if (GUI.Button (new Rect (0, 0, 128, 128), "Test")) {
  5. Debug.Log ("blahblahblah....");
  6. }
  7. }

image.png

说明:
提供了如下参数:
displayIndex Display index.display 索引
displayIndex1 Display index. display索引
displayIndexList Display index list.display索引列表

 

 

 

 



作者:地坛公园
链接:https://www.jianshu.com/p/70f6e0d8bbf8

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/786399
推荐阅读
相关标签
  

闽ICP备14008679号