lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  201 随笔 :: 0 文章 :: 12 评论 :: 21万 阅读

参考链接:

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 }
复制代码

 

效果如下:

posted on   艰苦奋斗中  阅读(1033)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示