Unity SpriteAtlas 打包预览窗口_unity texture2d.packtextures-程序员宅基地

技术标签: c#  unity  

这里需要做一个对图集利用率确认的工具,确定图集的合理性。

这里就做个单个图集的利用率确认和简单绘制。

这很鸡肋,如果必要就做成批量确认的工具,这里仅仅是示范。

关于Texture2D.PackTextures()

在这里插入图片描述

Texture2D.PackTextures() 这是定义在Texture2D里的打包函数,一开始我以为这就是图集的打包函数。

但是后来为我试了一下,并不是…

我们添加以下脚本,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.U2D;
using UnityEditor.U2D;
using System;

public class SpriteAtlasWindowOld : EditorWindow
{
    
    static SpriteAtlasWindowOld window;

    SpriteAtlas spriteAtlas;

    Sprite[] sprites;
    Texture2D[] texture2Ds;
    Texture2D previewTexture;

    readonly GUIStyle preBackground = "preBackground";

    [MenuItem("Assets/GUI/Sprite preview old")]
    static void Init()
    {
    
        window = GetWindow<SpriteAtlasWindowOld>("Spriteatlas Preview 01");

        window.spriteAtlas = Selection.activeObject as SpriteAtlas;

        window.sprites = new Sprite[window.spriteAtlas.spriteCount];
        window.spriteAtlas.GetSprites(window.sprites);
        window.texture2Ds = new Texture2D[window.sprites.Length];
        for (int i = 0; i < window.sprites.Length; i++)
        {
    
            window.texture2Ds[i] = window.sprites[i].texture;

            TextureImporter textureImporter = TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(window.texture2Ds[i])) as TextureImporter;
            textureImporter.isReadable = true;
            textureImporter.SaveAndReimport();
        }
        SpriteAtlasPackingSettings packingsetting = SpriteAtlasExtensions.GetPackingSettings(window.spriteAtlas);
        TextureImporterPlatformSettings platformsetting = SpriteAtlasExtensions.GetPlatformSettings(window.spriteAtlas, "Android");
        window.previewTexture = new Texture2D(platformsetting.maxTextureSize, platformsetting.maxTextureSize);

        window.previewTexture.PackTextures(window.texture2Ds, packingsetting.padding);
    }

    [MenuItem("Assets/GUI/Sprite preview old", true)]
    static bool Valid()
    {
    
        if (Selection.activeObject.GetType() == typeof(SpriteAtlas))
            return true;
        else
            return false;
    }

    private void OnGUI()
    {
    
        if (previewTexture != null)
        {
    
            Rect r = new Rect();
            r.width = previewTexture.width;
            r.height = previewTexture.height;

            if (Event.current.type == EventType.Repaint)
                preBackground.Draw(r, false, false, false, false);

            EditorGUI.DrawTextureTransparent(r, previewTexture);

            GUI.DrawTexture(r, previewTexture);
        }
    }

    private void OnDisable()
    {
    
        window = null;
    }

}

打开窗口看一下

在这里插入图片描述
再看下图集预览,

在这里插入图片描述
。。。。很明显,这不一样,所以我们不能用 Texture2D 里的这个函数了。

关于 Pack Preview 按键

既然 Texture2D 里的打包函数我们不能用,那我们直接看下源码里的 Pack preview 是怎么操作的。

我们在 Unity 编辑器源码里直接全局搜索 Pack Preview ,就可以找到自己想要的。

在这里插入图片描述

然后找到使用它的地方,

在这里插入图片描述
我们可以看到,蓝色框内的就是它的使用位置了。那我们接着看下它这个函数时怎么实现的

在这里插入图片描述
。。。。就这??

既然这一部分不给看,那我们就另寻他法咯

从图集的preview窗口入手

既然我们不能直接从打包预览后,直接获取它的打包结果,那我们就间接看下 Inspector 那边绘制的 texture 是哪里来的。

我们通过 InspectorWindow.cs 可以看到有个 PreviewWindow 类,我们再从这个类下手,果不其然,让我们找到了绘制的函数。

在这里插入图片描述
接着,我们再从 Editor.cs 入手,看看这个 DrawPreview() 是怎样的。

我们可以看到它再套用了一个静态,

在这里插入图片描述
这个静态又分为绘制多个的情况,和绘制单个的情况。

在这里插入图片描述
我们可以看到,在 DrawPreview 里又调了了一层 OnInteractivePreviewGUI ,在里面执行 OnPreviewGUI() ,进行具体的预览图绘制。
在这里插入图片描述
在这里插入图片描述
激动人心的时候到了,看下它是怎么获取打包预览图的。

