zoukankan      html  css  js  c++  java
  • [UnityAPI]EditorWindow类 & Editor类

    参考链接:

    https://docs.unity3d.com/ScriptReference/EditorWindow.html

    https://docs.unity3d.com/ScriptReference/Editor.html

    1.EditorWindow

    TestEditorWindow.cs

     1 using UnityEditor;
     2 using UnityEngine;
     3 
     4 public class TestEditorWindow : EditorWindow
     5 {
     6     string s = "hello world";
     7 
     8     [MenuItem("Window/MyWindow")]
     9     private static void Init()
    10     {
    11         TestEditorWindow testEditorWindow = GetWindow<TestEditorWindow>();
    12         testEditorWindow.Show();
    13     }
    14 
    15     private void OnGUI()
    16     {
    17         GUILayout.Label(s);
    18     }
    19 }

    效果如下:

    2.Editor

    MyPlayer.cs

    1 using UnityEngine;
    2 
    3 public class MyPlayer : MonoBehaviour
    4 {
    5     public int damage = 25;
    6     public GameObject gun;
    7 }

    MyPlayerEditor.cs

     1 using UnityEditor;
     2 using UnityEngine;
     3 
     4 [CustomEditor(typeof(MyPlayer))]
     5 [CanEditMultipleObjects]
     6 public class MyPlayerEditor : Editor
     7 {
     8     SerializedProperty damageProp;
     9     SerializedProperty gunProp;
    10 
    11     void OnEnable()
    12     {
    13         damageProp = serializedObject.FindProperty("damage");
    14         gunProp = serializedObject.FindProperty("gun");
    15     }
    16 
    17     public override void OnInspectorGUI()
    18     {
    19         // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
    20         serializedObject.Update();
    21 
    22         // Show the custom GUI controls.
    23         EditorGUILayout.IntSlider(damageProp, 0, 100, new GUIContent("Damage"));
    24         EditorGUILayout.PropertyField(gunProp, new GUIContent("Gun Object"));
    25 
    26         // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
    27         serializedObject.ApplyModifiedProperties();
    28     }
    29 }

    效果如下:

  • 相关阅读:
    memcached源码剖析5:并发模型
    memcached源码剖析4:并发模型
    深入剖析php执行原理(4):函数的调用
    深入剖析php执行原理(2):函数的编译
    剖析php脚本的超时机制
    strerror的坑
    深入理解php中的ini配置(2)
    深入理解php中的ini配置(1)
    一个“日期”字符串进行比较的case
    用valgrind检测php扩展内存泄露
  • 原文地址:https://www.cnblogs.com/lyh916/p/13174413.html
Copyright © 2011-2022 走看看