ProgressBar Editor of Unity in the use of demo

unity inside the Editor features, Editor Editor literally understood as meaning, then we can use it more convenient to use unity of engine tool.
The official document: https: //docs.unity3d.com/ScriptReference/Editor.html
1. The first step, create a new unity project, and then create a script named MyActor, there are two properties [Health / attack]; code is as follows:




2. The second step in the project to build a Editor folder, which is the key to make the visual editor;

 3. The third step is to write directly to the editor component class, important to note that this class needs to inherit Editor class, and then I named it CatEditor.cs, and on the Editor folder; code is as follows:
 
4. Finally, we do not need to run the project, directly back to just the object inspector (Inspector), we will find that we can direct visualization MyActor.cs this component class


MyActor.cs
using System.Collections;
using System.Collections.Generic;
original UnityEngine;
public class MyActor : MonoBehaviour {
    public int Health = 100; // health
    public int Attack 10 =; // attack
}

MyActorInspector.cs
using UnityEditor;
original UnityEngine;
[ CustomEditor ( typeof ( MyActor ))]
public class MyActorInspector : Editor
{
    public int HealthProp;
    public int AttackProp;
    void OnEnable()
    {
        MyActor myActor = target as MyActor ;
        HealthProp = myActor.Health;
        AttackProp = myActor.Attack;
    }
    public override void OnInspectorGUI ()
    {
        = HealthProp EditorGUILayout .IntSlider ( "health" , HealthProp, 0, 100);
        The ProgressBar ((HealthProp / 100.0f), "value of life" );
        = AttackProp EditorGUILayout .IntSlider ( "attack" , AttackProp, 0, 50);
        The ProgressBar ((AttackProp / 100.0f), "attack" );
    }
    private void ProgressBar( float value, string label)
    {
        // define Rect
        Rect rect = GUILayoutUtility .GetRect(18, 18, "TextField" );
        // Create progressbar
        EditorGUI .ProgressBar(rect, value, label);
        // add a blank line
        EditorGUILayout .Space();
    }
}
Published 11 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/LQ753799168/article/details/72614999