Spine版本3.8xx

我的Unity项目中的角色可以换装。并且工程内Assets文件夹下所有供给角色的衣服图片,都是可读写的,使用GetRepackedSkin时角色换装正常。

但现在我收到一个需求,app在运行时玩家自己创建服装图片(这些图片是放在可读写路径下,并不是Asssets文件夹下),类似DIY的功能。我尝试通过路径加载这个图片,并且设置可读写后。使用GetRepackedSkin角色换装异常。

我尝试了不使用GetRepackedSkin方法,所有的服装都是以碎片的形式穿在角色身上,显示是正常的。但这样DrawCall会非常高,代价是昂贵的。

我是通过下面这个函数加载Texture2D,通过这个Texture2D创建的精灵。

public static Texture2D load_texture_from_file(this GameObject obj,string path,bool readwrite = false,bool mipchain = false,bool liner = true)
    {
        if (!File.Exists(path))
        {
            return null;
        }
        byte[] bytes = File.ReadAllBytes(path);
        Texture2D tex = new Texture2D(0, 0, TextureFormat.RGBA32, false,true);
        tex.name = path;
        tex.LoadImage(bytes);
        if (readwrite)
        {
            RenderTexture renderTex = RenderTexture.GetTemporary(tex.width,tex.height,0, RenderTextureFormat.ARGB32,RenderTextureReadWrite.Linear);
            Graphics.Blit(tex, renderTex);
            RenderTexture previous = RenderTexture.active;
            RenderTexture.active = renderTex;
            Texture2D readableText = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, mipchain, liner);
            readableText.name = path+"_rw";
            readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
            readableText.Apply();
            RenderTexture.active = previous;
            RenderTexture.ReleaseTemporary(renderTex);
            return readableText;
        }
        bytes = null;
        return tex;
    }

下图是当我使用GetRepackedSkin方法时,运行期间渲染角色时的材质图片(红色区域是DIY衣服的碎片):

看起来他只有左下角,只有四分之一。我不知道为什么。

    Related Discussions
    ...

    Joied 很遗憾您遇到了问题。您能否先查看一下 spine-unity 运行时文档中关于运行时重新打包的重要说明是否与您的情况相符?:

    I'm sorry to hear you're having trouble. Could you first check to see if any of the important notes for runtime repacking in the spine-unity runtime documentation matches your case?:
    https://zh.esotericsoftware.com/spine-unity#%E7%BB%84%E5%90%88%E7%9A%AE%E8%82%A4

    重要提示: 如果重打包失败或产生意外问题, 可能源于以下原因:

    1. 禁用了读/写. 你可能需要在并入重打包texture的源textures上设置 Read/Write Enabled 参数.
    2. 启用了压缩: 需要确保源texture的Texture导入设置中 Compression 为 None , 而不是 Normal Quality.
    3. 质量层使用了half/quarter分辨率的textures: 这个是Unity的bug, 当使用half/quarter分辨率的texture时会复制错误区域. 确保项目设置中的所有质量层都使用全分辨率texture.
    4. 源texture不是2次texture但Unity将其放大为了最接近次数:a)从Spine导出时启用 Pack Settings 中的 Power of two, 或者b)确保Unity的Atlas Texture导入设置中将 Non-Power of Two 置为 None.

    如果上述情况都不存在,能否请您通过电子邮件向我们发送一个可以重现该问题的最小 Unity 项目?: contact@esotericsoftware.com
    If none of these are the case, could you please send us a minimal Unity project that can reproduce the problem via email?: contact@esotericsoftware.com

    我打印了Sprite以及Texture2D所有的属性。我发现是WrapMode的原因。我已经修复了,感谢Misaki提供的帮助。非常感谢。

      Joied 我很高兴听到您解决了这个问题!感谢您回复我们🙂

      I’m glad to hear you've figured it out! Thank you for getting back to us 🙂