[unity] Added SkeletonAnimationMulti methods SetActiveSkeleton(int index) and getter property SkeletonAnimations, some code cleanup.

This commit is contained in:
Harald Csaszar 2022-10-04 13:19:03 +02:00
parent e2dbc468e4
commit 9d6afa1853
2 changed files with 28 additions and 21 deletions

View File

@ -90,6 +90,7 @@
* Added outline shader parameter `Advanced - Opaque Alpha` which can be used to exclude problematic semi-transparent areas, which may receive an undesired large outline color overlay otherwise. * Added outline shader parameter `Advanced - Opaque Alpha` which can be used to exclude problematic semi-transparent areas, which may receive an undesired large outline color overlay otherwise.
* Added Spine Preferences setting `Prefabs` - `Optimize Preview Meshes`. When enabled, Spine prefab preview meshes will be removed in a pre-build step to reduce build size. This increases build time as all prefabs in the project will be processed. Defaults to false to not slow down builds substantially every time. * Added Spine Preferences setting `Prefabs` - `Optimize Preview Meshes`. When enabled, Spine prefab preview meshes will be removed in a pre-build step to reduce build size. This increases build time as all prefabs in the project will be processed. Defaults to false to not slow down builds substantially every time.
* Added Spine Preferences setting `Reload SkeletonData after Play`. When enabled, the shared `SkeletonData` of all skeletons in the active scene is reloaded (from the `.json` or `.skel.bytes` file) after exiting play-mode. You can disable this setting to avoid the reloading delay if you can ensure that there are no (accidental) modifications to the shared `SkeletonData` during play-mode (otherwise it would carry over its effect into subsequent plays). Defaults to `true` (the safe setting), which maintains existing behaviour. * Added Spine Preferences setting `Reload SkeletonData after Play`. When enabled, the shared `SkeletonData` of all skeletons in the active scene is reloaded (from the `.json` or `.skel.bytes` file) after exiting play-mode. You can disable this setting to avoid the reloading delay if you can ensure that there are no (accidental) modifications to the shared `SkeletonData` during play-mode (otherwise it would carry over its effect into subsequent plays). Defaults to `true` (the safe setting), which maintains existing behaviour.
* Added `SkeletonAnimationMulti` sample component methods `SetActiveSkeleton(int index)` and getter property `SkeletonAnimations` to more easily apply changes at all SkeletonAnimation instances instead of only the active one.
* **Breaking changes** * **Breaking changes**
* Made `SkeletonGraphic.unscaledTime` parameter protected, use the new property `UnscaledTime` instead. * Made `SkeletonGraphic.unscaledTime` parameter protected, use the new property `UnscaledTime` instead.

View File

