Unity自定义编辑器:基于枚举类型动态显示属性

1.参考链接

2.应用 target并设置多选编辑 添加CanEditMultipleObjects

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(LightsState))]
[CanEditMultipleObjects]
public class TestInspector : Editor
{
    private SerializedObject obj;
    private LightsState lightsState;
    private SerializedProperty equipMentId_;
    private SerializedProperty powerType_;
    private SerializedProperty material;

    void OnEnable()
    {
        obj = new SerializedObject(target);
        material = obj.FindProperty("materialReplace");

        equipMentId_= obj.FindProperty("equipMentId");
        powerType_= obj.FindProperty("powerType");
    }

    public override void OnInspectorGUI()
    {
        obj.Update();
        EditorGUILayout.PropertyField(equipMentId_);
        EditorGUILayout.PropertyField(powerType_);

        lightsState = (LightsState)target;
        lightsState.lightType = (LightType)EditorGUILayout.EnumPopup("信号灯类型切换-", lightsState.lightType);

        if (lightsState.lightType == LightType.柜体指示灯)
        {
            EditorGUILayout.PropertyField(material);
        }
        // 保存更改  
        obj.ApplyModifiedProperties();
    }
}
cs 复制代码
/****************************************************************************
 *  #SMARTDEVELOPERS#
 *  项目名称:#PROJECTNAME#
 *  描述: xxxxxxxxxxxxx
 *  版本: V1.00
 *  创建人: ****
 *  创建时间: #CREATETIME#
 *  程序修改记录(最新的放在最前面):
 * <版本号> <修改日期>, <修改人员>: <修改功能概述>
 * **************************************************************************/
