• 中文
  • Spine如何动态替换材质球

环境:unity3d
这样替换材质无效

var mat    = BResources.Load<Material>("Map/MonsterMat/SpineUnlit");
var render = spine.GetComponent<Renderer>();
for (int i = 0; i < render.materials.Length; i++)
{
    var unlitMat  = Material.Instantiate(mat);
    var oldMat = render.materials[i];
    render.materials[i] = unlitMat;
    //
    MaterialPropertyBlock mpb = new MaterialPropertyBlock();
    mpb.SetTexture("_MainTex",oldMat.mainTexture);
    render.SetPropertyBlock(mpb,i);
}

Related Discussions
...
  • Изменено
5 дней спустя

材质由 SkeletonRenderer 组件(以及类似 SkeletonAnimation 的子类)管理,对 MeshRenderer.Materials 的任何更改都将被覆盖。

您可以在此处找到有关它的文档:
spine-unity Runtime Documentation: Materials
spine-unity Runtime Documentation: SkeletonRendererCustomMaterials
(遗憾的是目前还没有中文版,对此我们深表歉意!)

从文档页面:

MeshRenderer 的 Materials 数组每帧由 SkeletonRenderer 管理,具体取决于当前分配的附件和包含这些附件的 AtlasAssets。

注意:对 Materials 数组的直接修改无效,因为它们将在以下 LateUpdate() 调用中被覆盖。使用 SkeletonRendererCustomMaterials 或 SkeletonGraphicCustomMaterials 组件来覆盖材质。

要通过代码为您的 SkeletonRenderer 动态设置材质,您可以直接访问 SkeletonRenderer.CustomMaterialOverride 进行材质数组覆盖,以及访问 SkeletonRenderer.CustomSlotMaterials 进行插槽材质覆盖。


Materials are managed by the SkeletonRenderer component (and subclasses like SkeletonAnimation), any changes to MeshRenderer.Materials will be overwritten.

You can find documentation about it here:
spine-unity Runtime Documentation: Materials
spine-unity Runtime Documentation: SkeletonRendererCustomMaterials
(Unfortunately it's not yet available in Chinese, we are sorry about that!)

From the documentation pages:

The Materials array of a MeshRenderer is managed by the SkeletonRenderer every frame, depending on the currently assigned attachments and the AtlasAssets these are contained in.

Note: Direct modifications to the Materials array have no effect as they will be overwritten in the following LateUpdate() call. Use a SkeletonRendererCustomMaterials or SkeletonGraphicCustomMaterials component to override materials.

To dynamically set materials for your SkeletonRenderer through code, you can directly access SkeletonRenderer.CustomMaterialOverride for material array overrides and SkeletonRenderer.CustomSlotMaterials for slot material overrides.

Package version: spine-unity-3.8-2020-12-09.unitypackage
修改后代码,实测无效,
查了下SpineUnityDemo: SpineGauge.scene,是editor环境下的替换材质,不是runtime替换材质

//mat替换
{
    var mat    = BResources.Load<Material>("Map/MonsterMat/SpineUnlit");
    var render = spine.GetComponent<Renderer>();
    for (int i = 0; i < render.materials.Length; i++)
    {
        var newMat = Material.Instantiate(mat);
        var oldMat = render.materials[i];
        newMat.mainTexture                   = oldMat.mainTexture;
        spine.CustomMaterialOverride[oldMat] = newMat;
    }
}

恐怕我不明白,不幸的是,机器翻译似乎无法正确翻译您的帖子。 你能更详细地描述一下 SpineGauge.scene 有什么问题吗? 您能否还描述一下代码的上下文,它来自哪里或实际问题是什么。

一般来说,用多句话或添加截图来描述任何问题总是有很大帮助的,因为从中文到英文的翻译往往让我们很难猜测真正的含义是什么。


I'm afraid I don't understand, unfortunately machine translation seems to fail translating your posting properly. Could you please describe in more detail what is wrong with the SpineGauge.scene? Could you please also describe the context of the code, where it is from or what the problem actually is.

In general it always helps a lot to describe any problem in many sentences or by adding screenshots, because translation from Chinese to English often makes it hard for us to guess what the real meaning was.