最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Unity注释小技巧

来源:懂视网 责编:小采 时间:2020-11-09 16:03:46
文档

Unity注释小技巧

Unity注释小技巧:你只需要在字段上添加特效即可 [Header( 注释 )][Space( 20 )] public Vector3 test1; ===================================================================================== 自己抽时间写了一个中文显示小脚本.
推荐度:
导读Unity注释小技巧:你只需要在字段上添加特效即可 [Header( 注释 )][Space( 20 )] public Vector3 test1; ===================================================================================== 自己抽时间写了一个中文显示小脚本.

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

文档

Unity注释小技巧

Unity注释小技巧:你只需要在字段上添加特效即可 [Header( 注释 )][Space( 20 )] public Vector3 test1; ===================================================================================== 自己抽时间写了一个中文显示小脚本.
推荐度:
标签: 添加 小技巧 技巧
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top