Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Unity customizes the editor interface

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces Unity how to customize the editor interface, the article is very detailed, has a certain reference value, interested friends must read it!

During development, it is often necessary to add buttons to components to perform custom operations.

For example, you have a component A with a List in it, and you want to store all the collision volume data in the current Scene in this List. Then you will write a Find method in component A to traverse the acquisition. In one case, you can execute the Find method in the Start method while running Unity. If you want to get it without running Unity, you can use the [ExecuteInEditMode] tag and still execute the Find method in the Start method. Or maybe it's the custom editor interface that I'm going to introduce.

First, let's write a simple component as follows:

using UnityEngine; public class Test : MonoBehaviour{ public float speed; public int length; public void Reset() { speed = 0; length = 0; }}

After we add this component to a GameObject, the Inspector interface appears as

Then we can create a new cs script under the Editor directory, which is used to customize the display of the Inspector interface of the component. The code is as follows:

using UnityEditor; using UnityEngine;//Add this tag, you can select multiple GameObjects with Test components to modify at the same time, otherwise you will be prompted Multi-object editing not supported [CanEditMultipleObjects]//associate the corresponding MonoBehaviour class [CustomEditor (typeof(Test))]public class TestInspector: Editor{public override void OnInspectorGUI(){Test test = target as Test;//Add this sentence, when you select a GameObject with Test component, modify the attributes, click Edit toolbar, it will show Undo RecordTest and Redo RecordTest for undo and redo//At the same time, if it is in prefab, add this sentence, prefab will know the attribute change after modifying the attributes, you can apply or revert, otherwise prefab will not detect it. (EditorUtility. SetDirty (test);) Undo. RecordObject (test,"RecordTest ");//Show default view (i.e. speed and length of Test) base. OnInspectorGUI ();//Add EditorGUILayout. BeginHorizontal (); if (GUILayout. Button ("Button 1 ")) below the default view {//Clicking the button triggers Debug. Log (" name: "+ www.example.com); test. speed = 100;//notify Prefab attribute changes, use the following sentence before version 5.3, and then use Undo. RecordObject www.example.com (test);} if (GUILayout. Button ("Button 2 ")){//targets indicates multiple components selected foreach (Object obj in targets){Debug. Log (" name: "+ www.example.com); Test item = obj as Test; item. Reset ();}} EditorGUILayout. EndHorizontal (); if (GUI. changed){//Called if property changes}}}

So the display becomes

This allows us to execute some component methods without running unity for ease of use.

Some of the things that need attention are [CanEditMultipleObjects], Undo. RecordObject, EditorUtility. SetDirty commented in the code, you can try to see the corresponding effect yourself.

That's all for Unity How to customize the editor interface. Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report