使用Preset一键设置文本样式
# 一般界面多的项目,都会有一套自己的ui规范,比如:1级弹框的按钮文本用多大,什么字体,什么颜色,二级弹框的用什么,
新年活动风格的一套界面文本用多大,什么字体,什么颜色,圣诞活动风格的界面用什么。。。
# 每次ui给到界面示意图时,最痛苦的就是,文本样式都是按照之前的xxx界面。所以把这些规范放入一套配置文件,用的时候直接设置就将方便很多。
# unity 2018开始,提供的preset就可以做这样的事。但是默认preset会把Text的所有属性都覆盖掉,但我们这边只需要覆盖font, font size和color
[CustomEditor(typeof(RectTransform))] [CanEditMultipleObjects] public class CustomRectTransformEditor : Editor { private Type _editorType; private UnityEditor.Editor _editorInstance; private List<RectTransform> _rtfList; private List<Text> _textList; private void OnEnable() { if (null != _editorInstance) return; if (null == _editorType) { var typeFullName = "UnityEditor.RectTransformEditor"; var dllName = "UnityEditor"; _editorType = Type.GetType($"{typeFullName},{dllName}"); } if (null == _editorType) return; if (null != targets) { _editorInstance = CreateEditor(targets, _editorType); CheckTargets(); } } private void CheckTargets() { for (var i = 0; i < targets.Length; ++i) { var t = targets[i]; var rtf = t as RectTransform; if (null != rtf) { if (null == _rtfList) _rtfList = new List<RectTransform>(); _rtfList.Add(rtf); var text = rtf.GetComponent<Text>(); if (null != text) { if (null == _textList) _textList = new List<Text>(); _textList.Add(text); } } } } private void OnDisable() { if (null != _editorInstance) { DestroyImmediate(_editorInstance); _editorInstance = null; if (null != _rtfList) _rtfList.Clear(); if (null != _textList) _textList.Clear(); } } public override void OnInspectorGUI() { if (null != _editorInstance) _editorInstance.OnInspectorGUI(); OnInspectorGUI_TextPreset(); } //==================== Text Preset private void OnInspectorGUI_TextPreset() { if (null != Selection.activeObject && Selection.activeObject is Preset) { var p = (Preset)Selection.activeObject; if (GUILayout.Button("设置FontAndSize")) { var modifyProps = p.PropertyModifications; foreach (var modifyProp in modifyProps) { switch (modifyProp.propertyPath) { case "m_FontData.m_FontSize": SetTextSize(int.Parse(modifyProp.value)); break; case "m_FontData.m_Font": SetTextFont(modifyProp.objectReference as Font); break; } } } if (GUILayout.Button("设置Color")) { var modifyProps = p.PropertyModifications; Color tempColor = Color.white; foreach (var modifyProp in modifyProps) { switch (modifyProp.propertyPath) { case "m_Color.r": tempColor.r = float.Parse(modifyProp.value); break; case "m_Color.g": tempColor.g = float.Parse(modifyProp.value); break; case "m_Color.b": tempColor.b = float.Parse(modifyProp.value); break; case "m_Color.a": tempColor.a = float.Parse(modifyProp.value); break; } } SetTextColor(ref tempColor); } } } private void SetTextSize(int size) { for (var i = 0; i < _textList.Count; ++i) { _textList[i].fontSize = size; } } private void SetTextFont(Font font) { for (var i = 0; i < _textList.Count; ++i) { _textList[i].font = font; } } private void SetTextColor(ref Color c) { for (var i = 0; i < _textList.Count; ++i) { _textList[i].color = c; } } //==================== }
【关于如何创建Preset文件】
# 每个组件都可以保存自己的Preset
【参考】
使用Unity Preset改进目前的工作流,提高开发效率 - 知乎 (zhihu.com)
Unity Presets预设_TO_ZRG的博客-CSDN博客
Unity3D研究院编辑器之脚本生成Preset Libraries(十四) | 雨松MOMO程序研究院 (xuanyusong.com)