网站应如何设计,广西展厅设计公司,开发一个软件需要多少钱?,个人申请公司流程本文只提及常用的特性#xff0c;更多特性请查看官方文档。
AddComponentMenu - Unity 脚本 API 常用特性 AddComponentMenu 添加组件菜单
使用 AddComponentMenu 属性可在“Component”菜单中的任意位置放置脚本#xff0c;而不仅是“Component Scripts”菜单。
使用…本文只提及常用的特性更多特性请查看官方文档。
AddComponentMenu - Unity 脚本 API 常用特性 AddComponentMenu 添加组件菜单
使用 AddComponentMenu 属性可在“Component”菜单中的任意位置放置脚本而不仅是“Component Scripts”菜单。
使用此属性可以更好地组织 Component 菜单从而改进添加脚本时的工作流程。
[AddComponentMenu(Transform/Follow Transform)]
public class FollowTransform : MonoBehaviour
{
} ContextMenu 向上下文菜单添加命令
ContextMenu 属性用于向上下文菜单添加命令。
在该附加脚本的 Inspector 中当用户选择该上下文菜单时 将执行此函数。
这对于从该脚本自动设置场景数据非常有用。 此函数必须是非静态的。
public class AttributeTest : MonoBehaviour
{[ContextMenu(Do Something)]void DoSomething(){Debug.Log(Perform operation);}
} ContextMenuItemAttribute
当点击Reset后会调用ResetDesc方法重置变量的值。即往已经存在的上下文菜单中添加自己的方法。 [ContextMenuItem(Reset, ResetDesc)]public string playerDesc ;public int money 0;void ResetDesc(){playerDesc ;money 0;} CreateAssetMenuAttribute 创建资源菜单
对 ScriptableObject 派生类型进行标记使其自动列在 Assets/Create 子菜单中以便能够轻松创建该类型的实例并将其作为“.asset”文件存储在项目中。
[CreateAssetMenu(fileName ScriptableObjectTest.asset, menuName ScriptableObject/ScriptableObjectTest)]
public class ScriptableObjectTest : ScriptableObject
{public string desc 这是个ScriptableObject;
} HeaderAttribute 添加标题
使用该 PropertyAttribute 在 Inspector 中的某些字段上方添加标题。
标题使用 DecoratorDrawer 完成。 [Header(Health Settings)]public int health 0; HideInInspector 隐藏于面板
使变量不显示在 Inspector 中但进行序列化。public变量默认会显示在Inspector界面中的加上这个标签就不会显示。 InspectorNameAttribute 修改面板显示的名称
对枚举值声明使用此属性可更改 Inspector 中显示的名称。
public class AttributeTest : MonoBehaviour
{public ModelImporterIndexFormat f;
}public enum ModelImporterIndexFormat
{Auto 0,[InspectorName(16 bits)]UInt16 1,[InspectorName(32 bits)]UInt32 2,
} MinAttribute 最小值限制
用于使脚本中的 float 或 int 变量受限于特定最小值的属性。 MultilineAttribute 多行文本
用于string类型可以编辑多行文本。 PropertyAttribute 自定义属性特性
用于派生自定义属性特性的基类。这可用于为脚本变量创建特性。
自定义特性可以与自定义 PropertyDrawer 类连接以控制具有该特性的脚本变量如何在 Inspector 中显示。 RangeAttribute 值的范围限制
用于使脚本中的 float 或 int 变量受限于特定范围的属性。
使用此属性时float 或 int 会在 Inspector 中显示滑动条。 [Range(1, 6)]public int integerRange;[Range(0.2f, 0.8f)]public float floatRange; RequireComponent 所需组件
RequireComponent 属性自动将所需的组件添加为依赖项。
当您将使用RequireComponent的脚本添加到GameObject时所需组件将自动添加到GameObject。这有助于避免设置错误。例如一个脚本可能要求一个刚体总是被添加到同一个GameObject中。
// PlayerScript requires the GameObject to have a Rigidbody component
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{Rigidbody rb;void Start(){rb GetComponentRigidbody();}void FixedUpdate(){rb.AddForce(Vector3.up);}
} SerializeField 序列化私有字段
强制 Unity 对私有字段进行序列化。
当 Unity 对脚本进行序列化时仅对公共字段进行序列化。 如果还需要 Unity 对私有字段进行序列化 可以将 SerializeField 属性添加到这些字段。 Unity 将对所有脚本组件进行序列化重新加载新程序集 并从序列化的版本重新创建脚本组件。此 序列化是通过 Unity 内部序列化系统完成的而不是通过 .NET 的序列化功能来完成。 序列化系统可执行以下操作 可序列化可序列化类型的公共非静态字段 可序列化标记有 SerializeField 属性的非公共非静态字段。 不能序列化静态字段。 不能序列化属性。 可序列化的类型 Unity 可序列化以下类型的字段 继承 UnityEngine.Object 的所有类例如 GameObject、Component、MonoBehaviour、Texture2D、AnimationClip。 所有基本数据类型例如 int、string、float、bool。 某些内置类型例如 Vector2、Vector3、Vector4、Quaternion、Matrix4x4、Color、Rect、LayerMask。 可序列化类型数组 可序列化类型列表 枚举 结构 有关序列化的更多信息请参阅脚本序列化。注意如果在一个列表或数组中将一个元素放置两次当此 列表被序列化时将获得该元素的两个副本而不是获得两次新列表中的一个副本。注意如果要序列化自定义 Struct 字段则必须为该 Struct 给定 [System.Serializable] 属性。提示Unity 不会序列化 Dictionary但您可以为键存储一个 List 和为值存储一个 List然后在 Awake() 上将它们组合在非序列化字典中。这不能解决您需要修改字典 并将其“保存”回时出现的问题但在许多其他情况下这是一个方便的技巧。 [SerializeField]private bool hasHealthPotion true; SpaceAttribute 间距
可在 Inspector 中添加一些间距 public int health 0;[Space(10)] // 10 pixels of spacing here.public int shield 0; TextAreaAttribute 可滚动的区域编辑字符串
属性用于通过高度灵活且可滚动的区域编辑字符串。
您可以指定 TextArea 的最小行数和最大行数该字段将根据文本的大小进行扩展。如果文本大于可用区域则会显示滚动条。 [TextArea]public string MyTextArea; TooltipAttribute 工具提示
为 Inspector 窗口中的字段指定工具提示。 下面的脚本中添加了一个 health。它向用户提供有关 health 变量的值的范围信息。建议的范围在 TooltipAttribute 字符串中提供。 [Tooltip(Health value between 0 and 100.)]public int health 0; 自定义特性PropertyAttribute与 PropertyDrawer