新项目使用Unity5.X,遇到了一些问题,其中就有Sprite的管理更新问题,查了一些资料,Mono推荐的是转为Prefab处理。

看了一些国外同行的处理方法,分析了一个编辑器插件脚本。学到了一些技巧,使用的话依然放在Editor目录下。总结在这里吧:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Linq;

public class SpriteToPrefab
{
    /// <summary>
    /// Creates Sprites To Prefab
    /// </summary>
    [MenuItem("Assets/Create/Sprite转Prefab", false, 11)]
    public static void SelectObjectToPrefabs()
    {
        //是否允许独立文件夹;
        bool isSoloFolder = EditorUtility.DisplayDialog("创建选择?", "是否单个Sprite创建独立文件夹?", "独立", "共享");
        for (int i = 0; i < Selection.objects.Length; i++)
        {
            //获得选择对象路径;
            string spritePath = AssetDatabase.GetAssetPath(Selection.objects[i]);
            //所有子Sprite对象;
            Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(spritePath).OfType<Sprite>().ToArray();
            string[] splitSpritePath = spritePath.Split(new char[] { '/' });
            //文件夹路径 通过完整路径再去掉文件名称即可;
            string fullFolderPath = Inset(spritePath, 0, splitSpritePath[splitSpritePath.Length - 1].Length + 1) + "/" + Selection.objects[i].name;
            //同名文件夹;
            string folderName = Selection.objects[i].name;
            string adjFolderPath = InsetFromEnd(fullFolderPath, Selection.objects[i].name.Length + 1);
            //验证路径;
            if (!AssetDatabase.IsValidFolder(fullFolderPath))
            {
                AssetDatabase.CreateFolder(adjFolderPath, folderName);
            }

            //创建对象;
            GameObject gameObject = new GameObject();
            //添加SpriteRenderer组件;
            SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
            for (int j = 0; j < sprites.Length; j++)
            {   //进度条;
                string pgTitle = (i + 1).ToString() + "/" + Selection.objects.Length + " 开始创建Prefab";
                string info = "Prefab: " + j + "->" + sprites[j].name;
                float nowProgress = (float) j/(float) sprites.Length;
                EditorUtility.DisplayProgressBar(pgTitle,info ,nowProgress );
                //对象名称;
                gameObject.name = sprites[j].name;
                //绑定具体Sprite;
                spriteRenderer.sprite = sprites[j];
                //判断保存路径;
                string savePath = isSoloFolder ? fullFolderPath + "/" + sprites[j].name + "/" + sprites[j].name + ".prefab" : fullFolderPath + "/" + sprites[j].name + ".prefab";

                if (isSoloFolder)
                {   //创建单个Sprite独立的文件夹;
                    if (!AssetDatabase.IsValidFolder(fullFolderPath + "/" + sprites[j].name))
                    {
                        AssetDatabase.CreateFolder(fullFolderPath, sprites[j].name);
                    }
                }
                //生成预制体;
                PrefabUtility.CreatePrefab(savePath, gameObject);
            }
            GameObject.DestroyImmediate(gameObject);
        }
        //释放进度条;
        EditorUtility.ClearProgressBar();

    }

    /// <summary>
    /// 截取路径
    /// </summary>
    /// <param name="path"></param>
    /// <param name="leftIn">左起点</param>
    /// <param name="rightIn">右起点</param>
    /// <returns></returns>
    public static string Inset(string path, int leftIn, int rightIn)
    {
        return path.Substring(leftIn, path.Length - rightIn - leftIn);
    }

    /// <summary>
    /// 截取路径
    /// </summary>
    /// <param name="path"></param>
    /// <param name="inset"></param>
    /// <returns></returns>
    public static string InsetFromEnd(string path, int inset)
    {
        return path.Substring(0, path.Length - inset);
    }

}

使用方法如下:

关于subString函数:https://msdn.microsoft.com/zh-tw/library/aka44szs