框架上传
This commit is contained in:
47
Assets/00.StaryEvo/Editor/EditorFont/ChangeFont.cs
Normal file
47
Assets/00.StaryEvo/Editor/EditorFont/ChangeFont.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Stary.Evo.Editor
|
||||
{
|
||||
public class ChangeFont :IFontChange
|
||||
{
|
||||
Font toChange;
|
||||
static Font toChangeFont;
|
||||
FontStyle toFontStyle;
|
||||
static FontStyle toChangeFontStyle;
|
||||
public void Creat()
|
||||
{
|
||||
EditorGUILayout.Space();
|
||||
toChange = (Font) EditorGUILayout.ObjectField(toChange, typeof(Font), true, GUILayout.MinWidth(100),GUILayout.MinHeight(30));
|
||||
toChangeFont = toChange;
|
||||
EditorGUILayout.Space();
|
||||
toFontStyle = (FontStyle) EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f),GUILayout.MinHeight(30));
|
||||
toChangeFontStyle = toFontStyle;
|
||||
EditorGUILayout.Space();
|
||||
GUI.color = Color.green;
|
||||
if (GUILayout.Button("更换",GUILayout.MinWidth(200), GUILayout.MinHeight(50)))
|
||||
{
|
||||
Change();
|
||||
}
|
||||
}
|
||||
private void Change()
|
||||
{
|
||||
var tArray = Resources.FindObjectsOfTypeAll(typeof(Text));
|
||||
for (int i = 0; i < tArray.Length; i++)
|
||||
{
|
||||
Text t = tArray[i] as Text;
|
||||
//这个很重要,博主发现如果没有这个代码,unity是不会察觉到编辑器有改动的,自然设置完后直接切换场景改变是不被保存的
|
||||
//如果不加这个代码,在做完更改后,自己随便手动修改下场景里物体的状态,在保存就好了
|
||||
Undo.RecordObject(t, t.gameObject.name);
|
||||
t.font = toChangeFont;
|
||||
t.fontStyle = toChangeFontStyle;
|
||||
//相当于让他刷新下 不然unity显示界面还不知道自己的东西被换掉了,还会呆呆的显示之前的东西
|
||||
EditorUtility.SetDirty(t);
|
||||
}
|
||||
|
||||
Debug.Log("Succed");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/00.StaryEvo/Editor/EditorFont/ChangeFont.cs.meta
Normal file
11
Assets/00.StaryEvo/Editor/EditorFont/ChangeFont.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c8ea60d514c51142a7ede6495a50981
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
49
Assets/00.StaryEvo/Editor/EditorFont/CreatWindowFont.cs
Normal file
49
Assets/00.StaryEvo/Editor/EditorFont/CreatWindowFont.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
/****************************************************
|
||||
文件:CreatWindowFont.cs
|
||||
作者:张铮
|
||||
邮箱:834207172@qq.com
|
||||
日期:2022/3/6 16:31:29
|
||||
功能:对更换字体功能进行重构
|
||||
*****************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo.Editor
|
||||
{
|
||||
|
||||
public class CreatWindowFont : EditorWindow
|
||||
{
|
||||
private FontMode fontMode;
|
||||
private IFontChange fontChange = new ChangeFont();
|
||||
private IFontChange defaultfontChange = new DefaultFontEditor();
|
||||
|
||||
[MenuItem("Evo/Utility/字体设置", false, 1)]
|
||||
static void Create()
|
||||
{
|
||||
GetWindow<CreatWindowFont>().Show();
|
||||
}
|
||||
void OnGUI()
|
||||
{
|
||||
fontMode = (FontMode) GUILayout.Toolbar((int) fontMode, Enum.GetNames(typeof(FontMode)));
|
||||
switch (fontMode)
|
||||
{
|
||||
case FontMode.更换字体:
|
||||
fontChange.Creat();
|
||||
break;
|
||||
case FontMode.默认字体:
|
||||
defaultfontChange.Creat();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum FontMode
|
||||
{
|
||||
更换字体,
|
||||
默认字体
|
||||
}
|
||||
}
|
||||
11
Assets/00.StaryEvo/Editor/EditorFont/CreatWindowFont.cs.meta
Normal file
11
Assets/00.StaryEvo/Editor/EditorFont/CreatWindowFont.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e553dbbd76141cb43a861b229a57d619
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
186
Assets/00.StaryEvo/Editor/EditorFont/DefaultFontEditor.cs
Normal file
186
Assets/00.StaryEvo/Editor/EditorFont/DefaultFontEditor.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using System.IO;
|
||||
using EditorFramework;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Stary.Evo.Editor
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置字体面板
|
||||
/// </summary>
|
||||
public class DefaultFontEditor : IFontChange
|
||||
{
|
||||
private static Font m_font;
|
||||
|
||||
public void Creat()
|
||||
{
|
||||
EditorGUILayout.Space();
|
||||
EditorGUILayout.LabelField("选择默认字体");
|
||||
EditorGUILayout.Space();
|
||||
m_font = (Font) EditorGUILayout.ObjectField(m_font, typeof(Font), true,GUILayout.MinWidth(100f),GUILayout.MinHeight(30));
|
||||
EditorGUILayout.Space();
|
||||
GUI.color = Color.green;
|
||||
if (GUILayout.Button("确定",GUILayout.MinWidth(200), GUILayout.MinHeight(50)))
|
||||
{
|
||||
ToolCacheManager.SaveFont(m_font);
|
||||
}
|
||||
}
|
||||
[MenuItem("GameObject/UI/DefaultText")]
|
||||
static void CreatImage()
|
||||
{
|
||||
Text text = CreateComponent<Text>();
|
||||
if (text!=null)
|
||||
{
|
||||
text.font = ToolCacheManager.GetFont();
|
||||
}
|
||||
}
|
||||
private static T CreateComponent<T>() where T : Component
|
||||
{
|
||||
string name = typeof(T).Name;
|
||||
GameObject go = new GameObject(name);
|
||||
Transform parent = GetUIParent();
|
||||
go.transform.SetParent(parent, false);
|
||||
T com = go.AddComponent<T>();
|
||||
return com;
|
||||
}
|
||||
private static Transform GetUIParent()
|
||||
{
|
||||
Transform select = Selection.activeTransform;
|
||||
if (select)
|
||||
{
|
||||
if (select.GetComponentInParent<Canvas>())
|
||||
{
|
||||
return select;
|
||||
}
|
||||
else
|
||||
{
|
||||
Canvas canvas = select.GetComponentInChildren<Canvas>();
|
||||
if (canvas == null)
|
||||
{
|
||||
canvas = InstanceCanvas(select);
|
||||
}
|
||||
return canvas.transform;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Canvas canvas = GetCanvasInScene();
|
||||
if (canvas == null)
|
||||
{
|
||||
canvas = InstanceCanvas();
|
||||
}
|
||||
return canvas.transform;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 参考PrefabStageUtility.HandleUIReparentingIfNeeded
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
static Canvas GetCanvasInScene()
|
||||
{
|
||||
Scene scene = SceneManager.GetActiveScene();
|
||||
foreach (GameObject go in scene.GetRootGameObjects())
|
||||
{
|
||||
var canvas = go.GetComponentInChildren<Canvas>();
|
||||
if (canvas != null)
|
||||
return canvas;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 参考PrefabStageUtility.HandleUIReparentingIfNeeded
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static Canvas InstanceCanvas(Transform parent = null)
|
||||
{
|
||||
const string kUILayerName = "UI";
|
||||
GameObject root = EditorUtility.CreateGameObjectWithHideFlags("Canvas", HideFlags.DontSave);
|
||||
root.layer = LayerMask.NameToLayer(kUILayerName);
|
||||
Canvas canvas = root.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
root.AddComponent<CanvasScaler>();
|
||||
root.AddComponent<GraphicRaycaster>();
|
||||
if (parent)
|
||||
{
|
||||
canvas.transform.SetParent(parent, false);
|
||||
}
|
||||
return canvas;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存管理类
|
||||
/// </summary>
|
||||
public class ToolCacheManager
|
||||
{
|
||||
private static string mFontDataPath = "Assets/Main/Tools/Font/FontData.asset";
|
||||
|
||||
private static string FontDataFolderPath
|
||||
{
|
||||
get { return Application.dataPath + "/Main/Tools/Font"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存设置的字体
|
||||
/// </summary>
|
||||
/// <param name="font"></param>
|
||||
public static void SaveFont(Font font)
|
||||
{
|
||||
CheckFontDataFolderExist();
|
||||
EditorFrameworkUtils.DeleteFile(FontDataFolderPath, "FontData.asset");
|
||||
FontData data = ScriptableObject.CreateInstance<FontData>();
|
||||
data.defaultFont = font;
|
||||
AssetDatabase.CreateAsset(data, mFontDataPath);
|
||||
EditorUtility.SetDirty(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从FontData.asset获取字体
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Font GetFont()
|
||||
{
|
||||
FontData fontData = AssetDatabase.LoadAssetAtPath<FontData>(mFontDataPath);
|
||||
if (fontData != null)
|
||||
return fontData.defaultFont;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保存放FontData.asset文件夹存在
|
||||
/// </summary>
|
||||
private static void CheckFontDataFolderExist()
|
||||
{
|
||||
if (!Directory.Exists(FontDataFolderPath))
|
||||
{
|
||||
Directory.CreateDirectory(FontDataFolderPath);
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
[InitializeOnLoad]
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class UnityUIEvent
|
||||
{
|
||||
static UnityUIEvent()
|
||||
{
|
||||
ObjectFactory.componentWasAdded += ComponentWasAdded;
|
||||
}
|
||||
|
||||
private static void ComponentWasAdded(Component obj)
|
||||
{
|
||||
Text text= obj.gameObject.GetComponent<Text>();
|
||||
if (text!=null)
|
||||
{
|
||||
text.font = ToolCacheManager.GetFont();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e800f1227426ad4ea0fd607689bf25e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
10
Assets/00.StaryEvo/Editor/EditorFont/FontData.cs
Normal file
10
Assets/00.StaryEvo/Editor/EditorFont/FontData.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo.Editor
|
||||
{
|
||||
[System.Serializable]
|
||||
public class FontData : ScriptableObject
|
||||
{
|
||||
[SerializeField] public Font defaultFont;
|
||||
}
|
||||
}
|
||||
11
Assets/00.StaryEvo/Editor/EditorFont/FontData.cs.meta
Normal file
11
Assets/00.StaryEvo/Editor/EditorFont/FontData.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95cd4753d6d6804499cc0a96c91cdecc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
19
Assets/00.StaryEvo/Editor/EditorFont/IFontChange.cs
Normal file
19
Assets/00.StaryEvo/Editor/EditorFont/IFontChange.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
/****************************************************
|
||||
文件:IFontChange.cs
|
||||
作者:zz
|
||||
邮箱:
|
||||
日期:2022/3/6 16:34:36
|
||||
功能:
|
||||
*****************************************************/
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo.Editor
|
||||
{
|
||||
interface IFontChange
|
||||
{
|
||||
void Creat();
|
||||
}
|
||||
}
|
||||
11
Assets/00.StaryEvo/Editor/EditorFont/IFontChange.cs.meta
Normal file
11
Assets/00.StaryEvo/Editor/EditorFont/IFontChange.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 601a6ca5b11bb6c489a238de6bfb56d1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user