UnityInspector面板上显示中文

更新时间:2023-08-03 12:36:04 阅读: 评论:0

UnityInspector⾯板上显⽰中⽂在Unity上显⽰中⽂,可以这样搞,直接上代码
/*--------------------------------------------------------------------
* Author  Name: DXL
* Creation Time: 2018.10.11
* File Describe: Inspector⾯板上⾼级显⽰,例如显⽰中⽂
* ------------------------------------------------------------------*/
using System;
using UnityEngine;
//显⽰中⽂
[AttributeUsage(AttributeTargets.Field)]
public class InspectorShow : PropertyAttribute
{
public string label;        //要显⽰的字符
public InspectorShow(string label)
{
永久和平
this.label = label;
}
}
//显⽰颜⾊
public class TitleAttribute : PropertyAttribute
{
情绪疗法/// <summary> 标题名称 </summary>
public string title;
/// <summary> 标题颜⾊ </summary>
public string htmlColor;
吹箫技巧/// <summary> 在属性上⽅添加⼀个标题 </summary>
/// <param name="title">标题名称</param>
/// <param name="htmlColor">标题颜⾊</param>高丽菜图片
public TitleAttribute(string title, string htmlColor = "#FFFFFF")
{
this.title = title;
this.htmlColor = htmlColor;
}
}
//显⽰枚举名称
[AttributeUsage(AttributeTargets.Field)]
public class EnumNameAttribute : PropertyAttribute
{
/// <summary> 枚举名称 </summary>
public string name;
public new int[] order = new int[0];
public EnumNameAttribute(string name)
{
this.name = name;
}
public EnumNameAttribute(string label, params int[] order)
{
this.name = label;
}
}
这段代码和⼯程中的其他代码放⼀起,⼀般情况下是放在Asts/Scripts⽂件夹下。
``
* Author  Name: DXL
* Creation Time: 2018.10.11
* File Describe: 实现InspectorShow⾥的⽅法,实现Inspector⾯板上的⾼级显⽰
* ------------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
//将属性名显⽰成中⽂
[CustomPropertyDrawer(typeof(InspectorShow))]
public class FieldLabelDrawer : PropertyDrawer
{
private InspectorShow FLAttribute
{
get { return (InspectorShow)attribute; }
获取你想要绘制的字段
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)    {
//在这⾥重新绘制
EditorGUI.PropertyField(position, property, new GUIContent(FLAttribute.label), true);
}
}
//显⽰颜⾊
[CustomPropertyDrawer(typeof(TitleAttribute))]
public class TitleAttributeDrawer : DecoratorDrawer
{
// ⽂本样式
private GUIStyle style = new GUIStyle();
碧柳public override void OnGUI(Rect position)
{
// 获取Attribute
TitleAttribute attr = (TitleAttribute)attribute;
/
/ 转换颜⾊
Color color = htmlToColor(attr.htmlColor);
// 重绘GUI
position = EditorGUI.IndentedRect(position);
GUI.Label(position, attr.title, style);
}
public override float GetHeight()
{
return ba.GetHeight() - 4;
}
/
// <summary> Html颜⾊转换为Color </summary>
/// <param name="hex"></param>
/// <returns></returns>
private Color htmlToColor(string hex)
{
{
// 默认⿊⾊
if (string.IsNullOrEmpty(hex)) return Color.black;
// 转换颜⾊
hex = hex.ToLower();
if (hex.IndexOf("#") == 0 && hex.Length == 7)
{
int r = Convert.ToInt32(hex.Substring(1, 2), 16);            int g = Convert.ToInt32(hex.Substring(3, 2), 16);            int b = Convert.ToInt32(hex.Substring(5, 2), 16);            return new Color(r / 255f, g / 255f, b / 255f);
}
el if (hex == "red")
{
d;
}
el if (hex == "green")
{
;头像图片背影
}
el if (hex == "blue")
{
return Color.blue;
}
el if (hex == "yellow")
{
llow;
}
el if (hex == "black")
{古语
return Color.black;
}
el if (hex == "white")
{
return Color.white;
}
el if (hex == "cyan")
{
an;
}
el if (hex == "gray")
{
ay;
}
el if (hex == "grey")
{
;
}
el if (hex == "magenta")
{
return Color.magenta;
}
el
{
return Color.black;
}
}
}
//显⽰中⽂枚举
[CustomPropertyDrawer(typeof(EnumNameAttribute))] public class EnumNameDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// 替换属性名称
EnumNameAttribute rename = (EnumNameAttribute)attribute;
< = rename.name;
// 重绘GUI
bool isElement = Regex.IsMatch(property.displayName, "Element \\d+");
if (isElement) = property.displayName;
if (property.propertyType == SerializedPropertyType.Enum)
{
drawEnum(position, property, label);
手机怎么打印文件
}
el
{
EditorGUI.PropertyField(position, property, label, true);
}
}
// 绘制枚举类型
private void drawEnum(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginChangeCheck();
// 获取枚举相关属性
Type type = fieldInfo.FieldType;
string[] names = umNames;
string[] values = new string[names.Length];
while (type.IsArray) type = type.GetElementType();
// 获取枚举所对应的名称
for (int i = 0; i < names.Length; i++)
{
FieldInfo info = type.GetField(names[i]);
EnumNameAttribute[] atts = (EnumNameAttribute[])info.GetCustomAttributes(typeof(EnumNameAttribute), fal);            values[i] = atts.Length == 0 ? names[i] : atts[0].name;
}
// 重绘GUI
int index = EditorGUI.Popup(position, , umValueIndex, values);
if (EditorGUI.EndChangeCheck() && index != -1) umValueIndex = index;
}
}
这段代码放到编辑器⽂件夹下,⼀般是Asts/Editor下

本文发布于:2023-08-03 12:36:04,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1106558.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:属性   获取   名称   绘制
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图