Unity游戏开发——C#特性Attribute与自动化
来源:互联网 发布:mac连打印机 编辑:程序博客网 时间:2024/05/29 18:23
这篇文章主要讲一下C#里面Attribute的使用方法及其可能的应用场景。
比如你把玩家的血量、攻击、防御等属性写到枚举里面。然后界面可能有很多地方要根据这个枚举获取属性的描述文本。
比如你做网络框架的时候,一个协议号对应一个类的处理或者一个方法。
比如你做ORM,一个类的属性是否映射持久化文件中的属性,映射过去的属性名是什么。
1、什么是Attribute
The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.
Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.
All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied.
The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes.
翻译过来就是:2、用Attribute将枚举和一个描述文本绑定在一起
public enum Properties{ /// <summary> /// 血量 /// </summary> HP = 1, /// <summary> /// 物理攻击 /// </summary> PhyAtk = 2, /// <summary> /// 物理防御 /// </summary> PhyDef = 3, /// <summary> /// 法术攻击 /// </summary> MagAtk = 4, /// <summary> /// 法术防御 /// </summary> MagDef = 5}
Properties.MagDef返回“法术防御”。
最原始的做法:
public class PropertiesUtils{ public static string GetDescByProperties(Properties p) { switch (p) { case Properties.HP: return "血量"; case Properties.PhyAtk: return "物理攻击"; case Properties.PhyDef: return "物理防御"; case Properties.MagAtk: return "法术攻击"; case Properties.MagDef: return "法术防御"; default: return "未知属性:" + p; } }}
这样确实可以解决问题,但是我们可以用Attribute来做的更好。可以做的更好干嘛不呢?
先定义一个用于存储描述文本的Attribute。
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Enum)]public class PropertiesDesc : System.Attribute{ public string Desc { get; private set; }}没错,看起来是不是觉得很简单。
public enum Properties{ [PropertiesDesc("血量")] HP = 1, [PropertiesDesc("物理攻击")] PhyAtk = 2, [PropertiesDesc("物理防御")] PhyDef = 3, [PropertiesDesc("法术攻击")] MagAtk = 4, [PropertiesDesc("法术防御")] MagDef = 5}OK。这样,我们相当于就把一个文本描述信息通过Attribute关联到我们的枚举属性了。
public class PropertiesUtils{ public static string GetDescByProperties(Properties p) { Type type = p.GetType(); FieldInfo[] fields = type.GetFields(); foreach (FieldInfo field in fields) { if (field.Name.Equals(p.ToString())) { object[] objs = field.GetCustomAttributes(typeof(PropertiesDesc), true); if (objs != null && objs.Length > 0) { return ((PropertiesDesc)objs[0]).Desc; } else { return p.ToString() + "没有附加PropertiesDesc信息"; } } } return "No Such field : "+p; }}可以看到。这里面已经不用自己去判断哪个枚举值返回哪个字符串描述了。而是获取这个枚举域的PropertiesDesc对象。然后返回它的Desc属性。
public class PropertiesUtils{ private static Dictionary<Type, Dictionary<string, string>> cache = new Dictionary<Type, Dictionary<string, string>>(); public static string GetDescByProperties(object p) { var type = p.GetType(); if (!cache.ContainsKey(type)) { Cache(type); } var fieldNameToDesc = cache[type]; var fieldName = p.ToString(); return fieldNameToDesc.ContainsKey(fieldName) ? fieldNameToDesc[fieldName] : string.Format("Can not found such desc for field `{0}` in type `{1}`", fieldName, type.Name); } private static void Cache(Type type) { var dict = new Dictionary<string, string>(); cache.Add(type, dict); var fields = type.GetFields(); foreach (var field in fields) { var objs = field.GetCustomAttributes(typeof(PropertiesDesc), true); if (objs.Length > 0) { dict.Add(field.Name, ((PropertiesDesc)objs[0]).Desc); } } }}
3、还能干什么?
做过网络框架的同学也应该比较熟悉的一个应用,使用Attribute来做自动的消息派发。
总之,Attribute可以做很多自动化的事情,就看你怎么用了。
- Unity游戏开发——C#特性Attribute与自动化
- 【Unity】C#特性Attribute与自动化
- 王楠——海外华人程序员视角:解密移动游戏开发与Unity 4引擎特性
- unity中的c# Attribute特性的使用记录
- Unity与游戏开发
- C# 特性(Attribute)(一)
- C# 特性(Attribute)(二)
- C#特性(Attribute)
- C#特性(Attribute)
- C# 特性(Attribute)
- C# Attribute特性 (一)
- C# Attribute特性 (二)
- C# Attribute特性 (三)
- C# Attribute 特性 学习
- C#特性Attribute
- C# 中的Attribute 特性
- C#中的attribute特性
- C# attribute特性实例
- 记录常遇到的异常 (NullPointerException,NumberFormatException,ClassCastException...)
- centos6.4搭建apache+mysql+php环境
- storm日志处理坑1
- 2014-12-12-系统退出强杀进程
- sql insert into
- Unity游戏开发——C#特性Attribute与自动化
- GCD索引(Grand Central Dispatch (GCD) Reference )
- Android 自定义RelativeLayout活动主窗口大小改变 得到软键盘的显示和隐藏
- 《老罗的Android之旅》阅读笔记——IPC机制Binder
- Oracle 删除重复数据只留一条
- C语言实现支持泛型的栈
- java动态代理详解
- 整理一篇json介绍
- linux 修改分区之后启动失败以及分区相关命令