在这里插入图片描述

最后,我们找到了这个函数。

在这里插入图片描述

转到这个函数看看。

在这里插入图片描述
这个类倒不是内置类,但是这个函数却是,所以,我们要用下反射了。

我们写个静态类,来装下这个函数。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
using UnityEngine.U2D;
using UnityEditor.U2D;

public static class ReflSpriteatlasExtensions
{
    
    public static Texture2D[] GetPreviewTextures(SpriteAtlas spriteAtlas)
    {
    
        MethodInfo methodInfo = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.NonPublic | BindingFlags.Static);
        if (methodInfo == null)
        {
    
            Debug.LogError("<color=red> 从 SpriteAtlasExtensions 获取方法为空! </color>");
            return null;
        }
        else
        {
    
            return methodInfo.Invoke(null, new SpriteAtlas[] {
     spriteAtlas }) as Texture2D[];
        }
    }
}

完事具备,接下来,就需要写个窗口来绘制预览图了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.U2D;
using UnityEditor.U2D;
using System;

public class SpritePreviewWindow : EditorWindow
{
    
    static SpritePreviewWindow window;

    SpriteAtlas spriteAtlas;

    Texture2D[] previewTextures;

    readonly GUIStyle preBackground = "preBackground";

    [MenuItem("Assets/GUI/Sprite preview 01")]
    static void Init()
    {
    
        window = GetWindow<SpritePreviewWindow>("Spriteatlas Preview 01");

        window.spriteAtlas = Selection.activeObject as SpriteAtlas;

        SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] {
     window.spriteAtlas }, BuildTarget.Android);

        //反射拿预览图集图片
        window.previewTextures = ReflSpriteatlasExtensions.GetPreviewTextures(window.spriteAtlas);
    }

    [MenuItem("Assets/GUI/Sprite preview 01", true)]
    static bool Valid()
    {
    
        if (Selection.activeObject.GetType() == typeof(SpriteAtlas))
            return true;
        else
            return false;
    }

    private void OnGUI()
    {
    
        if (previewTextures != null && previewTextures.Length > 0 && previewTextures[0] != null)
        {
    
            Rect r = new Rect();
            r.width = previewTextures[0].width;
            r.height = previewTextures[0].height;

            if (Event.current.type == EventType.Repaint)
                preBackground.Draw(r, false, false, false, false);

            EditorGUI.DrawTextureTransparent(r, previewTextures[0]);

            GUI.DrawTexture(r, previewTextures[0]);
        }
    }

    private void OnDisable()
    {
    
        window = null;
    }

}

虽然我不知道为什么他们缺了一角,但是没关系,这并不重要,重要的是它们一样了。

在这里插入图片描述

完美。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44293055/article/details/120473824

智能推荐

Java——注解(Annotation)-程序员宅基地

