效果图:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEditor; 5 [CustomEditor(typeof(test))] 6 public class Edit_test : Editor 7 { 8 test test_scripts; //脚本本体 9 SerializedObject serObj;//用来获取各脚本变量 10 SerializedProperty int_data; 11 SerializedProperty float_data; 12 SerializedProperty vector3_data; 13 SerializedProperty bool_data; 14 SerializedProperty texture2d_data; 15 16 /// <summary> 17 /// 初始化,绑定各变量 18 /// </summary> 19 private void OnEnable() 20 { 21 test_scripts= (test)target; 22 serObj = new SerializedObject(target); 23 24 int_data = serObj.FindProperty("int_data"); 25 float_data = serObj.FindProperty("float_data"); 26 vector3_data = serObj.FindProperty("vector3_data"); 27 bool_data = serObj.FindProperty("bool_data"); 28 texture2d_data = serObj.FindProperty("texture2d_data"); 29 } 30 31 /// <summary> 32 /// 显示 33 /// </summary> 34 public override void OnInspectorGUI() 35 { 36 serObj.Update(); 37 EditorGUILayout.LabelField("以下是各数据的设置", EditorStyles.miniLabel); 38 EditorGUILayout.Separator(); 39 EditorGUILayout.PropertyField(int_data, new GUIContent("int_data")); 40 EditorGUILayout.Slider(float_data, 0.0f, 50.0f, new GUIContent("float_data"));//滑动条 41 EditorGUILayout.PropertyField(vector3_data, new GUIContent("vector3_data")); 42 EditorGUILayout.PropertyField(bool_data, new GUIContent("bool_data")); 43 EditorGUILayout.PropertyField(texture2d_data, new GUIContent("texture2d_data")); 44 EditorGUILayout.EndFadeGroup(); 45 if (GUILayout.Button("输出信息")) 46 { 47 test_scripts.PrintData(); 48 } 49 serObj.ApplyModifiedProperties(); 50 } 51 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class test : MonoBehaviour 6 { 7 8 public int int_data; 9 public float float_data; 10 public Vector3 vector3_data; 11 public bool bool_data; 12 public Texture2D texture2d_data; 13 14 public void PrintData() 15 { 16 Debug.Log("int_data=" + int_data); 17 Debug.Log("float_data=" + float_data); 18 Debug.Log("Vector3_data=" + vector3_data); 19 Debug.Log("bool_data=" + bool_data); 20 } 21 }