/**************************************************** 
 文件：StringToLayer.cs
 作者：张铮
 邮箱：834207172@qq.com
 日期：2022/3/3 17:53:52
 功能：
*****************************************************/

using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace Stary.Evo.Editor
{
    public class StringToLayer
    {
        [MenuItem("Evo/Utility/Layer和Tag转换")]
        private static void Layer()
        {
            var sb = new StringBuilder(); //准备模板生成
            sb.AppendLine("public class _Const");
            sb.AppendLine("{");

            for (int i = 0; i < 32; i++) //遍历所有Layer
            {
                var name = LayerMask.LayerToName(i); //通过Unity的接口拿到Layer名称
                name = name
                    .Replace(" ", "_")
                    .Replace("&", "_")
                    .Replace("/", "_")
                    .Replace(".", "_")
                    .Replace(",", "_")
                    .Replace(";", "_")
                    .Replace("-", "_"); //对常见的特殊字符进行过滤
                if (!string.IsNullOrEmpty(name))
                    sb.AppendFormat("\tpublic const int LAYER_{0} = {1};\n", name.ToUpper(), i);
            }

            sb.AppendLine("\tpublic const string " + ("Tag_Untagged".ToUpper() + " = "
                                                                               + "\"Untagged\";"));
            sb.AppendLine("\tpublic const string " + ("Tag_Respawn".ToUpper() + " = "
                                                                              + "\"Respawn\";"));
            sb.AppendLine("\tpublic const string " + ("Tag_Finish".ToUpper() + " = "
                                                                             + "\"Finish\";"));
            sb.AppendLine("\tpublic const string " + ("Tag_EditorOnly".ToUpper() + "= " + "\"EditorOnly\";"));
            sb.AppendLine("\tpublic const string " + ("Tag_MainCamera".ToUpper() + "= " + "\"MainCamera\";"));
            sb.AppendLine("\tpublic const string " + ("Tag_Player".ToUpper() + " = "
                                                                             + "\"Player\";"));
            sb.AppendLine("\tpublic const string " + ("Tag_GameController".ToUpper()
                                                      + " = " + "\"GameController\";")); //把一部分内置Tag先写死
            var asset = UnityEditor.AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset"); //取得自定义Tag
            if ((asset != null) && (asset.Length > 0))
            {
                for (int i = 0; i < asset.Length; i++)
                {
                    //创建序列化对象
                    var so = new UnityEditor.SerializedObject(asset[i]);
                    var tags = so.FindProperty("tags"); //读取具体字段
                    for (int j = 0; j < tags.arraySize; ++j)
                    {
                        var item = tags.GetArrayElementAtIndex(j).stringValue;
                        sb.AppendFormat("\tpublic const string TAG_{0} = \"{1}\";\n",
                            item.ToUpper(), item);
                    } //添加到模板
                }
            }

            sb.AppendLine("}");
            File.WriteAllText("Assets/HotUpdate/GeneratedConst.cs", sb.ToString()); //写入硬盘
            UnityEditor.AssetDatabase.Refresh(); //通知Unity刷新


            ///可新增BudSetting中场景
        }
    }
}