Unity Editor 替换 UISprite 引用 Altas

2017-10-17 雨辰 Unity3D

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

/// <summary>
/// 根据鼠标点中的对象批量修改所有精灵 图集,脚本位于Editor文件夹
/// </summary>
public class ChangeAltasWindow : EditorWindow
{
    private static bool isChangAltas = false;
    private static int altCount = 1;

    public static List<UIAtlas> atles = new List<UIAtlas>();
    [MenuItem("Window/修改图集引用")]
    private static void ShowWindow()
    {
        ChangeAltasWindow cw = GetWindow<ChangeAltasWindow>(true, "修改图集引用");

        cw.minSize = new Vector2(310, 200);
        cw.maxSize = new Vector2(310, 300);

        //重置 面板
        atles.Clear();
        altCount = 1;
    }

    private void OnGUI()
    {
        GUILayout.Space(5);
        isChangAltas = EditorGUILayout.Toggle("是否改变当前图集", isChangAltas);
        GUILayout.Space(5);

        if (isChangAltas)
        {
            altCount = EditorGUILayout.IntField("修改图集数量", altCount);
            for (int i = 0; i < altCount; i++)
            {
                atles.Add(new UIAtlas());
                atles[i] = (UIAtlas)EditorGUILayout.ObjectField("目标图集" + i.ToString(), atles[i], typeof(UIAtlas), true);
            }
        }

        GUILayout.Space(5);
        //创建确认按钮
        if (GUILayout.Button("确认替换", GUILayout.Height(30), GUILayout.Width(300)))
        {
            Change();
        }
    }

    public static void Change() 
    {
        if (!isChangAltas)
        {
            return;
        }
        if (atles.Count == 0)
        {
            return;
        }

        Object[] sprites = Selection.GetFiltered(typeof(UISprite), SelectionMode.Deep);
        foreach (Object item in sprites)
        {
            UISprite sprite = (UISprite)item;
            if (sprite.atlas != null)
            {
                foreach (UIAtlas atl in atles)
                {
                    if (atl != null && sprite.atlas.name == atl.name)
                    {
                        sprite.atlas = atl;
                        Debug.Log(">>>>>>>>UIAtlas name <" + sprite.atlas.name + "> sprite name <" + sprite.name +">" );
                        break;
                    }
                }
            }

            EditorUtility.SetDirty(item); //重要(有点像应用设置的意思)
        }
    }
}

标签: Unity3D-Editer

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号