From cbe0e5433a1960cd4683843ecd46dcaabd6de86a Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Tue, 13 Jun 2023 17:11:33 +0200 Subject: [PATCH] [unity] Added `SkeletonGraphic.MeshScale` property to allow access to calculated mesh scale. --- CHANGELOG.md | 3 ++- .../SpineboyTargetControllerGraphic.cs | 2 +- .../spine-unity/Components/SkeletonGraphic.cs | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8968968b4..ec625f51e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,7 +102,8 @@ * Added support for light cookies at `Universal Render Pipeline/Spine/Sprite` shader. * Timeline extension package: An additional Spine preferences parameter `Timeline` - `Default Mix Duration` has been added, setting newly added `SpineAnimationStateClip` clips accordingly, defaults to false. This Spine preferences parameter can be enabled to default to the previous behaviour before this update. * Tint Black: Added support for [Tint Black](http://en.esotericsoftware.com/spine-slots#Tint-black) functionality at all Spine URP shaders (2D and 3D shaders) and at all standard pipeline `Spine/Sprite` shaders. This feature can be enabled via the `Tint Black` material parameter in the Inspector. Note: The URP Sprite shaders provided in the Spine URP Shaders extension UPM package require the latest version of the spine-unity runtime (package version 4.1.12, 2023-05-31 or newer) to display the added material parameters in the Inspector GUI. - + * Added `SkeletonGraphic.MeshScale` property to allow access to calculated mesh scale. `MeshScale` is based on (1) Canvas pixels per unit, and (2) `RectTransform` bounds when using `Layout Scale Mode` other than `None` at `SkeletonGraphic` which scales the skeleton mesh to fit the parent `RectTransform` bounds accordingly. + * **Breaking changes** * Made `SkeletonGraphic.unscaledTime` parameter protected, use the new property `UnscaledTime` instead. * `SkeletonGraphic` `OnRebuild` callback delegate is now issued after the skeleton has been initialized, before the `AnimationState` component is initialized. This makes behaviour consistent with `SkeletonAnimation` and `SkeletonMecanim` component behaviour. Use the new callback `OnAnimationRebuild` if you want to receive a callback after the `SkeletonGraphic` `AnimationState` has been initialized. diff --git a/spine-unity/Assets/Spine Examples/Scripts/Getting Started Scripts/SpineboyTargetControllerGraphic.cs b/spine-unity/Assets/Spine Examples/Scripts/Getting Started Scripts/SpineboyTargetControllerGraphic.cs index c51740341..c04b5dce5 100644 --- a/spine-unity/Assets/Spine Examples/Scripts/Getting Started Scripts/SpineboyTargetControllerGraphic.cs +++ b/spine-unity/Assets/Spine Examples/Scripts/Getting Started Scripts/SpineboyTargetControllerGraphic.cs @@ -54,7 +54,7 @@ namespace Spine.Unity.Examples { Vector2 localRectPosition; RectTransformUtility.ScreenPointToLocalPointInRectangle( skeletonGraphic.rectTransform, mousePosition, null, out localRectPosition); - Vector3 skeletonSpacePoint = localRectPosition / canvas.referencePixelsPerUnit; + Vector3 skeletonSpacePoint = localRectPosition / skeletonGraphic.MeshScale; skeletonSpacePoint.x *= skeletonGraphic.Skeleton.ScaleX; skeletonSpacePoint.y *= skeletonGraphic.Skeleton.ScaleY; bone.SetLocalPosition(skeletonSpacePoint); diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index dee7bf448..8dc0da467 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -67,6 +67,8 @@ namespace Spine.Unity { public bool startingLoop; public float timeScale = 1f; public bool freeze; + protected float meshScale = 1f; + public float MeshScale { get { return meshScale; } } public enum LayoutMode { None = 0, @@ -780,13 +782,13 @@ namespace Spine.Unity { meshGenerator.BuildMeshWithArrays(currentInstructions, updateTriangles); } - float scale = (canvas == null) ? 100 : canvas.referencePixelsPerUnit; + meshScale = (canvas == null) ? 100 : canvas.referencePixelsPerUnit; if (layoutScaleMode != LayoutMode.None) { - scale *= referenceScale; + meshScale *= referenceScale; if (!EditReferenceRect) - scale *= GetLayoutScale(layoutScaleMode); + meshScale *= GetLayoutScale(layoutScaleMode); } - meshGenerator.ScaleVertexData(scale); + meshGenerator.ScaleVertexData(meshScale); if (OnPostProcessVertices != null) OnPostProcessVertices.Invoke(this.meshGenerator.Buffers); Mesh mesh = smartMesh.mesh; @@ -858,11 +860,11 @@ namespace Spine.Unity { } protected void UpdateMeshMultipleCanvasRenderers (SkeletonRendererInstruction currentInstructions) { - float scale = (canvas == null) ? 100 : canvas.referencePixelsPerUnit; + meshScale = (canvas == null) ? 100 : canvas.referencePixelsPerUnit; if (layoutScaleMode != LayoutMode.None) { - scale *= referenceScale; + meshScale *= referenceScale; if (!EditReferenceRect) - scale *= GetLayoutScale(layoutScaleMode); + meshScale *= GetLayoutScale(layoutScaleMode); } // Generate meshes. int submeshCount = currentInstructions.submeshInstructions.Count; @@ -884,7 +886,7 @@ namespace Spine.Unity { meshGenerator.AddSubmesh(submeshInstructionItem); Mesh targetMesh = meshesItems[i]; - meshGenerator.ScaleVertexData(scale); + meshGenerator.ScaleVertexData(meshScale); if (OnPostProcessVertices != null) OnPostProcessVertices.Invoke(this.meshGenerator.Buffers); meshGenerator.FillVertexData(targetMesh); meshGenerator.FillTriangles(targetMesh);