当前位置:Gxlcms > mysql > Unity注释小技巧

Unity注释小技巧

时间:2021-07-01 10:21:17 帮助过:56人阅读

你只需要在字段上添加特效即可 [Header( " 注释 " )][Space( 20 )] public Vector3 test1; ===================================================================================== 自己抽时间写了一个中文显示小脚本.下次问问老大能不能加到项目中去 usin

你只需要在字段上添加特效即可


[Header("注释")]
[Space(20)]
public Vector3 test1;

=====================================================================================

自己抽时间写了一个中文显示小脚本.下次问问老大能不能加到项目中去

using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.Reflection;
using System.Collections.Generic;

[CustomEditor(typeof(MyCompoment))]
public class MyCompomentEditor : Editor{
    public MyCompomentEditor():base()
    {
        //Debug.Log("我初始化了");
    }


    private static bool isDevelop = true;

    public override void OnInspectorGUI()
    {
        if (isDevelop)
        {
            MyCompoment edit = (MyCompoment)target;
            Type t = edit.GetType();
            string label = string.Empty;
            FieldInfo[] fieldInfs = t.GetFields();
            System.Object[] atrrs = null;
            for (int i = 0; i < fieldInfs.Length; i++)
            {
                atrrs = fieldInfs[i].GetCustomAttributes(false);
                for (int k = 0; k < fieldInfs[i].GetCustomAttributes(false).Length; k++)
                {
                    if (atrrs[k] is LabelAttribute)
                    {

                        label = ((LabelAttribute)atrrs[k]).Label;
                        switch (fieldInfs[i].FieldType.Name)
                        {
                            case "String":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.TextField(label, fieldInfs[i].GetValue(edit).ToString()));
                                break;
                            case "Float":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.FloatField(label, (float)fieldInfs[i].GetValue(edit)));
                                break;
                            //case "Double":
                            //    fieldInfs[i].SetValue(edit, EditorGUILayout.Doube(label, (double)fieldInfs[i].GetValue(edit)));
                            //    break;
                            case "Int":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.IntField(label, (int)fieldInfs[i].GetValue(edit)));
                                break;
                            case "Int32":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.IntField(label, (int)fieldInfs[i].GetValue(edit)));
                                break;
                            case "Color":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.ColorField(label, (UnityEngine.Color)fieldInfs[i].GetValue(edit)));
                                break;
                            case "GameObject":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.ObjectField(label, (UnityEngine.Object)fieldInfs[i].GetValue(edit), typeof(GameObject)));
                                break;
                            case "Component":
                                Debug.Log("运行过Component");
                                fieldInfs[i].SetValue(edit, EditorGUILayout.ObjectField(label, (UnityEngine.Object)fieldInfs[i].GetValue(edit), typeof(Component)));
                                break;
                            case "Vector2":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.Vector2Field(label, (Vector2)fieldInfs[i].GetValue(edit)));
                                break;
                            case "Vector3":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.Vector3Field(label, (Vector3)fieldInfs[i].GetValue(edit)));
                                break;
                            case "Vector4":
                                fieldInfs[i].SetValue(edit, EditorGUILayout.Vector4Field(label, (Vector4)fieldInfs[i].GetValue(edit)));
                                break;
                            //case "Test":
                            //    Debug.Log("运行过Component");
                            //    fieldInfs[i].SetValue(edit, EditorGUILayout.ObjectField(label, (UnityEngine.Object)fieldInfs[i].GetValue(edit), typeof(Component)));
                            //    break;
                            default:

                                //Debug.Log("fieldInfs[i].Name   " + fieldInfs[i].FieldType.BaseType.Name);
                                if (fieldInfs[i].FieldType.BaseType.Name == "MonoBehaviour")
                                {
                                    fieldInfs[i].SetValue(edit, EditorGUILayout.ObjectField(label, (UnityEngine.Object)fieldInfs[i].GetValue(edit), fieldInfs[i].FieldType));

                                }
                                
                                break;
                        }
                    }
                }
            }

        }
        else
        {
            base.OnInspectorGUI();
        } 
    }


    #region 暂时没有用到的代码


    /*
    /// 
    /// 缓存实例的属性,下次就不需要使用循环了
    /// 
    public Dictionary dir;

    public void GetProrptes() 
    {
        if (isDevelop)
        {
            MyCompoment edit = (MyCompoment)target;
            Type t = edit.GetType();
            string label = string.Empty;

            FieldInfo[] fieldInfos = t.GetFields();
            System.Object[] atrrs = null;
            GUIContent contextUI = null;

            for (int i = 0; i < fieldInfos.Length; i++)
            {
                atrrs = fieldInfos[i].GetCustomAttributes(false);
                for (int k = 0; k < atrrs.Length; k++)
                {
                    if (atrrs[k] is LabelAttribute)
                    {
                        label = ((LabelAttribute)atrrs[k]).Label;
                        contextUI = new GUIContent();
                        contextUI.text = label;
                        EditorGUILayout.PropertyField(serializedObject.FindProperty(fieldInfos[i].Name), contextUI);
                    }
                }
            }
        }
        else
        {
            base.OnInspectorGUI();
        }
    }
    */
    #endregion
}

MyCompoment:

using UnityEngine;
using System.Collections;

[SerializeField]
public class MyCompoment : MonoBehaviour {

    [LabelAttribute(Label = "名字")]
    public string MyName = "123";

    [LabelAttribute(Label = "float数字")]
    public float float1 = 100;

    [LabelAttribute(Label = "double数字")]
    public double double1 = 100;

    [LabelAttribute(Label = "int数字")]
    public int int1 = 100;

    [LabelAttribute(Label = "颜色")]
    public Color color1 = Color.red;

    [LabelAttribute(Label = "游戏物体")]
    public GameObject GameObject1;

    [LabelAttribute(Label = "组件")]
    public StartPanel Component1;

    [LabelAttribute(Label = "2D")]
    public Vector2 Vector2;

    [LabelAttribute(Label = "3D")]
    public Vector3 Vector3;

    [LabelAttribute(Label = "4D")]
    public Vector4 Vector4;

}

LabelAttribute特性:

using UnityEngine;
using System.Collections;
using System;

public class LabelAttribute : Attribute {
    public string Label;

}

源代码: http://yunpan.cn/cJhp4tThyGauJ 访问密码 5789

人气教程排行