Unity 注释小技巧

如果扩展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

时间: 2024-08-17 11:35:25

Unity 注释小技巧的相关文章

Unity 游戏开发技巧集锦之使用cookie类型的纹理模拟云层的移动

Unity 游戏开发技巧集锦之使用cookie类型的纹理模拟云层的移动 使用cookie类型的纹理模拟云层的移动 现实生活中,当阳光直射大地,而天空中又有很多云时,云层的影子总是会投射在大地上,风吹着云层移动,影子也跟着运动,如图3-28所示. 图3-28  天空中的云朵与大地上的影子 要在游戏中,模拟与之类似的大气现象时,就需要使用cookie类型的纹理. 制作云层效果的纹理 本小节将使用PhotoShop绘制有云层效果的纹理图,然后为其添加透明度信息.具体操作过程如下: (1)使用Photo

QTP使用小技巧

一.添加固定注释 新建一TXT文档,将要添加的注释写在文档中 将文档名改为:ActionTemplate.mst 将文件放到QTP安装目录的dat文件夹中 设置好后,在QTP中每次新建一个测试就会自动添加固定的注释 二.调用外部vbs文件方法 1.将通用函数写在一个vbs文件中,以供其他脚本调用 2.调用外部VBS文件中的通用函数的方法(二选一即可),设置完后在QTP中直接使用函数名进行调用: 1)通过在QTP中设置:file-->settings-->Resource-->“添加VBS

Unity3D使用小技巧

原地址:http://unity3d.9tech.cn/news/2014/0411/40178.html 1.Crtl+f摄像机自动适配场景. 2.可以用一个立方体作为底盘. 3.人物角色可以直接引入包,有第一人称和第三人称,已经封装好. 4.光源可以设置投影. 5.3DMax是Z轴向上,而一般游戏引擎是Y轴向上,所以导出的时候一定要注意选择YZ转化的格式(YZ转化选项一定要勾选). 另外,obj格式不支持动画,其实游戏中最常用的格式是"FBX"和"DAE"格式,

NuGet 问题及小技巧

在使用NuGet程序包管理时,经常会遇到的一些小问题.比如,联网搜索时间太长,搜索不到常见或想要的程序包,程序包版本兼容问题,想安装制定的版本等. 那么我们可以使用以下的一些小技巧来解决. 1.检查NuGet程序包来源设置(是否勾选https://www.nuget.org/api/v2/): 2.搜索你想要的程序包及版本,可以在nuget官网中搜索, http://www.nuget.org/packages/<程序包名>                         例如http://w

最强 Android Studio 使用小技巧和快捷键【非原创】

(发现本文是个很不错的文章,相当实用,特分享与大家.分享自:http://m.open-open.com/m/lib/view/1458715872710.html 特此声明,好记性不如烂笔头,market下来以备后用) 原文如下: 写在前面 本文翻译自 Android Studio Tips by Philippe Breault,一共收集了62个 Android Studio 使用小技巧和快捷键. 根据这些小技巧的使用场景,本文将这62个小技巧分为常用技巧(1 – 28).编码技巧(29 –

8 个 Git 的小技巧

git 已经成为了我日常必备工具之一,我总结我几乎每天使用的8个有用(且简洁)的git技巧. 使用-p选择性添加 当你想提交内容时,你可以通过使用 git commit -am 来选择所有文件或使用 git add file 来添加特定文件.然而,有时候你可能想只添加文件的一部分来提交.你可以用 git add -p 交互性地选择哪些你想提交的部分. 在选择完你所想要提交的区块后,只需要做一个 git commit(没有 -a),这样只会提交选中的部分.同样可以使用 git checkout -

android 系统定制的小技巧

<转>android 系统定制的小技巧(网络收集) 1开机图片: android-logo-mask.pngandroid-logo-shine.png 这两个图片一个在上一个在下 ./out/target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates/classes/assets/images/android-logo-shine.png./frameworks/base/core/res/assets/image

android 系统定制的小技巧(网络收集)

1开机图片: android-logo-mask.png android-logo-shine.png 这两个图片一个在上一个在下 ./out/target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates/classes/assets/images/android-logo-shine.png ./frameworks/base/core/res/assets/images/android-logo-shine.png

VS 2010 C#入门操作小技巧

*推荐C#入门教学视频(http://www.51xue8.com/e/DownSys/play/?classid=27&id=6719&pathid=3&jishu=17) VS 2010 C#入门操作小技巧 1.认识VS2010:阅读技术文档或者教学视频对入门帮助很大! (1)添加类库 点击解决方案名称(如12.demo)右键->添加->类->更改类名 (2)利用控件设计界面 常用控件:button/label/textbox/combobox: 具体控件用途