日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity中使用Attribute

發(fā)布時(shí)間:2024/4/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity中使用Attribute 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Attribute是c#的語言特性

msdn說明如下:

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.

簡單翻譯如下:

Attribute類可以把目標(biāo)元素和一個(gè)預(yù)定義的信息或者是用戶自定義信息關(guān)聯(lián)起來。這里的目標(biāo)元素可以是assembly,class,constructor,delegate,enum,event,field,interface,method,可執(zhí)行文件模塊,parameter,property,return value,struct或其它的Attribute。
Attribute提供的信息也被稱為元數(shù)據(jù)(metadata)。元數(shù)據(jù)能用于在運(yùn)行時(shí)控制怎樣訪問你的程序數(shù)據(jù),或者在運(yùn)行前通過額外的工具來控制怎樣處理你的程序或部署它。例如.NET Framework預(yù)定義并使用attribute去控制運(yùn)行時(shí)行為,一些編程語言使用attribute類型來描述.NET Framework中通用類型不直接支持的語言特性。
所有的Attribute類型直接或間接從Attribute類繼承。Attribute能應(yīng)用到任何target元素;多個(gè)Attribute能應(yīng)用到相同的元素;
Attribute類提供遍歷的方法去取出和測試自定義Attribute。更多關(guān)于Attribute的信息,可以看Applying Attributes和Extending Metadata Using Attributes。

unity中的預(yù)定義attribute

看了上述說明,我們可能還不是很清楚attribute到底怎么用,我們先來看看Unity中常用的預(yù)定義attribute是怎么使用的,然后再學(xué)習(xí)怎么使用我們自己自定義的attribute。

DllImport

DllImport應(yīng)用于方法,用于告訴運(yùn)行時(shí)該方法位于指定的dll的非托管代碼中(如c/c++,oc等),如果dll是托管代碼生成的(如c#代碼生成的dll),則不需要應(yīng)用此特性。例如,當(dāng)我們需要調(diào)用iOS中的瀏覽器打開一個(gè)url時(shí),可以先編寫oc的方法,然后放在項(xiàng)目的plugin目錄中,并且在c#腳本中應(yīng)用DllImport就可以調(diào)用了。

oc代碼:

NSString* CreateNSString (const char* string) {if (string)return [NSString stringWithUTF8String: string];elsereturn [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);

其他unity預(yù)定義attribute的使用可以參考http://blog.sina.com.cn/s/blog_5b6cb9500101857b.html

使用自定義的attribute

自定義的attribute需要繼承System.Attribute,然后我們可以使用c#的預(yù)定義特性去注明此attribute的應(yīng)用范圍。如果我們自定義的attribute名為UI,那么我們的自定義的attribute類名則應(yīng)該為UIAttribute。

using System;

public enum UILayer
{
??? Default,
??? Secondary,
??? Popup,
??? TipAndWraning
}


[AttributeUsage(AttributeTargets.Class)]
public class UIAttribute : Attribute
{
??
public UILayer Layer { get; set; }
}

如上所示,我們定義了一個(gè)attribute,UI,并且指明了應(yīng)用范圍只能在類上應(yīng)用。我們用這個(gè)attribute來注明不同的View類的Layer屬性,使用代碼如下:

[UI(Layer = UILayer.Default)] public class LoginView : View {// } [UI(Layer = UILayer.Popup)] public class AlertInfoView : View {// }

當(dāng)我們創(chuàng)建不同View類的實(shí)例時(shí),可以從attribute中獲取到Layer信息:

public View CreateView(System.Type type){UIAttribute classAttribute = (UIAttribute)System.Attribute.GetCustomAttribute(type, typeof(UIAttribute));View view = System.Activator.CreateInstance(type) as View;view.Layer = classAttribute.Layer;return view;}public void Test() {View login = CreateView(typeof(LoginView));View alert = CreateView(typeof(AlertInfoView)); }

轉(zhuǎn)載于:https://www.cnblogs.com/alan777/p/6139802.html

總結(jié)

以上是生活随笔為你收集整理的Unity中使用Attribute的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。