1. 定义自己的Attribute。
在使用Unity开发的时候。时常会用到 [Serializable]
特性来序列化处理一个类。然后想着自己也定义一个 Attribute
来管理自己代码。
可以看到[Serializable]
代码如下:
using System.Runtime.InteropServices;
namespace System
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate, Inherited = false, AllowMultiple = false)]
[ComVisible(true)]
public sealed class SerializableAttribute : Attribute
{
public SerializableAttribute();
}
}
在提到
AttributeUsage
的时候顺便给一下其他设定好的一些特性。
Conditional
:这个预定义特性标记了一个条件方法,其执行依赖于指定的预处理标识符。类似于(脚本案例在下面)Obsolete
: 感觉这个见到的应该是最多的。通常Unity升级之后。在调用老的代码,会提示xxx方法已经过时,请使用xxx
。- 更多基础信息:https://www.runoob.com/csharp/csharp-attribute.html
Conditional类似于下面这种方法。只不过好像Conditional
是编译之后才会有效。
#define Debug
...
using System;
...
# if Debug
public class Test
{
//....
}
#endif
话题扯回来吧,咱还是弄咱的
Attribute
吧。
emmm… 我感觉好像也不用说了。上面贴了
[Serializable]
代码已经告诉了咱使用方式了。我就贴一个自己的案例代码吧。边看边说比较快。
//首先定义一个名文"Version"的属性。后面的“Attribute”是语法要求。必须加
//定义的时候,里面需要设置一些属性。百度一下很多的解释
[AttributeUsage(AttributeTargets.All | AttributeTargets.Struct, Inherited = false)]
public class VersionAttribute : Attribute
{
public string Author; //作者名字
public string Description; //描述
public string VersionCode; //版本号
public VersionAttribute(string author, string description, string versioncode)
{
this.Author = author;
this.Description = description;
this.VersionCode = versioncode;
}
public VersionAttribute(string description, string versioncode) : this("NoName", description, versioncode) { }
//这个方法呢。就厉害了。反正作为技术渣渣的我,一直都还没有弄过反射。大概项目中没有用到。
//调用这个方法,就可以查看所在程序集[assembly]下,所有使用了咱"Version"的类等等,和对应字段设置的属性值。
public static void GetVersionInfo(string assemblyName)
{
Assembly assebly = Assembly.Load(assemblyName);
Type[] types = assebly.GetTypes();
foreach (Type t_type in types)
{
if (!t_type.IsClass)
continue;
var attributes = t_type.GetCustomAttributes(typeof(VersionAttribute), false);
foreach (var attribute in attributes)
{
VersionAttribute vAttribute = (VersionAttribute)attribute;
UnityEngine.Debug.Log("类名:" + t_type.ToString());
UnityEngine.Debug.Log("作者:" + vAttribute.Author);
UnityEngine.Debug.Log("描述:" + vAttribute.Description);
UnityEngine.Debug.Log("版本:" + vAttribute.VersionCode);
}
}
}
}
//下面两个类就是使用的方法了
[Version("Edwin","C#特性测试","v1.0.0")]
public class UnityAttribute
{
[Conditional("DEBUG")]
public static void Message(string msg)
{
UnityEngine.Debug.Log("Attribute : " + msg);
}
}
[Version("李本心明", "C#特性测试", "v2.0.0")]
public class UnityAttribute2
{
[Conditional("DEBUG")]
public static void Message(string msg)
{
UnityEngine.Debug.Log("Attribute : " + msg);
}
}
下来咱就使用以下
GetVersionInfo
这个方法。看一下都哪些人偷偷的用了咱的Version
特性。
//打印一下,目前代码所在的 `Assembly`程序集。 发现是 “Assembly-CSharp”
Debug.Log("Assembly : " + System.Reflection.Assembly.GetExecutingAssembly().GetName());
//然后调用下面方法。查看谁都在用咱的`Version`特性。
VersionAttribute.GetVersionInfo("Assembly-CSharp");
打印效果如下
Very Beautiful !!
我是李本心明
首先谢谢大家的支持,其次如果你碰到什么其他问题的话,欢迎来 我自己的一个 讨论群559666429
来(扫扫下面二维码或者点击群链接 Unity3D[ 交流] ),大家一起找答案,共同进步。
由于工作生活太忙了,对于大家的帮助时间已经没有之前那么充裕了。如果有志同道合的朋友,可以接受无偿的帮助别人,可以加我QQ单独联系我,一块经营一下。