利用unity3d分离图集

本期视频演讲稿
https://www.bilibili.com/video/bv1C54y1W7vn

最近正在进行U3D项目移植到h5平台,在移植UI图集时,需要将分离图集
那么如何将下图分离成小图?

最终移植效果游戏在线试玩http://www.4399.com/flash/217641.htm
在这里插入图片描述
分离后
在这里插入图片描述

操作步骤

在u3d中选择图片,设置为可读可写模式,模式为multiple
在这里插入图片描述
使用spriteEditor自动划分图集边界
在这里插入图片描述

在这里插入图片描述
完成
在这里插入图片描述
分离图集的工具

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

public static class ImageSlicer
{
    [MenuItem("Assets/ImageSlicer/Process to Sprite")]

    static void DoSingle()
    {
        Texture2D image = Selection.activeObject as Texture2D;//获取旋转的对象

        ProcessToSprite(image);
       
        AssetDatabase.Refresh();
    }


    static void ProcessToSprite(Texture2D image,string save_rootPath=null, string folderName = null)
    {

      
        //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.isReadable = false;

        if (save_rootPath!=null)
        {
         
            AssetDatabase.CreateFolder(save_rootPath, folderName);//创建文件夹
        }
        else
        AssetDatabase.CreateFolder(rootPath, image.name);//创建文件夹



        if (texImp.spritesheet.Length == 0)
        {
            if (save_rootPath != null)
            {
                File.Copy(path, save_rootPath + folderName  + "/" + image.name + ".PNG");
            }
            else
                File.Copy(path,rootPath + "/" + image.name+ "/" + image.name + ".PNG");
        }
        else
        {
            foreach (SpriteMetaData metaData in texImp.spritesheet)//遍历小图集
            {

                Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);

                //abc_0:(x:2.00, y:400.00, width:103.00, height:112.00)
                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));
                }


                //转换纹理到EncodeToPNG兼容格式
                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();


                //AssetDatabase.CreateAsset(myimage, rootPath + "/" + image.name + "/" + metaData.name + ".PNG");

                if (save_rootPath != null)
                {
                    File.WriteAllBytes(save_rootPath + folderName + "/" + metaData.name + ".PNG", pngData);
                }
                else
                    File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".PNG", pngData);
                // 刷新资源窗口界面

            }
        }
      
    }


    [MenuItem("Assets/ImageSlicer/Process to Sprites")]
    static void ProcessToSprites()
    {
        var d = Selection.objects;
        var url = Application.dataPath;
      
        foreach (Texture2D item in d)
        {
            ProcessToSprite(item, Application.dataPath, "/LayaTexture");
        }
        AssetDatabase.Refresh();
    }

    //[MenuItem("Assets/ImageSlicer/Process to Sprites Ugui Iamge")]
    //static void ProcessToSpriteUguiImage()
    //{

    //    // string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称
    //    string path = Application.dataPath + "/ExpUGUIIMAGE";// + image.name + ".PNG";//图片路径名称

    //    AssetDatabase.CreateFolder(Application.dataPath, path);//创建文件夹
    //    foreach (var item in Selection.objects)
    //    {

    //        var myimage = (item as GameObject).GetComponent<Image>().sprite;
    //        //转换纹理到EncodeToPNG兼容格式
    //        //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.texture.EncodeToPNG();

    //        //TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;

    //        //texImp.isReadable = true;
    //        //texImp.


    //        var pngData = myimage.texture.EncodeToPNG();

    //        //AssetDatabase.CreateAsset(myimage, rootPath + "/" + image.name + "/" + metaData.name + ".PNG");


    //        File.WriteAllBytes(path + "/" + myimage.name + ".png", pngData);
    //    }







    //    AssetDatabase.Refresh();
    //}
}

本期内容讲解完毕,感谢观看
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程之力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值