Unity中Editor的ProgressBar的使用demo

来源:互联网 发布:刘欢和李宗盛 知乎 编辑:程序博客网 时间:2024/06/09 18:22
unity里面的Editor功能,Editor从字面理解为编辑器的意思,然我们可以利用它更方便的使用unity的引擎工具。
官方文档:https://docs.unity3d.com/ScriptReference/Editor.html
1.第一步,新建一个unity项目,然后新建一个脚本命名为MyActor,有两个属性【生命值/攻击力】;代码如下:




2.第二步,在项目中建一个Editor文件夹,这是做成可视化编辑器的关键;

 3.第三步,直接写编辑器组件类,特别需要注意的是这个类需要继承Editor类,然后我把它命名为CatEditor.cs,并且放在Editor文件夹下; 代码如下:
 
4.最后,我们不需要运行项目,直接回到刚才对象的检视面板(Inspector),我们会发现,我们可以直接可视化操作MyActor.cs这个组件类


MyActor.cs
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassMyActor:MonoBehaviour{
   publicintHealth=100;//生命值
   publicintAttack=10;//攻击力
}

MyActorInspector.cs
usingUnityEditor;
usingUnityEngine;
[CustomEditor(typeof(MyActor))]
publicclassMyActorInspector:Editor
{
   publicintHealthProp;
   publicintAttackProp;
   voidOnEnable()
    {
       MyActormyActor = targetasMyActor;
        HealthProp = myActor.Health;
        AttackProp = myActor.Attack;
    }
   publicoverridevoidOnInspectorGUI()
    {
        HealthProp =EditorGUILayout.IntSlider("生命值", HealthProp, 0, 100);
        ProgressBar((HealthProp / 100.0f),"生命值");
        AttackProp =EditorGUILayout.IntSlider("攻击力", AttackProp, 0, 50);
        ProgressBar((AttackProp / 100.0f),"攻击力");
    }
   privatevoidProgressBar(floatvalue,stringlabel)
    {
       //定义 Rect
       Rectrect =GUILayoutUtility.GetRect(18, 18,"TextField");
       //创建progressbar
       EditorGUI.ProgressBar(rect, value, label);
       //添加一个空行
       EditorGUILayout.Space();
    }
}
原创粉丝点击