码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 注释小技巧

时间:2015-03-09 00:18:43      阅读:1614      评论:0      收藏:0      [点我收藏+]

标签:

如果扩展Editor可以让属性在Inspector视图中显示中文名称. 来看看官方有标签特性,能让你的字段头顶显示中文

技术分享

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


[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 暂时没有用到的代码


    /*
    /// <summary>
    /// 缓存实例的属性,下次就不需要使用循环了
    /// </summary>
    public Dictionary<string, string> 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

Unity 注释小技巧

标签:

原文地址:http://www.cnblogs.com/plateFace/p/4322485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!