当前位置: 首页 > article >正文

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


1.参考链接

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

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();
    }
}
/****************************************************************************
 *  #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);
    }
}


http://www.kler.cn/a/488311.html

相关文章:

  • 了解模2除法:原理与应用
  • ios越狱脚本巨魔商店安装教程
  • Dart语言的语法糖
  • 新时期下k8s 网络插件calico 安装
  • 《解锁计算机视觉智慧:编程实现图片场景文字描述的开源宝藏》
  • 数据结构:包装类和泛型
  • sql 函数
  • 联邦大语言模型典型系统: FATE - LLM、FedLLM、FederatedScope - LLM、PrimiHub
  • 【Flink】flink或java异常日志输出不完整问题解决
  • 10步打造完美ASP.NET、Web API和控制台应用程序文件夹结构
  • C语言:构造类型(共用体/联合体,枚举)
  • Apache AGE:基于PostgreSQL的图数据库深度解析
  • Python--正则表达式
  • HarmonyOS开发:粒子动画应用实战
  • pytest 常用插件
  • Redis数据库笔记——Redis快的原因
  • Java(day7)
  • HTTP 与 HTTPS 网络传输
  • Linux -- 初步了解 TCP 编程
  • ubuntu 20.04 安装 5.4 内核
  • CClinkIEfield Basic转Modbus TCP网关模块连接三菱FX5U PLC
  • 关于物联网的基础知识(四)——国内有代表性的物联网平台都有哪些?
  • xml-dota-yolo数据集格式转换
  • 【FPGA】时序约束与分析
  • 部署langchain服务
  • 使用 FastAPI 和 Async Python 打造高性能 API