@ -56,17 +56,24 @@ namespace Spine.Unity {
SkeletonAnimation currentSkeletonAnimation; SkeletonAnimation currentSkeletonAnimation;
void Clear () { void Clear () {
foreach (var s in skeletonAnimations) foreach (SkeletonAnimation skeletonAnimation in skeletonAnimations)
Destroy(s.gameObject); Destroy(skeletonAnimation.gameObject);
skeletonAnimations.Clear(); skeletonAnimations.Clear();
animationNameTable.Clear(); animationNameTable.Clear();
animationSkeletonTable.Clear(); animationSkeletonTable.Clear();
} }
void SetActiveSkeleton (int index) {
if (index < 0 || index >= skeletonAnimations.Count)
SetActiveSkeleton(null);
else
SetActiveSkeleton(skeletonAnimations[index]);
}
void SetActiveSkeleton (SkeletonAnimation skeletonAnimation) { void SetActiveSkeleton (SkeletonAnimation skeletonAnimation) {
foreach (var sa in skeletonAnimations) foreach (SkeletonAnimation iter in skeletonAnimations)
sa.gameObject.SetActive(sa == skeletonAnimation); iter.gameObject.SetActive(iter == skeletonAnimation);
currentSkeletonAnimation = skeletonAnimation; currentSkeletonAnimation = skeletonAnimation;
} }
@ -81,34 +88,35 @@ namespace Spine.Unity {
public Dictionary<Animation, SkeletonAnimation> AnimationSkeletonTable { get { return this.animationSkeletonTable; } } public Dictionary<Animation, SkeletonAnimation> AnimationSkeletonTable { get { return this.animationSkeletonTable; } }
public Dictionary<string, Animation> AnimationNameTable { get { return this.animationNameTable; } } public Dictionary<string, Animation> AnimationNameTable { get { return this.animationNameTable; } }
public SkeletonAnimation CurrentSkeletonAnimation { get { return this.currentSkeletonAnimation; } } public SkeletonAnimation CurrentSkeletonAnimation { get { return this.currentSkeletonAnimation; } }
public List<SkeletonAnimation> SkeletonAnimations { get { return skeletonAnimations; } }
public void Initialize (bool overwrite) { public void Initialize (bool overwrite) {
if (skeletonAnimations.Count != 0 && !overwrite) return; if (skeletonAnimations.Count != 0 && !overwrite) return;
Clear(); Clear();
var settings = this.meshGeneratorSettings; MeshGenerator.Settings settings = this.meshGeneratorSettings;
Transform thisTransform = this.transform; Transform thisTransform = this.transform;
foreach (var sda in skeletonDataAssets) { foreach (SkeletonDataAsset dataAsset in skeletonDataAssets) {
var sa = SkeletonAnimation.NewSkeletonAnimationGameObject(sda); SkeletonAnimation newSkeletonAnimation = SkeletonAnimation.NewSkeletonAnimationGameObject(dataAsset);
sa.transform.SetParent(thisTransform, false); newSkeletonAnimation.transform.SetParent(thisTransform, false);
sa.SetMeshSettings(settings); newSkeletonAnimation.SetMeshSettings(settings);
sa.initialFlipX = this.initialFlipX; newSkeletonAnimation.initialFlipX = this.initialFlipX;
sa.initialFlipY = this.initialFlipY; newSkeletonAnimation.initialFlipY = this.initialFlipY;
var skeleton = sa.skeleton; Skeleton skeleton = newSkeletonAnimation.skeleton;
skeleton.ScaleX = this.initialFlipX ? -1 : 1; skeleton.ScaleX = this.initialFlipX ? -1 : 1;
skeleton.ScaleY = this.initialFlipY ? -1 : 1; skeleton.ScaleY = this.initialFlipY ? -1 : 1;
sa.Initialize(false); newSkeletonAnimation.Initialize(false);
skeletonAnimations.Add(sa); skeletonAnimations.Add(newSkeletonAnimation);
} }
// Build cache // Build cache
var animationNameTable = this.animationNameTable; Dictionary<string, Animation> animationNameTable = this.animationNameTable;
var animationSkeletonTable = this.animationSkeletonTable; Dictionary<Animation, SkeletonAnimation> animationSkeletonTable = this.animationSkeletonTable;
foreach (var skeletonAnimation in skeletonAnimations) { foreach (SkeletonAnimation skeletonAnimation in skeletonAnimations) {
foreach (var animationObject in skeletonAnimation.Skeleton.Data.Animations) { foreach (Animation animationObject in skeletonAnimation.Skeleton.Data.Animations) {
animationNameTable[animationObject.Name] = animationObject; animationNameTable[animationObject.Name] = animationObject;
animationSkeletonTable[animationObject] = skeletonAnimation; animationSkeletonTable[animationObject] = skeletonAnimation;
} }
@ -119,7 +127,6 @@ namespace Spine.Unity {
} }
public Animation FindAnimation (string animationName) { public Animation FindAnimation (string animationName) {
// Analysis disable once LocalVariableHidesMember
Animation animation; Animation animation;
animationNameTable.TryGetValue(animationName, out animation); animationNameTable.TryGetValue(animationName, out animation);
return animation; return animation;
@ -138,11 +145,10 @@ namespace Spine.Unity {
if (skeletonAnimation != null) { if (skeletonAnimation != null) {
SetActiveSkeleton(skeletonAnimation); SetActiveSkeleton(skeletonAnimation);
skeletonAnimation.skeleton.SetToSetupPose(); skeletonAnimation.skeleton.SetToSetupPose();
var trackEntry = skeletonAnimation.state.SetAnimation(MainTrackIndex, animation, loop); TrackEntry trackEntry = skeletonAnimation.state.SetAnimation(MainTrackIndex, animation, loop);
skeletonAnimation.Update(0); skeletonAnimation.Update(0);
return trackEntry; return trackEntry;
} }
return null; return null;
} }