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

devtools/2025/1/8 18:46:05/


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;//ledprivate 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.ppmy.cn/devtools/148964.html

相关文章

C语言——字符函数和内存函数

目录 前言 字符函数 1strlen 模拟实现 2strcpy 模拟实现 3strcat 模拟实现 4strcmp 模拟实现 5strncpy 模拟实现 6strncat 模拟实现 7strncmp 模拟实现 8strstr 模拟实现 9strtok 10strerror 11大小写字符转换函数 内存函数 1memcpy 模拟实现 2…

什么是 ERP?

目录 企业资源计划&#xff08;ERP&#xff09;的定义 ERP与财务管理的区别 ERP基础知识 ERP的业务价值 ERP简史 ERP部署模式&#xff1a;从本地部署到云端 ERP云 — 新的ERP交付模式 迁移至ERP云技术解决方案的7个原因 企业资源计划&#xff08;ERP&#xff09;的定义 …

线程-8-日志_线程池

主要看code 日志&#xff1a; 日志指标/属性 设计模式&#xff1a;策略模式 日志格式&#xff1a; [可读性很好的时间] [⽇志等级] [进程pid] [打印对应⽇志的⽂件名][⾏号] - 消息内容&#xff0c;⽀持可 变参数 [2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [16] - h…

git 常用命令和本地合并解决冲突

目录 一、常用命令 二、本地可视化合并分支解决冲突 一、常用命令 最近&#xff0c;使用mac电脑&#xff0c;无法直接使用小乌龟进行可视化操作&#xff0c;现在记录一些常用命令。 拉取&#xff1a; git clone <git url> 仅拉起某个单独分支&#xff1a; git clo…

playwright 录制

一、新建项目TestProject3 二、准备swagger 三、开始录制 打开PowerShell 7 (x64) cd D:\xxx\xxx\VS2022Projects\TestProject3\TestProject3\bin\Debug\net8.0 pwsh playwright.ps1 codegen --targetcsharp -b chromium localhost:5252/swagger/index.html #支持的语言 java…

conda快速安装并配置pycharm

1、镜像下载地址 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/2、安装完成后配置环境变量 安装记得all users 配置环境 3、配置镜像 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https…

阿尔法linux开发板ping不通百度

我使用的阿尔法linux板子&#xff0c;发现按照《03【正点原子】I.MX6U网络环境TFTP&NFS搭建手册V1.3.2》一套操作下来&#xff0c;还是没办法实现板子上网。 我总结了下面方法&#xff0c;我如何实现联网和互ping通&#xff0c;大致总结下三步 一、pc端的wifi网络&#xf…

Flutter Web 中文字体显示异常问题

flutter web 在中文使用粗体的时候发现了两个问题 一个字的笔画颜色不相同带有 ‘口’的字 这个口由于太粗出现了实体闭合的情况 解决方案 替换字体 对于这个问题解决的办法只有替换中文字体库&#xff0c;因为只有粗体才有问题&#xff0c;所以只需要添加粗体字体即可。我…