01:利用反射调用特性
public class Texing1 : MonoBehaviour
{
[MyHeader("这个是特性")]
public string Name;
void Start()
{
Type type=this.GetType();
object[] o = type.GetField("Name").GetCustomAttributes(false);
MyHeaderAttribute myHeaderAttribute=o[0] as MyHeaderAttribute;
Debug.Log(myHeaderAttribute.Header);
}
}
public class MyHeaderAttribute : Attribute
{
public string Header;
public MyHeaderAttribute(string Header)
{
this.Header = Header;
}
}
02:
using UnityEngine;
public class Texing2 : MonoBehaviour
{
[ContextMenuItem("安下","Test")]
[Space(2)]
public string Name;
[Tooltip("这个是HP提示")]
[Space(10)]
public float Hp;
[Range(1,2)]
[Space(20)]
public float value;
//私有的可以显示出来
[SerializeField]
private int attack;
//可以滑动
[TextArea(0,3)]
public string kk;
public void Test()
{
UnityEngine.Debug.Log("输出了名字");
}
[ContextMenu("调用")]
public void Test1()
{
UnityEngine.Debug.Log("调用了特性");
}
}
03
using Unity.Collections;
using UnityEngine;
//自定义添加组件
[AddComponentMenu("我的组件")]
//禁止添加同一个组件
[DisallowMultipleComponent]
//此组件必须要添加其他的组件
[RequireComponent(typeof(Camera))]
public class Texing3 : MonoBehaviour
{
}
C#特性
using System;
using UnityEngine;
/// <summary>
/// 多种类型的枚举
/// </summary>
[Flags]
public enum NumberType
{
One,
Two,
Three
}
public class Texing4 : MonoBehaviour
{
[Obsolete("过时的")]
public string Name;
public NumberType Type = NumberType.One|NumberType.Two;
[Header("血量")]
public float HP;
[Inject]
public void Sum()
{
}
}
/// <summary>
/// AttributeTarget 特性的限制范围
/// Inherited 是否容许重复
/// AllowMultiple 派生类是否可以使用特性
/// </summary>
[AttributeUsage(AttributeTargets.Field|AttributeTargets.Enum,Inherited = true,AllowMultiple =true)]
public class InjectAttribute : Attribute
{
}
[MyTest("哈哈")]
class Program
{
static void Main(string[] args)
{
Type type = typeof(Program);
MyTestAttribute myTestAttribute=(MyTestAttribute)type.GetCustomAttributes(false)[0];
WriteLine(myTestAttribute.Name);
ReadKey();
}
}
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false,Inherited = false)]
sealed class MyTestAttribute : Attribute
{
public string Name;
public MyTestAttribute(string Name)
{
this.Name = Name;
}
}
using System;
using System.Reflection;
using static System.Console;
namespace Attar2
{
class Program
{
static void Main(string[] args)
{
WriteLine(PropertiesUtils.GetDescByProperties(Properties.PhyAtk));
ReadKey();
}
}
public enum Properties
{
[PropertiesDesc("血量")] HP = 1,
[PropertiesDesc("物理攻击")] PhyAtk = 2,
[PropertiesDesc("物理防御")] PhyDef = 3,
[PropertiesDesc("法术攻击")] MagAtk = 4,
[PropertiesDesc("法术防御")] MagDef = 5,
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
public class PropertiesDesc : Attribute
{
public string Desc { get; set; }
public PropertiesDesc(string Desc)
{
this.Desc = Desc;
}
}
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;
}
}
}