Unity 注释小技巧

WBOY
發布: 2016-06-07 14:59:38
原創
2063 人瀏覽過

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

Unity 注释小技巧

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


[Header(<span>"</span><span>注释</span><span>"</span><span>)]
[Space(</span><span>20</span><span>)]
</span><span>public</span> Vector3 test1;
登入後複製

 

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

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

Unity 注释小技巧

 

<span>using</span><span> UnityEngine;
</span><span>using</span><span> System.Collections;
</span><span>using</span><span> UnityEditor;
</span><span>using</span><span> System;
</span><span>using</span><span> System.Reflection;
</span><span>using</span><span> System.Collections.Generic;

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


    </span><span>private</span> <span>static</span> <span>bool</span> isDevelop = <span>true</span><span>;

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

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

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

                                }
                                
                                </span><span>break</span><span>;
                        }
                    }
                }
            }

        }
        </span><span>else</span><span>
        {
            </span><span>base</span><span>.OnInspectorGUI();
        } 
    }


    </span><span>#region</span> 暂时没有用到的代码


    <span>/*</span><span>
    /// <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 <span>*/</span>
    <span>#endregion</span><span>
}</span></string></span>
登入後複製

MyCompoment:

<span>using</span><span> UnityEngine;
</span><span>using</span><span> System.Collections;

[SerializeField]
</span><span>public</span> <span>class</span><span> MyCompoment : MonoBehaviour {

    [LabelAttribute(Label </span>= <span>"</span><span>名字</span><span>"</span><span>)]
    </span><span>public</span> <span>string</span> MyName = <span>"</span><span>123</span><span>"</span><span>;

    [LabelAttribute(Label </span>= <span>"</span><span>float数字</span><span>"</span><span>)]
    </span><span>public</span> <span>float</span> float1 = <span>100</span><span>;

    [LabelAttribute(Label </span>= <span>"</span><span>double数字</span><span>"</span><span>)]
    </span><span>public</span> <span>double</span> double1 = <span>100</span><span>;

    [LabelAttribute(Label </span>= <span>"</span><span>int数字</span><span>"</span><span>)]
    </span><span>public</span> <span>int</span> int1 = <span>100</span><span>;

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

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

    [LabelAttribute(Label </span>= <span>"</span><span>组件</span><span>"</span><span>)]
    </span><span>public</span><span> StartPanel Component1;

    [LabelAttribute(Label </span>= <span>"</span><span>2D</span><span>"</span><span>)]
    </span><span>public</span><span> Vector2 Vector2;

    [LabelAttribute(Label </span>= <span>"</span><span>3D</span><span>"</span><span>)]
    </span><span>public</span><span> Vector3 Vector3;

    [LabelAttribute(Label </span>= <span>"</span><span>4D</span><span>"</span><span>)]
    </span><span>public</span><span> Vector4 Vector4;

}</span>
登入後複製

LabelAttribute特性:

<span>using</span><span> UnityEngine;
</span><span>using</span><span> System.Collections;
</span><span>using</span><span> System;

</span><span>public</span> <span>class</span><span> LabelAttribute : Attribute {
    </span><span>public</span> <span>string</span><span> Label;

}</span>
登入後複製

 

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

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板