样式
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace xxx
{
public class StyleUtils
{
public static GUIStyle GetStyle(string stylestr)
{
GUIStyle style = new GUIStyle(stylestr);
style.stretchHeight = true;
style.stretchWidth = true;
style.border = new RectOffset(11, 11, 11, 15);
style.margin = new RectOffset(2, 2, 0, 0);
style.padding = new RectOffset(2, 2, 0, 0);
style.overflow = new RectOffset(7, 7, 6, 9);
style.alignment = TextAnchor.MiddleCenter;
style.fontSize = 14;
style.richText = true;
return style;
}
public static GUIStyle GetStyle(string stylestr, TextAnchor textAnchor)
{
GUIStyle style = new GUIStyle(stylestr);
style.stretchHeight = true;
style.stretchWidth = true;
style.border = new RectOffset(11, 11, 11, 15);
style.margin = new RectOffset(2, 2, 0, 0);
style.padding = new RectOffset(2, 2, 0, 0);
style.overflow = new RectOffset(7, 7, 6, 9);
style.alignment = textAnchor;
style.fontSize = 14;
style.richText = true;
return style;
}
public static GUIStyle GetLine()
{
GUIStyle style = new GUIStyle("ProfilerRightPane");
return style;
}
}
}
#endif
图片资源特性
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace xxx
{
[AttributeUsage( AttributeTargets.Field, AllowMultiple =false)]
public class ImagePathAttribute : Attribute
{
private string assetPath = string.Empty;
public string AssetPath => assetPath;
public ImagePathAttribute(string assetPath)
{
this.assetPath = assetPath;
}
}
}
使用方法
[ImagePath(duck_path + "duck-01.png")]
public static Texture2D duck_go_2;
多语言特性
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace xxx
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class TranslationAttribute : Attribute
{
private TranslationEnum m_enum;
public TranslationEnum M_enum => m_enum;
private string m_str;
public string M_str => m_str;
public TranslationAttribute(TranslationEnum @enum, string str)
{
m_enum = @enum;
m_str = str;
}
}
}
#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace xxx
{
public class AttributeUtils
{
public static void GetIconDataAttribute()
{
Type type = typeof(IconUtils);
System.Reflection.FieldInfo[] fieldInfo = type.GetFields();
foreach (var item in fieldInfo)
{
ImagePathAttribute attribute = (ImagePathAttribute)Attribute.GetCustomAttribute(item, typeof(ImagePathAttribute));
if (attribute != null)
{
var texture = AssetUtils.GetTexture2D(attribute.AssetPath);
if (texture != null)
{
item.SetValue(item, texture);
}
}
}
}
public static void GetTranslationAttribute(TranslationEnum language)
{
var type = Activator.CreateInstance<FieldLNGConfig>();
System.Reflection.FieldInfo[] fieldInfo = typeof(FieldLNGConfig).GetFields();
foreach (var item in fieldInfo)
{
var attribute = (TranslationAttribute[])Attribute.GetCustomAttributes(item, type: typeof(TranslationAttribute));
foreach (var atr in attribute)
{
if(atr.M_enum == language)
{
item.SetValue(item, atr.M_str);
}
}
}
}
}
}
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace xxx
{
public enum TranslationEnum
{
ChineseSimplified = 40,
Enginlish = 10,
}
}
使用方法
[Translation(TranslationEnum.Enginlish, "Voice Position")]
[Translation(TranslationEnum.ChineseSimplified, "说话位置")]
public static string voicePos_str = "";