批量生成圖片預製躰,并且保留當前文件目錄

因爲項目中要用到一個批量生成預製躰的工具給策劃用,所以就把這個工作交給我了,不多說了,直接上代碼,第一次寫工具,也是第一次寫博客,寫的不好,請多多指教!


public class BatchCreatePrefab : EditorWindow
{
[MenuItem(“Tool/Common/生成预制”)]
private static void PrefabWrapTool()
{
GetWindow().Show();
}

    private int m_index = 0;
    private static string[] s_outputDirectories;

    private void OnEnable()
    {
        Selection.selectionChanged += Repaint;
    }

    private void OnDisable()
    {
        Selection.selectionChanged -= Repaint;
    }

    /// <summary>
    /// 刷新目录
    /// </summary>
    private void UpdateDirectory()
    {
        s_outputDirectories = Directory.GetDirectories("Assets\\Resources", "*", SearchOption.AllDirectories);
        System.Array.Sort(s_outputDirectories);
        Repaint();
    }
    private static string RemoveDirectory = "Assets";
    private void OnGUI()
    {
        if (s_outputDirectories == null)
        {
            UpdateDirectory();
        }
        EditorGUILayout.BeginHorizontal();
        float defaultLabelWidth = EditorGUIUtility.labelWidth;
        EditorGUIUtility.labelWidth = 60;
        EditorGUILayout.PrefixLabel("输出目录");
        m_index = EditorGUILayout.Popup(m_index, s_outputDirectories);
        EditorGUIUtility.labelWidth = defaultLabelWidth;
        if (GUILayout.Button("刷新", GUILayout.MaxWidth(40)))
        {
            UpdateDirectory();
        }
        EditorGUILayout.EndHorizontal();
        //忽略层级目录
        EditorGUILayout.BeginHorizontal();
        EditorGUIUtility.labelWidth = 100;
        EditorGUILayout.PrefixLabel("忽略层级目录:");
        RemoveDirectory = EditorGUILayout.TextField(RemoveDirectory);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space();

        //选中生成数量
        int selectionCount = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets).Length;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.Space();
        //绘制一个生成按钮
        if (GUILayout.Button("生成(" + selectionCount + ")", GUILayout.Height(20), GUILayout.MaxWidth(100)))
        {
            CreateAllSelectionPrefab(s_outputDirectories[m_index]);

        }
        EditorGUILayout.EndHorizontal();
    }

    private static void CreateAllSelectionPrefab(string outputPath)
    {
        Object[] allSelectedTextuers = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets);
        if (allSelectedTextuers.Length == 0)
        {
            Debug.LogWarning("未选中物体");
            return;
        }

        GameObject spriteObject = new GameObject();
        SpriteRenderer spriteRenderer = spriteObject.AddComponent<SpriteRenderer>();


        for (int i = 0; i < allSelectedTextuers.Length; i++)
        {
            string path = AssetDatabase.GetAssetPath(allSelectedTextuers[i]).Replace("/", "\\");
            string replaceString = path.Replace("/", "\\");
            if (!Directory.Exists(RemoveDirectory))
            {
                Object.DestroyImmediate(spriteObject);
                Debug.LogError("忽略层级目录错误");
                return;
            }
            string replaceDirectory = replaceString.Replace(RemoveDirectory, null);//替换字符串
            int lastCharIndex = replaceDirectory.LastIndexOf("\\");//得到物体的下标
            string removeString = replaceDirectory.Remove(lastCharIndex, replaceDirectory.Length - lastCharIndex);
            string finalOutputPath = outputPath + removeString;
            Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
            spriteRenderer.sprite = sprite;
            spriteObject.name = allSelectedTextuers[i].name;
            CreateSpritePrefab(spriteObject, finalOutputPath);
        }
        Object.DestroyImmediate(spriteObject);
        Debug.Log("生成成功");
    }

    private static void CreateSpritePrefab(GameObject spriteObject, string outputPath)
    {
        if (Directory.Exists(outputPath) == false)
        {
            Directory.CreateDirectory(outputPath);
            AssetDatabase.Refresh();
        }
        outputPath = outputPath.Replace("\\", "/");
        PrefabUtility.CreatePrefab(outputPath + "/" + spriteObject.name + ".prefab", spriteObject);
        AssetDatabase.Refresh();
    }

}

運行時窗口
下一篇
批量重命名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值