unity特性

在这里插入图片描述
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;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值