• RuntimesUnity
  • Unity运行时内存中存在大量mesh

首先说一下动画:
1.是使用大量图片制作的帧动画(我深刻的理解到了, spine到底应该用来做什么)
2.一个spine工程有多个动作(10+ ?), 每个动作 6+ 张图片

我发现每一帧获取到的mesh的 m_instanceID 都是不同的, 然后检查了一下内存, 发现四分钟左右mesh增加了4000个. 且顶其中大量增加的mesh的名称是"Skeleton Mesh Instance".

动画的做法是将所有的附件放到一个插槽下, 通过附件的显示隐藏来实现帧动画. 通过这种做法, 在Unity中播放时每一帧都会创建一个新的mesh, 且旧的mesh不会被销毁?

Related Discussions
...

在Unity中使用Spine时,如果每一帧都创建一个新的Mesh且旧的Mesh没有被销毁,这可能会导致内存泄漏和性能问题。以下是一些建议来优化你的Spine动画,以避免这种情况:

  1. 避免每帧创建新Mesh
    Spine在Unity中通常不会每帧创建新的Mesh,而是重用现有的Mesh。确保你使用的是最新版本的Spine-Unity运行时,并且没有修改过Mesh的创建逻辑。

  2. 使用RenderExistingMesh组件
    你可以使用RenderExistingMesh组件来优化Mesh的渲染。这个组件可以帮助你避免每帧创建新的Mesh。参考文档:RenderExistingMesh

  3. 减少顶点和骨骼数量
    尽量减少每个Skeleton的顶点和骨骼数量。你可以使用Prune工具来减少不必要的顶点和骨骼。

  4. 使用二进制格式导出Skeleton
    确保你的Skeleton是以二进制格式导出的,而不是JSON格式。这可以减少加载时间和内存占用。参考文档:Binary Export for Unity

  5. 检查动画的实现方式
    你的动画是通过大量图片制作的帧动画,这可能不是最优的实现方式。Spine更适合使用骨骼动画而不是帧动画。尽量使用骨骼动画来减少资源消耗。

  6. 优化SkeletonAnimation的更新频率
    你可以通过减少SkeletonAnimation的更新频率来优化性能。例如,可以参考示例场景Spine Examples/Other Examples/FixedTimestepUpdates,该场景展示了如何每N帧更新一次。

通过以上方法,你应该能够减少内存中的Mesh数量,并提高Spine动画在Unity中的性能。

    Spinebot 完美的解决方案, 但是我对mesh重用这部分还是有些疑惑. 使用的spine版本是3.8.99. 我在代码里每帧获取mesh, 会是这个操作导致mesh没有被重用吗?

    @Init If a new Mesh is created frequently for the same skeleton, it is likely that something is wrong with your code. Version 3.8 is already rather old and many bugs have been fixed in the meantime. Nevertheless, be sure you're using the latest spine-unity 3.8 runtime package from the download page and not an outdated spine-unity package:
    https://esotericsoftware.com/spine-unity-download

    To track down the issue, you can try to create a minimal Unity project which still causes the issue. If the same behaviour also occurs with Spine 4.2 and the latest spine-unity 4.2 package, please send us the minimal Unity reproduction project so that we can investigate the issue. You can send it as a zip file to contact@esotericsoftware.com, briefly mentioning this forum thread URL so that we know the context.

    • Init replied to this.

      Harald 不好意思隔了一天回复, spine 3.8 是稳定的.
      MeshFilter.mesh会返回一个mesh实例, 这个实例不会被自动删除. 我在Updata()函数中调用导致mesh实例不断增多, 最终导致内存溢出.
      MeshFilter.shareMesh可以避免这个问题

      @Init The only occurrance of setting MeshFilter.mesh that I can see is in SkeletonRenderTexture, quadMeshFilter.mesh = quadMesh;. Did you mean this occurrance? If so, this is fixed on the 4.2 branch already (for the future please mention that you're using SkeletonRenderTexture).

      In general, please always describe precisely where you found the problematic code.

      • Init replied to this.
        5 days later

        Harald
        我在代码中使用了 var mesh = MeshFilter.mesh. 而且没有手动销毁获取到的mesh导致内存溢出, 而非spine运行库的问题.

        @Init Glad to hear you've found the issue, thanks for letting us know.

        • Init replied to this.

          Harald
          感谢你们的回复.
          你们的活跃使我在使用spine时充满了安全感.
          谢谢你们!