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");
        }
    }
}