using Custom;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LightsState : MonoBehaviour
{
    [Header("设备id")]
    public string equipMentId;
    [Header("指示灯类型")]
    public LightType lightType;
    [Header("电源四个孔判断")]
    public ButtonPosition powerType;
    public Material materialReplace;
    //led
    private Image image_light;
    private Color colorDefaultLed;
    //柜体
    private MeshRenderer meshrenderer;
    private Material materialdefault;
    private void Awake()
    {
        Init();
    }
    void Init()
    {
        switch (lightType)
        {
            case LightType.页面Led信号灯:
                image_light = GetComponent<Image>();
                colorDefaultLed = image_light.color;
                break;
            case LightType.柜体指示灯:
                meshrenderer = GetComponent<MeshRenderer>();
                materialdefault = meshrenderer.material;
                break;
            default:
                break;
        }
    }
    private void OnEnable()
    {
        MessageCenter.Instance.RegisterListener((Enum)(MessageFanType.SignalState), InvokeMessage);
        MessageCenter.Instance.RegisterListener((Enum)(MessageFanType.Totalpower), InvokeMessageTotalpower);
        UpdateState();
    }
    void InvokeMessage(Message message)
    {
        switch (powerType)
        {
            case ButtonPosition.ShallowHoleOne:
            case ButtonPosition.CenterOne:
                if (PowerManagement.LeftPower == 1)
                {
                    //左侧总电源
                    //---------------------
                    SingleStateData singleStateData = (SingleStateData)message.Body;
                    if (singleStateData.equipmentid == equipMentId)
                    {
                        switch (lightType)
                        {
                            case LightType.页面Led信号灯:
                                ExturedLedLight(singleStateData.state);
                                break;
                            case LightType.柜体指示灯:
                                ExturedMaterialLight(singleStateData.state);
                                break;
                            default:
                                break;
                        }

                    }
                }
                else
                {

                }
                break;
            case ButtonPosition.ShallowHoleTwo:
            case ButtonPosition.CenterTwo:
                if (PowerManagement.RightPower == 1)
                {
                    //右侧总电源
                    //---------------------
                    SingleStateData singleStateData = (SingleStateData)message.Body;
                    if (singleStateData.equipmentid == equipMentId)
                    {
                        switch (lightType)
                        {
                            case LightType.页面Led信号灯:
                                ExturedLedLight(singleStateData.state);
                                break;
                            case LightType.柜体指示灯:
                                ExturedMaterialLight(singleStateData.state);
                                break;
                            default:
                                break;
                        }

                    }
                }
                else
                {

                }
                break;
            default:
                break;
        }
       
    }
    void InvokeMessageTotalpower(Message message)
    {
        UpdateState();
    }
    /// <summary>
    /// 手动更新 
    /// </summary>
    public void UpdateState()
    {
        if (string.IsNullOrEmpty(equipMentId))
        {
            Debug.LogWarning("设备id未赋值:" + transform.name);
            return;
        }

        if (SystemDataSet.AllData_YX.ContainsKey(equipMentId))
        {
            switch (powerType)
            {
                case ButtonPosition.ShallowHoleOne:
                case ButtonPosition.CenterOne:
                    if(PowerManagement.LeftPower == 1)
                    {
                        switch (lightType)
                        {
                            case LightType.页面Led信号灯:
                                ExturedLedLight(SystemDataSet.AllData_YX[equipMentId].pointStatus);
                                break;
                            case LightType.柜体指示灯:
                                ExturedMaterialLight(SystemDataSet.AllData_YX[equipMentId].pointStatus);
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        //只做信号灯更新 不做改值
                        switch (lightType)
                        {
                            case LightType.页面Led信号灯:
                                ExturedLedLight(0);
                                break;
                            case LightType.柜体指示灯:
                                ExturedMaterialLight(0);
                                break;
                            default:
                                break;
                        }

                    }
                    break;
                case ButtonPosition.ShallowHoleTwo:
                case ButtonPosition.CenterTwo:
                    if (PowerManagement.RightPower == 1)
                    {
                        switch (lightType)
                        {
                            case LightType.页面Led信号灯:
                                ExturedLedLight(SystemDataSet.AllData_YX[equipMentId].pointStatus);
                                break;
                            case LightType.柜体指示灯:
                                ExturedMaterialLight(SystemDataSet.AllData_YX[equipMentId].pointStatus);
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        //只做信号灯更新 不做改值
                        switch (lightType)
                        {
                            case LightType.页面Led信号灯:
                                ExturedLedLight(0);
                                break;
                            case LightType.柜体指示灯:
                                ExturedMaterialLight(0);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                default:
                    break;
            }
          
        }
    }
    /// <summary>
    /// led 灯
    /// </summary>
    void ExturedLedLight(int state)
    {
        if (state==0)
        {
            image_light.color = Color.cyan;
        }
        else
        {
            image_light.color = colorDefaultLed;
        }
    }
    /// <summary>
    /// 柜体灯
    /// </summary>
    void ExturedMaterialLight(int state)
    {
        if (state == 0)
        {
            meshrenderer.material = materialdefault;
        }
        else
        {
            if (materialReplace == null)
            {
                Debug.LogError("信号灯替换材质未赋值!!!" + transform.name);
            }
            else
            {
                meshrenderer.material = materialReplace;
            }
        }
    }
    private void OnDisable()
    {
        MessageCenter.Instance.RemoveListener(MessageFanType.SignalState, InvokeMessage);
        MessageCenter.Instance.RemoveListener(MessageFanType.Totalpower, InvokeMessageTotalpower);
    }
}
相关推荐
HH‘HH3 小时前
Unity 相机与光照调整:从基础到进阶的实战指南
unity
热爱生活的五柒5 小时前
如何快速删除vscode项目下所有的http链接
ide·vscode·编辑器
合众恒跃6 小时前
RK3588 + RK182X 双芯异构边缘计算方案:部署流程与实测性能解析
大数据·人工智能·科技·编辑器·etl工程师
HH‘HH21 小时前
Unity引擎界面各个功能面板详细介绍
unity·游戏引擎
℡枫叶℡21 小时前
Unity 2D资产命名常用缩写
unity·资产命名缩写
JK Chen1 天前
UE 编辑器内 Source Code IDE 没有 Rider
ide·编辑器
丁小未1 天前
Unity AssetBundle商业化项目资源方案
unity·游戏引擎·assetbundle·unity框架
unityのkiven1 天前
通过 Unity 实现杀戮尖塔2式卡牌系统与机制
unity·游戏引擎
郝学胜-神的一滴1 天前
中级OpenGL教程 020:巧用数组与循环实现多点点光源渲染,告别冗余代码重构方案
c++·unity·游戏引擎·godot·图形渲染·unreal
图扑软件1 天前
其域创新 × 图扑软件|3DGS 高斯泼溅智慧钢厂实景孪生
前端·低代码·3d·编辑器·图形渲染·可视化