Unity中使用自定义Attribute

更新时间:2023-08-03 11:40:39 阅读: 评论:0

Unity中使⽤⾃定义Attribute
Attribute是c#的语⾔特性
msdn说明如下:
The Attribute class associates predefined system information or ur-defined custom information with a target element. A target element can be an asmbly, 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 process data, or before run time by external tools to control how your application itlf is procesd or maintained. For example, the Framework predefines and us attribute types to control run-time behavior, and some programming languages u attribute types to reprent language features not directly supported by the Framework common type system.
All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any ta
rget element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. U 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, e Applying Attributes and Extending Metadata Using Attributes.
简单翻译如下:
Attribute类可以把⽬标元素和⼀个预定义的信息或者是⽤户⾃定义信息关联起来。这⾥的⽬标元素可以是
asmbly,class,constructor,delegate,enum,event,field,interface,method,可执⾏⽂件模
块,parameter,property,return value,struct或其它的Attribute。
Attribute提供的信息也被称为元数据(metadata)。元数据能⽤于在运⾏时控制怎样访问你的程序数据,或者在运⾏前通过额外的⼯具来控制怎样处理你的程序或部署它。例如 Framework预定义并使⽤attribute去控制运⾏时⾏为,⼀些编程语⾔使⽤attribute类型来描述 Framework中通⽤类型不直接⽀持的语⾔特性。
所有的Attribute类型直接或间接从Attribute类继承。Attribute能应⽤到任何target元素;多个Attribute能应⽤到相同的元素;
Attribute类提供遍历的⽅法去取出和测试⾃定义Attribute。更多关于Attribute的信息,可以看Applying Attributes和Extending Metadata Using Attributes。
unity中的预定义attribute
看了上述说明,我们可能还不是很清楚attribute到底怎么⽤,我们先来看看Unity中常⽤的预定义attribute是怎么使⽤的,然后再学习怎么使⽤我们⾃⼰⾃定义的attribute。
DllImport
DllImport应⽤于⽅法,⽤于告诉运⾏时该⽅法位于指定的dll的⾮托管代码中(如c/c++,oc等),如果dll是托管代码⽣成的(如c#代码⽣成的dll),则不需要应⽤此特性。例如,当我们需要调⽤iOS中的浏览器打开⼀个url时,可以先编写oc的⽅法,然后放在项⽬的plugin⽬录中,并且在c#脚本中应⽤DllImport就可以调⽤了。
oc代码:
NSString* CreateNSString (const char* string)
{
if (string)
return [NSString stringWithUTF8String: string];
el
return [NSString stringWithUTF8String: ""];
护士实习日志}
extern "C" {百六
void OpenUrl(const char* urlString)
{
//打开浏览器
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:CreateNSString(urlString)]];
return;
}
}
c#代码:
[DllImport("__Internal")]
public static extern string OpenUrl(string url);
⽤⾃定义Attribute将枚举和⼀个描述⽂本绑定在⼀起
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
//属性绑定信息描述
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Enum|System.AttributeTargets.Struct)] public class EnumPropertiesDesc : System.Attribute
{
string desc;
public EnumPropertiesDesc(string str)
{
desc = str;
}
public string Desc
{
get
青园小区{
return desc;
}
}
珍惜粮食作文
}
public class EnumPropertiesUtils
{
static Dictionary<Type, Dictionary<string, string>> cache = new Dictionary<Type, Dictionary<string, string>>();
public static string GetPropertiesUtils(object o)
{
如何治疗焦虑症
var type = o.GetType();
Debug.Log("PropertiesUtils_type:" + type);
if(!cache.ContainsKey(type))
{
Cache(type);
}
var fieldNameToDesc = cache[type];
var fieldName = o.ToString();
Debug.Log("fieldName:" + fieldName);
Debug.Log("fieldName:" + fieldName);
return fieldNameToDesc.ContainsKey(fieldName) ? fieldNameToDesc[fieldName] : string.Format("Can not found such desc for field `{0}` in type `{1}`", fieldN    }
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(EnumPropertiesDesc), true);
if(objs.Length>0)
滑动摩擦力的方向{
dict.Add(field.Name, ((EnumPropertiesDesc)objs[0]).Desc);
}
}
}
}
public enum EnumTest
{
[EnumPropertiesDesc("枚举A")]
Test1,
[EnumPropertiesDesc("枚举B")]
Test2
}
public class ClassTest:MonoBehaviour
{
EnumTest enumTest;
public void Start()
{
enumTest = EnumTest.Test2;
Debug.Log(string.Format("属性绑定信息测试:{0}",
EnumPropertiesUtils.GetPropertiesUtils(enumTest)));
}
}

本文发布于:2023-08-03 11:40:39,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1127607.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:信息   绑定   程序
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图