UnityEditor多种小工具

1.手动F1刷新运行
2.清空playerprefs
3.标记ab包
4.间隔点击运行
5.多语言prefab生成工具

public class ZSXMenu : EditorWindow
{
    [MenuItem("Helps/刷新并运行 _F1")]
    public static void ShowZHENTW_copyfff()
    {
        AssetDatabase.Refresh();
        EditorApplication.ExecuteMenuItem("Edit/Play");
    }
   [MenuItem("Helps/清空playerPrefs %&c")]
    public static void ClearPrefab()
    {
        Debug.Log("清空playerPrefs了");
        PlayerPrefs.DeleteAll();
    }
    
 [MenuItem("Assets/标记AB包 %#&B", false, 80)]
    public static void ShowRightMouse()
    {
        var tSelect = Selection.activeObject;
        if (tSelect is GameObject)
        {
            var tSed = tSelect as GameObject;
            var tPath = AssetDatabase.GetAssetPath(tSed);
            var tAsset = AssetImporter.GetAtPath(tPath);
            tAsset.assetBundleName = tSed.name + ".unity3d"; //设置Bundle文件的名称    
            tAsset.SaveAndReimport();
            AssetDatabase.Refresh();
            Selection.activeObject = null;
        }
        else
        {
            Debug.Log("非 GameObject");
        }
    }

    [MenuItem("Helps/刷登录")]
    public static void ShowRefresh()
    {
        GetWindow<LoginEditPlay>(false);
    }
        [MenuItem("Helps/多语言copy(删减版)")]
    public static void ShowZHENTW_Language()
    {
        GetWindow<GenerateStringLanguage>(false);
    }
}
//刷登录
public class LoginEditPlay : GetAssetGOWindow
{
    int mTimeCalcAdd = 0;
    bool mWorking = false;
    string mTimeLabel = "4";
    void OnGUI()
    {
        mTimeLabel = EditorGUILayout.TextField("开始时间:", mTimeLabel);
        GUILayout.Label("若点击了按钮,不要关闭此页面哦__" + GetNowTime());
        if (GUILayout.Button("运行或停止", GUILayout.Height(30)))
        {
            mTimeCalcAdd = 0;
            mWorking = !mWorking;     
        }
        if (mWorking)
        {
            var t = int.Parse(mTimeLabel);
            mTimeCalcAdd++;
            if (GetNowTime() % t == 0)
            {
                EditorApplication.ExecuteMenuItem("Edit/Play");
            }
            Repaint();//重绘
        }
    }
        long GetNowTime()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return ((long)ts.TotalSeconds);
        }
    }
    //多语言copy(删减版)
public class GenerateStringLanguage : GetAssetGOWindow
{
    private GameObject mTargetRoot;
    private Dictionary<string, string> mDicName = new Dictionary<string, string>();
    private string mForntName = "tc_:8";
    private bool mIsOverFlowHorizontal;
    
    void OnGUI()
    {
        GUILayout.Space(5);
        EditorGUILayout.LabelField("多语言流程:1.给要加的所有Text加上LanguageText.cs脚本\n2.把Prefab拖入视图中去\n3.点击生成列表\n4.随机生成下名字,用':'分隔开\n5.copy到USER_ZH.txt去\n6.选中刚才的Prefab右键(标记AB包)", GUILayout.Height(88));

        mTargetRoot = (GameObject)EditorGUILayout.ObjectField("Prefab", mTargetRoot, typeof(UnityEngine.Object), false);
        mIsOverFlowHorizontal = EditorGUILayout.Toggle("水平Overflow", mIsOverFlowHorizontal);

        if (GUILayout.Button("生成含(LanguageText.cs)的列表", GUILayout.Height(30)))
        {
            mDicName.Clear();
            if (mTargetRoot == null) { Debug.Log("prefab为null"); return; }
            var tLanguageTexts = mTargetRoot.GetComponentsInChildren<LanguageText>(true);
            for (int i = 0; i < tLanguageTexts.Length; i++)
            {
                var value = tLanguageTexts[i].mKey;
                mDicName[tLanguageTexts[i].GetComponent<Text>().text] = value;
            }
            if (mDicName.Count == 0)
                Debug.Log("并无 LanguageText.cs在" + mTargetRoot.name + "上");
            mForntName = mTargetRoot.name + ":8";
        }

        var keys = mDicName.Keys.ToList();
        var values = mDicName.Values.ToList();
        for (int i = 0; i < keys.Count; i++)
        {
            mDicName[keys[i]] = EditorGUILayout.TextField(keys[i], values[i]);
        }
        EditorGUILayout.BeginHorizontal();
        {
            mForntName = EditorGUILayout.TextField("随机名(前缀:数量)", mForntName);
            if (GUILayout.Button("-随机生成下名字-", GUILayout.Height(17)))
            {
                var tLength = mForntName.Split(':');
                if (tLength == null || tLength.Length != 2) return;
                for (int i = 0; i < keys.Count; i++)
                {
                    mDicName[keys[i]] = GetRandomString(tLength[0], int.Parse(tLength[1]));
                }
            }
        }
        EditorGUILayout.EndHorizontal();

        if (GUILayout.Button("生成下", GUILayout.Height(30)))
        {
            if (mTargetRoot == null) return;
            var tLanguageTexts = mTargetRoot.GetComponentsInChildren<LanguageText>(true);
            for (int i = 0; i < tLanguageTexts.Length; i++)
            {
                tLanguageTexts[i].mKey = mDicName[tLanguageTexts[i].GetComponent<Text>().text];

                tLanguageTexts[i].GetComponent<Text>().horizontalOverflow = mIsOverFlowHorizontal ? HorizontalWrapMode.Overflow : HorizontalWrapMode.Wrap;
            }
            var tBuild = GetCopyKeyValue(mTargetRoot);

            SaveNewPrefabs(new List<GameObject>() { mTargetRoot });
            if (string.IsNullOrEmpty(tBuild.ToString())) return;
            GUIUtility.systemCopyBuffer = tBuild.ToString();

            mDicName = new Dictionary<string, string>();
            mTargetRoot = null;
            AssetDatabase.Refresh();
        }
    }