文章浏览阅读105次。1. 简介官方解释:Java 注解用于为 Java 代码提供元数据。作为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上可以用于这一目的。注解的定义:通俗的来讲,注解就如同标签。一个注解准确意义上来说,只不过是一种特殊的注释而已,如果没有解析它的代码,它可能连注释都不如。注解的本质就是一个继承了 Annotation 接口的接口,下面是注解 @Override 的定义,其实它本质上就是:public interface Override extends Annotation{

[转]动态添加ContextMenuStrip项(ToolStripItem)-程序员宅基地

文章浏览阅读121次。//绑定菜单 private void BindMenu(DataTable dt){foreach (DataRow row in dt.Rows){ToolStripItem item = new ToolStripMenuItem();item.Name = row[0].ToString();item.Text = row[1].ToString()..._toolstripitem new

Java 基本数据类型取值范围讲解_java基本数据类型范围是怎么来的-程序员宅基地

文章浏览阅读7k次,点赞5次,收藏8次。转自http://apps.hi.baidu.com/share/detail/37526799java中的类型概念名的说法不一、让我很是迷茫,我个人的理解整理,如有错误还请高人指点,! 一、Java的类型词语理解:1) 原始数据类型,简单类型,基本类型都是一个含义;2)复合类型,扩展类型,复杂类型、引用类型都是一个含义;3)浮点类型,实数、实型都是一个含义_java基本数据类型范围是怎么来的

快速傅里叶变换FFT-程序员宅基地

文章浏览阅读74次。快速傅里叶变换FFT(Fast Fourier Transformation)本文主要讲述如何使用FFT来实现快速多项式乘法。多项式的表示系数表示对于一个多项式\[A(x)=\sum_{j=0}^{n-1} a_jx^j\]向量\(a=(a_0, a_1, ..., a_{n-1})\)为多项式的系数表示。利用系数表示时,给出\(x_0\),在\(O(n)\)的时间内可以求..._fft 点数和基数无法整除

2019暑期实习面试 - 腾讯PCG移动客户端iOS开发面试-程序员宅基地

文章浏览阅读228次。基本信息事业群:PCG岗位:移动客户端开发(iOS、Objective-C语言开发)实习时间:6月份之后的暑期实习面试日期:3月30日 - 3月31日offer call:4月10日(看到很多小伙伴都接到offer call了,内心有一些着急,9号换了ycy头像)一面面试时间一共为:28min,3月30日问题涉及:iOS项目、操作系统问题未涉及:..._ios 腾讯pcg面试题

jquery屏蔽掉键盘enter提交 onkeydown-程序员宅基地

文章浏览阅读250次。屏蔽掉enter提交onkeydown onkeydown="if(event.keyCode==13){return false;}"转载于:https://www.cnblogs.com/huanghuali/p/10031745.html_jquery div onkeydown

随便推点

C# 获取枚举值/获取名字和值-程序员宅基地

文章浏览阅读4.1k次。枚举 int 转 枚举名称public void Test(){ //调用 string name1= Conver..._c#怎么拿到杖举的名字

PopWindow在Android 2.3.3 或以下的系统的一个bug 及其解决办法_廉温-程序员宅基地

文章浏览阅读1.5k次。今日终于修复了一个非常严重的bug: 这个bug非常奇怪,我在Anroid.4.0.4或以上测试都正常,但是后来廉温说他在他的手机按一下右下角的"设置"按钮(Button),居然出现了崩溃现象(理论上会在设置按钮上弹出一popuwindow); 廉温手机系统2.3.X ;然后我用平板(系统也是2.3.x)测试下,果然出现错误; 由于平板基本报废,无法USB连接_廉温

nginx在Windows下配置运行及nginx局域网共享文件的方法_nginx共享时中文下载-程序员宅基地

文章浏览阅读5.5k次,点赞2次,收藏28次。一、Nginx在Windows下配置运行Nginx 是一款自由的、开源的、高性能的 HTTP 服务器和反向代理服务器;同时也是一个 IMAP、POP3、SMTP 代理服务器。Nginx 可以作为一个 HTTP 服务器进行网站的发布处理,另外 Nginx 可以作为反向代理进行负载均衡的实现。首先,进入nginx官网,进入下载的页面,选择合适的版本进行下载。将下载的压缩包解压即可直接运行。(不要直接双击nginx.exe!!!)运行步骤:进入conf文件夹修改nginx.conf配置文件将_nginx共享时中文下载

pip命令安装 pyinstaller失败解决办法-程序员宅基地

文章浏览阅读6.7k次,点赞3次,收藏7次。写在前面,如果是用win10系统的用户,一定要先通过管理员模式打开命令窗口,我是直接按 win + x键,选择 "Windows Powershell(管理员)",否则会提示:[Errno 13] Permission denied:下面是在管理员模式下通过 pip命令安装失败从下面错误来看,是安装build的依赖失败,但具体是哪些也不清楚,网上找也没有类似错误的解决办法..._pip install pyinstaller无法安装

epoll 使用实例-程序员宅基地

文章浏览阅读66次。原文:http://blog.csdn.net/force_eagle/article/details/4348017epoll网上g一大把, 就不详细叙述了.推荐几篇好文章:epoll精髓 epoll相关资料整理 epoll LT VS ET epoll(7) - Linux man pageEPOLL为我们带来了什么[cpp] view..._epoll hangup

离线下载conda包并安装_conda cudatoolkit-dev 离线安装-程序员宅基地

文章浏览阅读1.2k次,点赞6次,收藏4次。早就看conda不爽了,实验室服务器duangduangduang的吵,下载还是这个速度,我…于是乎咱们用别的工具下载conda要安装的包,然后在用conda安装,所谓移花接木?也不过如此hhhconda info 你包的名字比如:conda info cudatoolkit=10.2然后用那个url:https://repo.anaconda.com/pkgs/main/linux-64/cudatoolkit-10.2.89-hfd86e86_0.tar.bz2下载即可,我们得到一_conda cudatoolkit-dev 离线安装