Unity 中常用特性(Attribute)总结(中篇)
此篇为此系列的中篇。包含 System 命名空间下的特性和 UnityEngine 命名空间下的 I - R 部分。
开头不再赘述。直接进入正题。
UnityEngine命名空间下
ImageEffectAfterScale
描述
作用于 Image Effect (效果待验证),使 Image Effect 在动态分辨率阶段之后渲染。
用法
ImageEffectAfterScale
ImageEffectAllowedInSceneView
描述
作用于 Image Effect (效果待验证),使 Image Effect 渲染在 Scene view 相机上
用法
ImageEffectAllowedInSceneView
ImageEffectOpaque
描述
作用于 Image Effect (效待验证果),使 Image Effect 渲染在透明图形前、不透明图像后,使扩展了深度缓冲(depth buffer)如环境光遮蔽(SSAO)等效果的 Image Effect 仅作用于不透明像素;此属性可用于减少具有后期处理(post processing)的场景中的视觉伪像(visual artifacts)数量
用法
ImageEffectOpaque
ImageEffectTransformsToLDR
描述
在使用 HDR 渲染条件下,渲染 Image Effect 切换到 LDR 渲染
用法
ImageEffectTransformsToLDR
ImageEffectUsesCommandBuffer
描述
使用命令缓冲区实现图像效果时使用该属性,Unity会将场景渲染到RenderTexture中,而不是实际的目标中
用法
ImageEffectUsesCommandBuffer
InspectorName
描述
在枚举值声明上使用此属性可更改 Inspector 中显示的显示名称。
用法
InspectorName(string displayName)
可接受的参数
1.string displayName:要显示的名称。
示例代码
public enum Enum
{
[InspectorName("EnumAnotherName1")]
Enum1 = 0,
[InspectorName("EnumAnotherName2")]
Enum2 = 1,
[InspectorName("EnumAnotherName3")]
Enum3 = 2,
}
Min
描述
限制 float 或 int 变量的最小值。
用法
Min(float min)
可接受的参数
1.float min:允许的最小值。
示例代码
[Min(10)]
public int minValue;
Multiline
描述
作用于 string ,使 string 在 Inspector 显示多行文本区
用法
Multiline
或
Multiline(int lines)
可接受的参数
1.int lines:为多少行文本腾出空间。默认值为 3。
NonReorderable
描述
在 Inspector 中禁用数组或列表的重新排序。
默认情况下,数组或列表脚本变量与 UI 控件一起显示,该控件允许通过元素内容左侧的拖动手柄重新排序数组元素。您可以在脚本数组或列表变量上使用 [NonReorderable] 属性来禁用它。禁用重新排序后,Inspector 会显示带有简单 UI 控件的数组或列表,该控件具有数组大小,后跟数组元素。
用法
NonReorderable
示例代码
public class NonReorderableAttributeTest : MonoBehaviour
{
[NonReorderable]
public int[] array;
}
PreferBinarySerialization
描述
作用于 ScriptableObject 派生类,使用二进制序列化取代项目资源序列化,可提升读写效率,提高压缩表现且无法直接识别,但当资源文件包含多个资源时无法使特定资源使用二进制序列化,嵌入场景的组员也会无视二进制序列化
用法
PreferBinarySerialization
示例代码
[CreateAssetMenu]
[PreferBinarySerialization]
public class CustomData : ScriptableObject
{
public float[] lotsOfFloatData = new[] { 1f, 2f, 3f };
public byte[] lotsOfByteData = new byte[] { 4, 5, 6 };
}
Property
描述
自定义特性的基类,用于创建自定义特性派生类
Range
描述
用于将脚本中的 float 或 int 变量限制在特定范围内的属性。
使用此属性时,float 或 int 将在检查器中显示为滑块,而不是默认的数字字段。
用法
Range(float min, float max)
可接受的参数
1.float min:允许的最小值。
2.float max:允许的最大值。
示例代码
public class Example : MonoBehaviour
{
[Range(1, 6)]
public int integerRange;
[Range(0.2f, 0.8f)]
public float floatRange;
}
RequireComponent
描述
作用于类, 在挂载该脚本同时会自动挂载该脚本依赖的组件,且删除时弹出警告
用法
RequireComponent(Type requiredComponent, Type requiredComponent2, Type requiredComponent3)
可以填最少一个,最多三个组件
可接受的参数
1.Type requiredComponent:需要的组件
示例代码
// 要求 GameObject 具有 Rigidbody 部件
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(Vector3.up);
}
}
RPC(已移除)
描述
用于设置 RPC 功能的属性。
RuntimeInitializeOnLoadMethod
描述
作用于静态方法,允许运行时情况下加载游戏后,无需用户行为即可初始化运行时类方法;被该特性标记的方法会在游戏加载完成 Awake 方法执行后执行,但所有被标记方法执行顺序不定
用法
RuntimeInitializeOnLoadMethod
或
RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType loadType)
可接受的参数
1.RuntimeInitializeLoadType loadType:确定是在加载场景之前还是之后调用方法。
示例代码
// 创建一个非MonoBehaviour类显示游戏加载时的消息。
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.");
}
}
另一个示例代码(演示参数的使用)
// Demonstration of RuntimeInitializeOnLoadMethod and the argument it can take.
using UnityEngine;
class MyClass
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void OnBeforeSceneLoadRuntimeMethod()
{
Debug.Log("在第一个场景加载之前");
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void OnAfterSceneLoadRuntimeMethod()
{
Debug.Log("加载第一个场景后");
}
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
Debug.Log("RuntimeMethodLoad: 在第一个场景加载后");
}
}
本文参考的文章
Unity 特性(Attribute)总览
Attributes(Unity 官方文档)
Unity手册—Attribute汇总说明