    public string GetRandomString(string a, int length)
    {
        string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++)
        {
            int number = UnityEngine.Random.Range(0, 62);
            sb.Append(str.Substring(number, 1));
        }
        return a + sb.ToString();
    }

    public StringBuilder GetCopyKeyValue(GameObject tGo)
    {
        StringBuilder sb = new StringBuilder();
        if (tGo != null)
        {
            var tLanguageTexts = tGo.GetComponentsInChildren<LanguageText>(true);
            for (int i = 0; i < tLanguageTexts.Length; i++)
            {
                var key = tLanguageTexts[i].mKey;
                if (string.IsNullOrEmpty(key)) continue;
                var zhValue = tLanguageTexts[i].GetComponent<Text>().text;
                var lineStr = key + "\t" + zhValue;
                sb.AppendLine(lineStr);
            }
            Debug.Log(sb.ToString());
        }
        else
        {
            Debug.Log("未搜索到有");
        }
        return sb;
    }
}

public class GetAssetGOWindow : EditorWindow
{
    /// <summary> 也就是Inspector面板 的 Apply按钮   并且标记上AB包了</summary>
    public void SaveNewPrefabs(List<GameObject> pWillChanges)
    {
        if (pWillChanges == null || pWillChanges.Count == 0) return;
        for (int i = 0; i < pWillChanges.Count; i++)
        {
            var tPrefab = pWillChanges[i];
            var tOldHierarchyPrefab = PrefabUtility.InstantiatePrefab(tPrefab) as GameObject;
            var tPath = AssetDatabase.GetAssetPath(tPrefab);
            var tNewEmpty = PrefabUtility.CreateEmptyPrefab(tPath);
            var gameNew = PrefabUtility.ReplacePrefab(tOldHierarchyPrefab, tNewEmpty, ReplacePrefabOptions.ConnectToPrefab);
            GameObject.DestroyImmediate(tOldHierarchyPrefab);

            var tPathAB = AssetDatabase.GetAssetPath(gameNew);
            var tAssetAB = AssetImporter.GetAtPath(tPathAB);
            tAssetAB.assetBundleName = gameNew.name + ".unity3d";
            tAssetAB.SaveAndReimport();
        }
    }


    /// <summary>根据字符串得到GO           pFilter:Pefab或Script                pGoStr:prefabName </summary>
    public GameObject FindOneGo(string pFilter, string pGoStr)
    {
        var tGuids = AssetDatabase.FindAssets("t:" + pFilter + " " + pGoStr, new string[] { "Assets/Bundles/UI" });
        GameObject tGo = null;
        int i = 0;
        foreach (var guid in tGuids)
        {
            tGo = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)) as GameObject;
            i++;
        }
        if (i > 1) Debug.Log("提供的字符串中 搜索出来,有2个+(得到是最后一个)   建议prefab名字加个'_1' 得到字符串后改回来");
        return tGo;
    }
    public List<GameObject> GetPrefabs(string pPath)
    {
        if (string.IsNullOrEmpty(pPath)) pPath = "Assets/Bundles/UI";
        var guids = AssetDatabase.FindAssets("t:Prefab", new string[] { pPath });
        List<GameObject> tAllPrefab = new List<GameObject>();
        foreach (var item in guids)
        {
            var go = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(item)) as GameObject;
            tAllPrefab.Add(go);
        }
        return tAllPrefab;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值