unity 代码拆分图集

1、 将分割代码放入Editor中

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

public class Tools : EditorWindow
{
    [MenuItem("Tools/openWindow")]
    public static void createWindow()
    {
        Tools window = EditorWindow.GetWindow<Tools>("设置位置");
        window.Show();
        window.minSize = new Vector2(200, 300);
    }

    private void OnGUI()
    {
       
        if (GUILayout.Button("分割图集"))
        {
            ProcessToSprite();
        }
        
    }
    static SpriteMetaData[] spriteArr;

    static void getSprite()
    {
        Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象

        string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称

        string path = rootPath + "/" + image.name + ".png";//图片路径名称

        TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口

        spriteArr = texImp.spritesheet;
        for (int i = 0; i < spriteArr.Length; i++)
        {
            MonoBehaviour.print(spriteArr[i].rect);
        }
    }

    static void setSprite()
    {
        Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象

        string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称

        string path = rootPath + "/" + image.name + ".png";//图片路径名称

        TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口
        texImp.spritesheet = spriteArr;
    }
	
	[MenuItem("Tools/ProcessToSprite #&C")]
    static void ProcessToSprite()
    {
        Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象

        string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称

        string path = rootPath + "/" + image.name + ".png";//图片路径名称

        TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口

        AssetDatabase.CreateFolder(rootPath, image.name);//创建文件夹
        MonoBehaviour.print(path);
                                                         // texImp.spritesheet = 
        
        foreach (SpriteMetaData metaData in texImp.spritesheet)//遍历小图集
        {
            Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);

            for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)//Y轴像素
            {
                for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)
                    myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, image.GetPixel(x, y));
            }

            if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24)
            {
                Texture2D newTexture = new Texture2D(myimage.width, myimage.height);
                newTexture.SetPixels(myimage.GetPixels(0), 0);
                myimage = newTexture;
            }
            var pngData = myimage.EncodeToPNG();

            File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".png", pngData);
            // 刷新资源窗口界面
            AssetDatabase.Refresh();
        }
    }
    
}

2、选择图集,勾选模式及可编辑

3、点击Sprite Editor ,选择图片裁剪模式,对图片进行裁剪,Apply保存设置

4、选中图集图片,快捷键shift +alt +c 会在图集当前目录下生成对应的散图,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值