视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Unity注释小技巧
2020-11-09 16:03:46 责编:小采
文档


你只需要在字段上添加特效即可 [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 访问密码 57

下载本文
显示全文
专题