批量替换预设sprite

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

public class ChangeAtlas : MonoBehaviour
{

    [MenuItem("Wpw/Assets/ChangeAll")]
    static void Test_Compoent()
    {
        Rect rc = new Rect(5, 5, 400, 200);
        ChangeAtlasWindow mcw = EditorWindow.GetWindowWithRect<ChangeAtlasWindow>(rc, false, "新建地图", true);
    }
}
public class ChangeAtlasWindow : EditorWindow
{

    public Sprite oldSprite;
    public Sprite newSprite;
    public List<Image> imageList;
    string fullPath = "Assets/Resources" + "/";
    private void OnGUI()
    {
        GUI.color = Color.white;
        oldSprite = EditorGUILayout.ObjectField("旧图", oldSprite, typeof(Sprite), false) as Sprite;
        newSprite = EditorGUILayout.ObjectField("新图", newSprite, typeof(Sprite), false) as Sprite;
        if (oldSprite != null && newSprite != null)
        {
            if (GUILayout.Button("更改"))
            {
                ChangeAtlas();
            }
        }
    }
    /// <summary>
    /// 注意,如果不是全部都要替换成新图集内图片,慎用!
    /// </summary>
    private void ChangeAtlas()
    {
        imageList = new List<Image>();
        LoadAssetByAllChildren(ref imageList, fullPath);
        for (int i = 0; i < imageList.Count; i++)
        {
            if (imageList[i].sprite == oldSprite)
            {
                imageList[i].sprite = newSprite;
            }
        }
    }
    /// <summary>
    /// 读取组件列表
    /// </summary>
    /// <param name="parList"></param>
    /// <param name="fullPath"></param>
    private void LoadAssetByAllChildren(ref List<Image> parList, string fullPath)
    {
        //获取指定路径下面的所有资源文件  
        if (Directory.Exists(fullPath))
        {
            DirectoryInfo direction = new DirectoryInfo(fullPath);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
            //Debug.Log(files.Length);
            for (int i = 0; i < files.Length; i++)
            {
                //Debug.Log("i === " + i.ToString() + " == " + files[i].Name);
                if (files[i].Name.EndsWith(".meta"))
                {
                    continue;
                }
                if (files[i].Name.EndsWith(".prefab"))
                {
                    string ss = files[i].FullName.Replace("D:\\client\\ClientMainProject_code\\Assets\\", "");
                    GameObject gobj = AssetDatabase.LoadAssetAtPath("Assets/" + ss, typeof(GameObject)) as GameObject;
                    List<Image> localImageList = new List<Image>();
                    gobj.GetComponentsInChildren<Image>(true, localImageList);
                    for (int j = 0; j < localImageList.Count; j++)
                    {
                        if (localImageList[j] != null)
                        {
                            parList.Add(localImageList[j]);
                        }
                    }
                }
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值