一:Obsolete 被该特性标注的方法,在使用过程中会被提示已经弃用,给调用者一个提示。在大型项目中普遍用于新方法代替旧方法
[Obsolete("该方法已经过时了,请用某某某方法代替。",false)]//参数 1 string:在调用时显示提示信息。参数2 bool :true 表示该方法不可被调用,false表示该方法依然可以调用
public void MethodOld()
{
}
二:Conditional : 取消某方法的所有调用,我们可以在类的最上方定义一个宏,通过注释宏来快速取消或者重新调用某方法,多用于代码的测试。
// define后边的 call 应当与 特性 Conditional("call") 后的字符串一致
//#define call //当注释掉该宏时 所有的MyMethod 不会被调用
#define Haha //当该宏没有被注释时 所有的MyMethod1 被调用
using System.Diagnostics;
using UnityEngine;
class MyType:MonoBehaviour
{
void Start()
{
MyMethod();
MyMethod1();
}
[Conditional("call")]
public void MyMethod()
{
UnityEngine.Debug.Log("我被标注为call 我被调用了");
}
[Conditional("Haha")]
public void MyMethod1()
{
UnityEngine.Debug.Log("我被标注为Haha 我被调用了");
}
}
三:CallerFilePath,CallerLineNumber,CallerMemberName 调用者信息特性,返回调用该方法的文件、所在行数、以及方法名称,我们在vs中,直接选中方法,点击Shift + F12 vs会列出所有调用过该方法的位置,以上特性并无卵用,不过还是记录一下
public void PrintOut(string myParameter,[CallerFilePath]string fileName = "",[CallerLineNumber]int lineNumb=0,[CallerMemberName]string methodName="")
{
UnityEngine.Debug.Log("调用者的文件名称(脚本名称):"+fileName);
UnityEngine.Debug.Log("在哪一行调用的:" + lineNumb);
UnityEngine.Debug.Log("在哪个方法调用的" + methodName);
}
四:DebuggerStepThrough 在代码 打断点 调试 过程中,不进入该方法,一般加在不可能有错误的方法中,方便调试
[DebuggerStepThrough]
public void PrintOut(string myParameter,[CallerFilePath]string fileName = "",[CallerLineNumber]int lineNumb=0,[CallerMemberName]string methodName="")
{
UnityEngine.Debug.Log("调用者的文件名称(脚本名称):"+fileName);
UnityEngine.Debug.Log("在哪一行调用的:" + lineNumb);
UnityEngine.Debug.Log("在哪个方法调用的" + methodName);
}
五:Serializable 标记某类可序列化,序列化与反序列化一般在本地储存、网络传输的时候才会使用,当我们想把处于内存中的对象保存在本地或者网络传输的时候,需要先将其序列化,在目的地或者重新使用的时候,再将其反序列化
Nonserialized 不允许序列化,在被标注为Serializable序列化的类中,某字段前加Nonserialized,表示该字段不允许序列化。该博客介绍了序列化与反序列化的详细内容
using System;
using System.Collections.Generic;
using UnityEngine;
public class LearningType : MonoBehaviour
{
public Data mydate;
void Start()
{
}
}
[Serializable]//该类可以被序列化
public class Data
{
[NonSerialized] //该字段不允许序列化
public List<string> names;
public List<int> ages;//该字段被序列化
}
六:创建自定义的特性
using System;
using UnityEngine;
//Age=100 通过指定属性的名字给属性赋值,这种是命名参数
[MyPropertyClass("一些字符串", Age = 100)]
public class LearningType : MonoBehaviour
{
void Start()
{
//---------------------------------------获取特性类中的内容 特性类的使用
Type type = typeof(LearningType);
//获取所有的应用在LearningType上的特性类 参数bool 表示是否搜索继承类上使用了什么特性
object[] array = type.GetCustomAttributes(false);
//我们的 LearningType 上只使用了一个特性MyPropertyClass 所以array数组只有一个值
MyPropertyClassAttribute myPropertyClassAttribute = array[0] as MyPropertyClassAttribute;
//输出我们获取到的数据,
Debug.Log(myPropertyClassAttribute.Age);
Debug.Log(myPropertyClassAttribute.Info);
//然后我们可以对获取到的值进行处理
}
}
//-----------------------------------------------定义一个特性
/// <summary>
/// 特性类名称的后缀以Attribute结尾,当我们使用该特性的时候,会自动省略Attribute
/// 特性类需要继承 System.Attribute
/// 特性类声明为密封类,禁止被继承
/// </summary>
[AttributeUsage(AttributeTargets.Class)]//表示该特性类可以应用哪些程序结构
public sealed class MyPropertyClassAttribute : System.Attribute
{
public string Info { get; set; }
public int Age { get; set; }
public MyPropertyClassAttribute(string info)
{
Info = info;
}
}