[unity] Fixed SkeletonGraphic not updating the canvas renderer texture after skin changes (e.g. when calling GetRepackedSkin). Closes #1459.

This commit is contained in:
Harald Csaszar 2019-08-20 15:22:01 +02:00
parent 0207da7493
commit 687b53d15d
2 changed files with 32 additions and 5 deletions

View File

@ -59,6 +59,8 @@ namespace Spine.Unity {
public bool freeze;
public bool unscaledTime;
private Texture baseTexture = null;
#if UNITY_EDITOR
protected override void OnValidate () {
// This handles Scene View preview.
@ -152,9 +154,8 @@ namespace Spine.Unity {
}
public override Texture mainTexture {
get {
// Fail loudly when incorrectly set up.
if (overrideTexture != null) return overrideTexture;
return skeletonDataAsset == null ? null : skeletonDataAsset.atlasAssets[0].PrimaryMaterial.mainTexture;
return baseTexture;
}
}
@ -272,6 +273,7 @@ namespace Spine.Unity {
};
meshBuffers = new DoubleBuffered<MeshRendererBuffers.SmartMesh>();
baseTexture = skeletonDataAsset.atlasAssets[0].PrimaryMaterial.mainTexture;
canvasRenderer.SetTexture(this.mainTexture); // Needed for overwriting initializations.
// Set the initial Skin and Animation
@ -296,8 +298,7 @@ namespace Spine.Unity {
skeleton.SetColor(this.color);
var smartMesh = meshBuffers.GetNext();
var currentInstructions = this.currentInstructions;
MeshGenerator.GenerateSingleSubmeshInstruction(currentInstructions, skeleton, this.material);
MeshGenerator.GenerateSingleSubmeshInstruction(currentInstructions, skeleton, null);
bool updateTriangles = SkeletonRendererInstruction.GeometryNotEqual(currentInstructions, smartMesh.instructionUsed);
meshGenerator.Begin();
@ -317,6 +318,15 @@ namespace Spine.Unity {
canvasRenderer.SetMesh(mesh);
smartMesh.instructionUsed.Set(currentInstructions);
if (currentInstructions.submeshInstructions.Count > 0) {
var material = currentInstructions.submeshInstructions.Items[0].material;
if (material != null && baseTexture != material.mainTexture) {
baseTexture = material.mainTexture;
if (overrideTexture == null)
canvasRenderer.SetTexture(this.mainTexture);
}
}
//this.UpdateMaterial(); // TODO: This allocates memory.
}

View File

@ -133,6 +133,14 @@ namespace Spine.Unity {
}
#region Step 1 : Generate Instructions
/// <summary>
/// A specialized variant of <see cref="GenerateSkeletonRendererInstruction"/>.
/// Generates renderer instructions using a single submesh, using only a single material and texture.
/// </summary>
/// <param name="instructionOutput">The resulting instructions.</param>
/// <param name="skeleton">The skeleton to generate renderer instructions for.</param>
/// <param name="material">Material to be set at the renderer instruction. When null, the last attachment
/// in the draw order list is assigned as the instruction's material.</param>
public static void GenerateSingleSubmeshInstruction (SkeletonRendererInstruction instructionOutput, Skeleton skeleton, Material material) {
ExposedList<Slot> drawOrder = skeleton.drawOrder;
int drawOrderCount = drawOrder.Count;
@ -161,6 +169,7 @@ namespace Spine.Unity {
};
#if SPINE_TRIANGLECHECK
object rendererObject = null;
bool skeletonHasClipping = false;
var drawOrderItems = drawOrder.Items;
for (int i = 0; i < drawOrderCount; i++) {
@ -174,11 +183,13 @@ namespace Spine.Unity {
var regionAttachment = attachment as RegionAttachment;
if (regionAttachment != null) {
rendererObject = regionAttachment.RendererObject;
attachmentVertexCount = 4;
attachmentTriangleCount = 6;
} else {
var meshAttachment = attachment as MeshAttachment;
if (meshAttachment != null) {
rendererObject = meshAttachment.RendererObject;
attachmentVertexCount = meshAttachment.worldVerticesLength >> 1;
attachmentTriangleCount = meshAttachment.triangles.Length;
} else {
@ -194,8 +205,14 @@ namespace Spine.Unity {
current.rawTriangleCount += attachmentTriangleCount;
current.rawVertexCount += attachmentVertexCount;
totalRawVertexCount += attachmentVertexCount;
}
#if !SPINE_TK2D
if (material == null)
current.material = (Material)((AtlasRegion)rendererObject).page.rendererObject;
#else
if (material == null)
current.material = (rendererObject is Material) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
#endif
instructionOutput.hasActiveClipping = skeletonHasClipping;
instructionOutput.rawVertexCount = totalRawVertexCount;