コード例 #1
0
    public static void RefreshText()
    {
        string[]          sdirs       = { "Assets/Art/UIPrefab", "Assets/Editor Default Resources" };
        var               asstIds     = AssetDatabase.FindAssets("t:Prefab", sdirs);
        List <GameObject> gameObjects = new List <GameObject>();

        foreach (var asstId in asstIds)
        {
            string path = AssetDatabase.GUIDToAssetPath(asstId);
            var    pfb  = AssetDatabase.LoadAssetAtPath <GameObject>(path);
            gameObjects.Add(pfb);
        }
        int           i     = 0;
        List <UIText> texts = new List <UIText>();

        foreach (var pfb in gameObjects)
        {
            foreach (var text in pfb.GetComponentsInChildren <MyText>(true))
            {
                bool       exist = false;
                GameObject go    = null;
                foreach (var mono in pfb.GetComponentsInChildren <MonoBehaviour>(true))
                {
                    foreach (var field in mono.GetType().GetFields())
                    {
                        var xx = mono.GetFieldByReflect(field.Name);
                        if (xx.IsNotNull() && xx.GetType() == typeof(MyText))
                        {
                            if (xx.As <MyText>() == text)
                            {
                                go    = mono.gameObject;
                                exist = true;
                                break;
                            }
                        }
                    }
                }
                if (!exist && text.GetComponent("TextAdapt") == null)
                {
                    texts.Add(GetText(true, i, pfb.name, text));
                }
                else
                {
                    texts.Add(GetText(false, i, go?.gameObject.name, text));
                }
                text.Index = i;
                i++;
            }
            try
            {
                PrefabUtility.SavePrefabAsset(pfb);
            }
            catch (Exception e)
            {
                Debug.LogWarning(e.Message);
            }
        }

        //excel文本

        ExcelTool excelTool = new ExcelTool(true);
        PropertyExcelTextManager propertyExcelTextManager = ScriptableObject.CreateInstance <PropertyExcelTextManager>();

        propertyExcelTextManager.PropertyExcelText = excelTool.CreatePropertyExcelTextArrayWithExcel(propertyExcelTextManager);
        List <UIText>     excelTexts = new List <UIText>();
        LanguageTranslate translate  = new LanguageTranslate();

        for (int j = 0; j < propertyExcelTextManager.PropertyExcelText.Length; j++)
        {
            excelTexts.Add(new UIText()
            {
                Fixed              = true,
                Index              = j,
                Name               = DataEncryptManager.StringDecoder(propertyExcelTextManager.PropertyExcelText[j].ConstKey),
                ParentName         = "Excel",
                SimplifiedChineses = new List <string>()
                {
                    translate.ConvertChinSimp(propertyExcelTextManager.PropertyExcelText[j].ConstValue)
                },
                TraditionalChineses = new List <string>()
                {
                    translate.ConvertChinTrad(propertyExcelTextManager.PropertyExcelText[j].ConstValue)
                },
            });
        }
        UITextManager textManager = new UITextManager
        {
            FixedText = texts,
            ExcelText = excelTexts
        };

        AssetDatabase.CreateAsset(textManager, $"{ExcelConfig.assetPath}UITextManager.asset");
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }