using System.Collections;  
 | 
using System.Collections.Generic;  
 | 
using UnityEngine;  
 | 
using UnityEditor;  
 | 
using UnityEngine.UI;  
 | 
using System.IO;  
 | 
using vnxbqy.UI;  
 | 
using System;  
 | 
  
 | 
public static class FormatPrefabTool  
 | 
{  
 | 
    enum FormatPrefabType  
 | 
    {  
 | 
        ItemCell,  
 | 
        FunctionButton,  
 | 
    }  
 | 
  
 | 
    [SerializeField] static FormatPrefabType s_FormatPrefabType = FormatPrefabType.ItemCell;  
 | 
  
 | 
    static Vector2 s_ScrollPosition = Vector2.zero;  
 | 
  
 | 
    public static void OnGUI()  
 | 
    {  
 | 
        ToolsHelper.DisplayFolderPath();  
 | 
        s_FormatPrefabType = (FormatPrefabType)EditorGUILayout.EnumPopup(s_FormatPrefabType, GUILayout.Width(100));  
 | 
  
 | 
        switch (s_FormatPrefabType)  
 | 
        {  
 | 
            case FormatPrefabType.ItemCell:  
 | 
                DisplayItemCell();  
 | 
                break;  
 | 
            case FormatPrefabType.FunctionButton:  
 | 
                DisplayFuncButton();  
 | 
                break;  
 | 
        }  
 | 
    }  
 | 
  
 | 
    #region ItemCell  
 | 
    public enum ItemCellComType  
 | 
    {  
 | 
        None,  
 | 
        ItemBG,  
 | 
        ItemIcon,  
 | 
        Lock,  
 | 
        Count,  
 | 
        State,  
 | 
        Star,  
 | 
        Reduce,  
 | 
    }  
 | 
  
 | 
    public class ItemCellCom  
 | 
    {  
 | 
        public ItemCellComType type;  
 | 
        public RectTransform transform;  
 | 
        public Image image;  
 | 
        public Text text;  
 | 
        public List<Image> stars = new List<Image>();  
 | 
  
 | 
        public ItemCellCom(ItemCellComType _type, RectTransform _trans)  
 | 
        {  
 | 
            type = _type;  
 | 
            transform = _trans;  
 | 
            switch (type)  
 | 
            {  
 | 
                case ItemCellComType.Reduce:  
 | 
                case ItemCellComType.Lock:  
 | 
                case ItemCellComType.ItemIcon:  
 | 
                case ItemCellComType.ItemBG:  
 | 
                case ItemCellComType.State:  
 | 
                    image = _trans.GetComponent<Image>();  
 | 
                    break;  
 | 
                case ItemCellComType.Count:  
 | 
                    text = _trans.GetComponent<Text>();  
 | 
                    break;  
 | 
                case ItemCellComType.Star:  
 | 
                    foreach (RectTransform _rect in _trans)  
 | 
                    {  
 | 
                        stars.Add(_rect.GetComponent<Image>());  
 | 
                    }  
 | 
                    break;  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public class FormatItemCell  
 | 
    {  
 | 
        public int Size = 84;  
 | 
        public GameObject template;  
 | 
        public List<ItemCellCom> ItemCellComs = new List<ItemCellCom>();  
 | 
        public List<RectTransform> ItemTrans = new List<RectTransform>();  
 | 
        public List<ItemCellComType> ItemComTypes = new List<ItemCellComType>();  
 | 
    }  
 | 
  
 | 
    static List<FormatItemCell> formatItems = new List<FormatItemCell>();  
 | 
    static void DisplayItemCell()  
 | 
    {  
 | 
        GUILayout.BeginHorizontal();  
 | 
        if (GUILayout.Button("添加模板Item"))  
 | 
        {  
 | 
            formatItems.Add(new FormatItemCell());  
 | 
        }  
 | 
        GUILayout.FlexibleSpace();  
 | 
        GUILayout.EndHorizontal();  
 | 
  
 | 
        s_ScrollPosition = GUILayout.BeginScrollView(s_ScrollPosition, GUILayout.Height(300));  
 | 
        for (int i = 0; i < formatItems.Count; i++)  
 | 
        {  
 | 
            var _formatItem = formatItems[i];  
 | 
            GUILayout.BeginHorizontal();  
 | 
            var _object = EditorGUILayout.ObjectField(_formatItem.template, typeof(GameObject), false) as GameObject;  
 | 
            if (_object != _formatItem.template)  
 | 
            {  
 | 
                _formatItem.ItemCellComs.Clear();  
 | 
                _formatItem.template = _object;  
 | 
                _formatItem.ItemTrans.Clear();  
 | 
                _formatItem.ItemComTypes.Clear();  
 | 
                if (_formatItem.template != null)  
 | 
                {  
 | 
                    FindChild(_formatItem, _formatItem.template.transform);  
 | 
                }  
 | 
            }  
 | 
            GUILayout.Label("Item Size:");  
 | 
            _formatItem.Size = EditorGUILayout.IntField(_formatItem.Size);  
 | 
            if (GUILayout.Button("-"))  
 | 
            {  
 | 
                formatItems.RemoveAt(i);  
 | 
                i--;  
 | 
                continue;  
 | 
            }  
 | 
            GUILayout.FlexibleSpace();  
 | 
            GUILayout.EndHorizontal();  
 | 
  
 | 
            for (int k = 0; k < _formatItem.ItemTrans.Count; k++)  
 | 
            {  
 | 
                GUILayout.BeginHorizontal();  
 | 
                EditorGUILayout.ObjectField(_formatItem.ItemTrans[k], typeof(RectTransform), false);  
 | 
                var _type = (ItemCellComType)EditorGUILayout.EnumPopup(_formatItem.ItemComTypes[k]);  
 | 
                if (_type != _formatItem.ItemComTypes[k])  
 | 
                {  
 | 
                    _formatItem.ItemCellComs.RemoveAll((x) =>  
 | 
                    {  
 | 
                        return x.type == _formatItem.ItemComTypes[k];  
 | 
                    });  
 | 
                    var _item = _formatItem.ItemCellComs.Find((x) =>  
 | 
                    {  
 | 
                        return x.type == _type;  
 | 
                    });  
 | 
                    if (_item == null)  
 | 
                    {  
 | 
                        _formatItem.ItemComTypes[k] = _type;  
 | 
                        _item = new ItemCellCom(_type, _formatItem.ItemTrans[k]);  
 | 
                        _formatItem.ItemCellComs.Add(_item);  
 | 
                    }  
 | 
                    else  
 | 
                    {  
 | 
                        _formatItem.ItemComTypes[k] = ItemCellComType.None;  
 | 
                    }  
 | 
                }  
 | 
                GUILayout.FlexibleSpace();  
 | 
                GUILayout.EndHorizontal();  
 | 
            }  
 | 
            GUILayout.Space(10);  
 | 
        }  
 | 
        GUILayout.EndScrollView();  
 | 
  
 | 
  
 | 
  
 | 
        if (GUILayout.Button("开始替换"))  
 | 
        {  
 | 
            if (ToolsHelper.folderPath != string.Empty && formatItems.Count > 0)  
 | 
            {  
 | 
                StartFormatPrefab();  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
  
 | 
    static void FindChild(FormatItemCell _format, Transform _parent)  
 | 
    {  
 | 
        foreach (RectTransform _tran in _parent)  
 | 
        {  
 | 
            _format.ItemTrans.Add(_tran);  
 | 
            _format.ItemComTypes.Add(ItemCellComType.None);  
 | 
            FindChild(_format, _tran);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    static void StartFormatPrefab()  
 | 
    {  
 | 
        var _files = Directory.GetFiles(ToolsHelper.folderPath, "*.prefab", SearchOption.AllDirectories);  
 | 
        if (_files == null || _files.Length == 0)  
 | 
        {  
 | 
            return;  
 | 
        }  
 | 
        var _index = 0;  
 | 
        EditorApplication.update = delegate ()  
 | 
        {  
 | 
            var _file = _files[_index];  
 | 
            GameObject _prefab = AssetDatabase.LoadAssetAtPath(ToolsHelper.GetRelativeAssetsPath(_file), typeof(GameObject)) as GameObject;  
 | 
            if (_prefab != null)  
 | 
            {  
 | 
                bool _change = false;  
 | 
                var _itemCells = _prefab.GetComponentsInChildren<ItemCell>(true);  
 | 
                if (_itemCells != null && _itemCells.Length > 0)  
 | 
                {  
 | 
                    Replace(_itemCells);  
 | 
                    _change = true;  
 | 
                }  
 | 
                var _itemBases = _prefab.GetComponentsInChildren<CommonItemBaisc>(true);  
 | 
                if (_itemBases != null && _itemBases.Length > 0)  
 | 
                {  
 | 
                    Replace(_itemBases);  
 | 
                    _change = true;  
 | 
                }  
 | 
                var _itemBehaviours = _prefab.GetComponentsInChildren<ItemBehaviour>(true);  
 | 
                if (_itemBases != null && _itemBases.Length > 0)  
 | 
                {  
 | 
                    Replace(_itemBehaviours);  
 | 
                    _change = true;  
 | 
                }  
 | 
                if (_change)  
 | 
                {  
 | 
                    EditorUtility.SetDirty(_prefab);  
 | 
                }  
 | 
            }  
 | 
            bool _isCancel = EditorUtility.DisplayCancelableProgressBar("替换预制体Item",  
 | 
                StringUtility.Contact(_index, "/", _files.Length), (float)_index / _files.Length);  
 | 
            _index++;  
 | 
            if (_isCancel || _index >= _files.Length)  
 | 
            {  
 | 
                EditorUtility.ClearProgressBar();  
 | 
                EditorApplication.update = null;  
 | 
                AssetDatabase.SaveAssets();  
 | 
                AssetDatabase.Refresh();  
 | 
                _index = 0;  
 | 
            }  
 | 
        };  
 | 
    }  
 | 
  
 | 
    static void Replace<T>(T[] _items) where T : MonoBehaviour  
 | 
    {  
 | 
        if (typeof(T) == typeof(ItemCell) || typeof(T) == typeof(CommonItemBaisc))  
 | 
        {  
 | 
            CommonItemBaisc[] _itemCells = _items as CommonItemBaisc[];  
 | 
            for (int i = 0; i < _itemCells.Length; i++)  
 | 
            {  
 | 
                var _item = _itemCells[i];  
 | 
                FormatItemCell _formatItem = null;  
 | 
                if (_item.bgIcon != null)  
 | 
                {  
 | 
                    int _size = (int)_item.bgIcon.rectTransform.sizeDelta.x;  
 | 
                    _formatItem = formatItems.Find((x) =>  
 | 
                    {  
 | 
                        return x.Size == _size;  
 | 
                    });  
 | 
                }  
 | 
                if (null == _formatItem)  
 | 
                {  
 | 
                    continue;  
 | 
                }  
 | 
                foreach (var _source in _formatItem.ItemCellComs)  
 | 
                {  
 | 
                    switch (_source.type)  
 | 
                    {  
 | 
                        case ItemCellComType.ItemBG:  
 | 
                            if (_item.bgIcon != null)  
 | 
                            {  
 | 
                                PasteImage(_item.bgIcon, _source.image);  
 | 
                            }  
 | 
                            break;  
 | 
                        case ItemCellComType.ItemIcon:  
 | 
                            if (_item.itemIcon != null)  
 | 
                            {  
 | 
                                PasteImage(_item.itemIcon, _source.image);  
 | 
                            }  
 | 
                            break;  
 | 
                        case ItemCellComType.State:  
 | 
                            if (_item.stateIcon != null)  
 | 
                            {  
 | 
                                PasteImage(_item.stateIcon, _source.image);  
 | 
                            }  
 | 
                            break;  
 | 
                        case ItemCellComType.Reduce:  
 | 
                            if (_item is ItemCell)  
 | 
                            {  
 | 
                                var _itemCell = _item as ItemCell;  
 | 
                                if (_itemCell.reducebtn != null)  
 | 
                                {  
 | 
                                    PasteImage(_itemCell.reducebtn.image, _source.image);  
 | 
                                }  
 | 
                            }  
 | 
                            break;  
 | 
                        case ItemCellComType.Count:  
 | 
                            if (_item.countText != null)  
 | 
                            {  
 | 
                                PasteText(_item.countText, _source.text);  
 | 
                            }  
 | 
                            break;  
 | 
                    }  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
        else if (typeof(T) == typeof(ItemBehaviour))  
 | 
        {  
 | 
            ItemBehaviour[] _itemCells = _items as ItemBehaviour[];  
 | 
            for (int i = 0; i < _itemCells.Length; i++)  
 | 
            {  
 | 
                var _item = _itemCells[i];  
 | 
                FormatItemCell _formatItem = null;  
 | 
                if (_item.backGround != null)  
 | 
                {  
 | 
                    int _size = (int)_item.backGround.rectTransform.sizeDelta.x;  
 | 
                    _formatItem = formatItems.Find((x) =>  
 | 
                    {  
 | 
                        return x.Size == _size;  
 | 
                    });  
 | 
                }  
 | 
                if (null == _formatItem)  
 | 
                {  
 | 
                    continue;  
 | 
                }  
 | 
                foreach (var _source in _formatItem.ItemCellComs)  
 | 
                {  
 | 
                    switch (_source.type)  
 | 
                    {  
 | 
                        case ItemCellComType.ItemBG:  
 | 
                            if (_item.backGround != null)  
 | 
                            {  
 | 
                                PasteImage(_item.backGround, _source.image);  
 | 
                            }  
 | 
                            break;  
 | 
                        case ItemCellComType.ItemIcon:  
 | 
                            if (_item.icon != null)  
 | 
                            {  
 | 
                                PasteImage(_item.icon, _source.image);  
 | 
                            }  
 | 
                            break;  
 | 
                        case ItemCellComType.Count:  
 | 
                            if (_item.count != null)  
 | 
                            {  
 | 
                                PasteText(_item.count, _source.text);  
 | 
                            }  
 | 
                            break;  
 | 
                    }  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
  
 | 
    static void PasteImage(Image _target, Image _paste)  
 | 
    {  
 | 
        _target.sprite = _paste.sprite;  
 | 
        _target.color = _paste.color;  
 | 
        _target.raycastTarget = _paste.raycastTarget;  
 | 
        PasteRectTransform(_target.rectTransform, _paste.rectTransform);  
 | 
    }  
 | 
  
 | 
    static void PasteText(Text _target, Text _paste)  
 | 
    {  
 | 
        _target.color = _paste.color;  
 | 
        _target.fontSize = _paste.fontSize;  
 | 
        _target.font = _paste.font;  
 | 
        _target.raycastTarget = _paste.raycastTarget;  
 | 
        _target.alignment = _paste.alignment;  
 | 
        _target.horizontalOverflow = _paste.horizontalOverflow;  
 | 
        _target.verticalOverflow = _paste.verticalOverflow;  
 | 
        PasteRectTransform(_target.rectTransform, _paste.rectTransform);  
 | 
    }  
 | 
  
 | 
    static void PasteRectTransform(RectTransform _target, RectTransform _paste)  
 | 
    {  
 | 
        _target.pivot = _paste.pivot;  
 | 
        _target.anchorMax = _paste.anchorMax;  
 | 
        _target.anchorMin = _paste.anchorMin;  
 | 
        _target.sizeDelta = _paste.sizeDelta;  
 | 
        _target.anchoredPosition = _paste.anchoredPosition;  
 | 
    }  
 | 
    #endregion  
 | 
  
 | 
    #region FunctionButton  
 | 
    static Sprite m_FuncButtonLockIcon;  
 | 
    private static void DisplayFuncButton()  
 | 
    {  
 | 
        GUILayout.BeginHorizontal();  
 | 
        GUILayout.Label("替换锁Icon:");  
 | 
        m_FuncButtonLockIcon = EditorGUILayout.ObjectField(m_FuncButtonLockIcon, typeof(Sprite), false) as Sprite;  
 | 
        GUILayout.FlexibleSpace();  
 | 
        GUILayout.EndHorizontal();  
 | 
        if (GUILayout.Button("开始替换"))  
 | 
        {  
 | 
            if (ToolsHelper.folderPath != string.Empty)  
 | 
            {  
 | 
                StartFormatFuncButton();  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
  
 | 
    private static void StartFormatFuncButton()  
 | 
    {  
 | 
        var _files = Directory.GetFiles(ToolsHelper.folderPath, "*.prefab", SearchOption.AllDirectories);  
 | 
        if (_files == null || _files.Length == 0)  
 | 
        {  
 | 
            return;  
 | 
        }  
 | 
        var _index = 0;  
 | 
        EditorApplication.update = delegate ()  
 | 
        {  
 | 
            var _file = _files[_index];  
 | 
            GameObject _prefab = AssetDatabase.LoadAssetAtPath(ToolsHelper.GetRelativeAssetsPath(_file), typeof(GameObject)) as GameObject;  
 | 
            if (_prefab != null)  
 | 
            {  
 | 
                try  
 | 
                {  
 | 
                    var _copy = PrefabUtility.InstantiatePrefab(_prefab) as GameObject;  
 | 
                    if (_copy != null)  
 | 
                    {  
 | 
                        bool _change = false;  
 | 
                        var _funcbuttons = _copy.GetComponentsInChildren<FunctionButton>(true);  
 | 
                        if (_funcbuttons != null && _funcbuttons.Length > 0)  
 | 
                        {  
 | 
                            for (int i = 0; i < _funcbuttons.Length; i++)  
 | 
                            {  
 | 
                                var _funcButton = _funcbuttons[i];  
 | 
                                if (_funcButton.alternativeConfig.name == "FunctionButton_Version4")  
 | 
                                {  
 | 
                                    GameObject _obj = null;  
 | 
                                    if (_funcButton.transform.Find("Container_SortArea") == null)  
 | 
                                    {  
 | 
                                        _obj = new GameObject("Container_SortArea");  
 | 
                                    }  
 | 
                                    else  
 | 
                                    {  
 | 
                                        _obj = _funcButton.transform.Find("Container_SortArea").gameObject;  
 | 
                                    }  
 | 
                                    var _layout = _obj.AddMissingComponent<HorizontalLayoutGroup>();  
 | 
                                    _layout.childAlignment = TextAnchor.MiddleCenter;  
 | 
                                    _layout.childControlWidth = true;  
 | 
                                    _layout.childControlHeight = false;  
 | 
                                    _layout.childForceExpandHeight = false;  
 | 
                                    _layout.childForceExpandWidth = false;  
 | 
                                    _obj.transform.SetParent(_funcButton.transform);  
 | 
                                    var _rect = _obj.transform as RectTransform;  
 | 
                                    _rect.localScale = Vector3.one;  
 | 
                                    _rect.anchorMin = Vector2.zero;  
 | 
                                    _rect.anchorMax = Vector2.one;  
 | 
                                    _rect.offsetMax = Vector2.zero;  
 | 
                                    _rect.offsetMin = Vector2.zero;  
 | 
                                    _rect.pivot = Vector2.one / 2;  
 | 
                                    if (_funcButton.locked != null)  
 | 
                                    {  
 | 
                                        _funcButton.locked.SetParent(_rect);  
 | 
                                        var _image = _funcButton.locked.GetComponent<Image>();  
 | 
                                        if (_image != null && m_FuncButtonLockIcon != null)  
 | 
                                        {  
 | 
                                            _image.sprite = m_FuncButtonLockIcon;  
 | 
                                            _image.SetNativeSize();  
 | 
                                        }  
 | 
                                    }  
 | 
                                    if (_funcButton.title != null)  
 | 
                                    {  
 | 
                                        _funcButton.title.transform.SetParent(_rect);  
 | 
                                        _funcButton.title.rectTransform.sizeDelta = (_funcButton.transform as RectTransform).sizeDelta;  
 | 
                                    }  
 | 
                                    _change = true;  
 | 
                                }  
 | 
                            }  
 | 
                        }  
 | 
                        if (_change)  
 | 
                        {  
 | 
                            PrefabUtility.ReplacePrefab(_copy, _prefab, ReplacePrefabOptions.ConnectToPrefab);  
 | 
                        }  
 | 
                        GameObject.DestroyImmediate(_copy);  
 | 
                    }  
 | 
                }  
 | 
                catch (Exception e)  
 | 
                {  
 | 
                    Debug.Log(e.Message);  
 | 
                }  
 | 
                  
 | 
            }  
 | 
            bool _isCancel = EditorUtility.DisplayCancelableProgressBar("替换预制体",  
 | 
                StringUtility.Contact(_index, "/", _files.Length), (float)_index / _files.Length);  
 | 
            _index++;  
 | 
            if (_isCancel || _index >= _files.Length)  
 | 
            {  
 | 
                EditorUtility.ClearProgressBar();  
 | 
                EditorApplication.update = null;  
 | 
                AssetDatabase.SaveAssets();  
 | 
                AssetDatabase.Refresh();  
 | 
                _index = 0;  
 | 
            }  
 | 
        };  
 | 
    }  
 | 
    #endregion  
 | 
  
 | 
}  
 |