RuntimeLoadFromExportsExample.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class RuntimeLoadFromExportsExample : MonoBehaviour {
  34. public TextAsset skeletonJson;
  35. public TextAsset atlasText;
  36. public Texture2D[] textures;
  37. public Material materialPropertySource;
  38. public float delay = 0;
  39. public string skinName;
  40. public string animationName;
  41. SpineAtlasAsset runtimeAtlasAsset;
  42. SkeletonDataAsset runtimeSkeletonDataAsset;
  43. SkeletonAnimation runtimeSkeletonAnimation;
  44. SkeletonGraphic runtimeSkeletonGraphic;
  45. public bool blendModeMaterials = false;
  46. public bool applyAdditiveMaterial = false;
  47. public BlendModeMaterials.TemplateMaterials blendModeTemplateMaterials;
  48. public BlendModeMaterials.TemplateMaterials graphicBlendModeMaterials;
  49. public Material skeletonGraphicMaterial;
  50. void CreateRuntimeAssetsAndGameObject () {
  51. // 1. Create the AtlasAsset (needs atlas text asset and textures, and materials/shader);
  52. // 2. Create SkeletonDataAsset (needs json or binary asset file, and an AtlasAsset)
  53. // 2.1 Optional: Setup blend mode materials at SkeletonDataAsset. Only required if the skeleton
  54. // uses blend modes which require blend mode materials.
  55. // 3.a Create SkeletonAnimation (needs a valid SkeletonDataAsset)
  56. // 3.b Create SkeletonGraphic (needs a valid SkeletonDataAsset)
  57. runtimeAtlasAsset = SpineAtlasAsset.CreateRuntimeInstance(atlasText, textures, materialPropertySource, true, null, true);
  58. runtimeSkeletonDataAsset = SkeletonDataAsset.CreateRuntimeInstance(skeletonJson, runtimeAtlasAsset, true);
  59. if (blendModeMaterials)
  60. runtimeSkeletonDataAsset.SetupRuntimeBlendModeMaterials(
  61. applyAdditiveMaterial, blendModeTemplateMaterials);
  62. }
  63. IEnumerator Start () {
  64. CreateRuntimeAssetsAndGameObject();
  65. if (delay > 0) {
  66. runtimeSkeletonDataAsset.GetSkeletonData(false); // preload
  67. yield return new WaitForSeconds(delay);
  68. }
  69. InstantiateSkeletonAnimation();
  70. InstantiateSkeletonGraphic();
  71. }
  72. void InstantiateSkeletonAnimation () {
  73. runtimeSkeletonAnimation = SkeletonAnimation.NewSkeletonAnimationGameObject(runtimeSkeletonDataAsset);
  74. runtimeSkeletonAnimation.transform.parent = transform;
  75. runtimeSkeletonAnimation.name = "SkeletonAnimation Instance";
  76. // additional initialization
  77. runtimeSkeletonAnimation.Initialize(false);
  78. if (skinName != "")
  79. runtimeSkeletonAnimation.Skeleton.SetSkin(skinName);
  80. runtimeSkeletonAnimation.Skeleton.SetSlotsToSetupPose();
  81. if (animationName != "")
  82. runtimeSkeletonAnimation.AnimationState.SetAnimation(0, animationName, true);
  83. }
  84. void InstantiateSkeletonGraphic () {
  85. Canvas canvas = this.GetComponentInChildren<Canvas>();
  86. Transform parent = canvas.transform;
  87. runtimeSkeletonGraphic = SkeletonGraphic.NewSkeletonGraphicGameObject(runtimeSkeletonDataAsset, parent, skeletonGraphicMaterial);
  88. runtimeSkeletonGraphic.name = "SkeletonGraphic Instance";
  89. if (blendModeMaterials) {
  90. runtimeSkeletonGraphic.allowMultipleCanvasRenderers = true;
  91. runtimeSkeletonGraphic.additiveMaterial = graphicBlendModeMaterials.additiveTemplate;
  92. runtimeSkeletonGraphic.multiplyMaterial = graphicBlendModeMaterials.multiplyTemplate;
  93. runtimeSkeletonGraphic.screenMaterial = graphicBlendModeMaterials.screenTemplate;
  94. }
  95. // additional initialization
  96. runtimeSkeletonGraphic.Initialize(false);
  97. if (skinName != "")
  98. runtimeSkeletonGraphic.Skeleton.SetSkin(skinName);
  99. runtimeSkeletonGraphic.Skeleton.SetSlotsToSetupPose();
  100. if (animationName != "")
  101. runtimeSkeletonGraphic.AnimationState.SetAnimation(0, animationName, true);
  102. }
  103. }
  104. }