1. 程式人生 > >【unity】使用編輯器程式碼修改預設

【unity】使用編輯器程式碼修改預設

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System;
using Assets.Plugins.Scripts.Util;

public class CListMaskEditor : MonoBehaviour
{
  //  [MenuItem("MC/CListMaskHandle(Don't click! Don't click! Don't click!)")]
    static void SecondPanelHandle()
    {
        string uiDirPath = Application.dataPath + "/OriginalResource/UI/";
        foreach (string path in Directory.GetDirectories(uiDirPath))
        {
            string modulePath = path.Substring(path.IndexOf("Assets", StringComparison.Ordinal));
            if (path.Contains("/Textures")) //獨立貼圖
            {

            }
            else// if(path.Contains("MingUI")) //模組
            {
                Debug.Log(modulePath);
                string[] assets = Directory.GetFiles(modulePath + "/Prefabs/");
                foreach (string asset in assets)
                {
                    GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(asset);
                    if (go != null)
                    {
                        GameObject obj = GameObject.Instantiate(go) as GameObject;
                        //找到需要的obj
                        CList[] cLists = obj.GetComponentsInChildren<CList>();
                        if (cLists != null)
                        {
                            foreach (CList clist in cLists)
                            {
                                //增加一個CSprite
                                CSprite maskCS = ComponentUtil.CreateCSprite(clist.gameObject, "maskCS", true);
                                maskCS.Path = "Widget.bg_mask2";
                                //設定大小和位置
                                //設定軸點和錨點
                                maskCS.rectTransform.anchorMax = new Vector2(1, 0);
                                maskCS.rectTransform.anchorMin = new Vector2(0, 0);
                                maskCS.rectTransform.pivot = new Vector2(0.5f, 0.5f);
                                //設定高度
                                maskCS.rectTransform.sizeDelta = new Vector2(0.0f, 40.0f);
                                //設定位置
                                maskCS.rectTransform.anchoredPosition = new Vector2(0, 20);
                                //替換目標預設
                                PrefabUtility.ReplacePrefab(obj, go, ReplacePrefabOptions.ReplaceNameBased);
                            }
                            //銷燬例項化的obj
                            GameObject.DestroyImmediate(obj);
                        }

                    }
                }
            }
        }
        //儲存修改和重新整理
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.Log("Done");
    }
}