using System.Collections;
using System.Collections.Generic;
using Spine;
using Spine.Unity;
using UnityEngine;
public class Demo : MonoBehaviour
{
public SkeletonGraphic mySkeletonGraphic;
public SkeletonGraphic mySkeletonGraphic2;
public SkeletonGraphic mySkeletonGraphic3;
public Transform transform;
public string path;
void Start()
{
mySkeletonGraphic = LoadSpineAnimationFromResource("Animation/dankuang_SkeletonData", transform);
mySkeletonGraphic.AnimationState.SetAnimation(0, "idle", true);
mySkeletonGraphic.AnimationState.Complete += delegate
{
mySkeletonGraphic.AnimationState.TimeScale = 0;
};
mySkeletonGraphic2 = LoadSpineAnimationFromABPackage(Path() + "/Resources/Animation/frame/IOS/spine.logicfour_score_frame", "dankuang_SkeletonData", transform);
mySkeletonGraphic2.AnimationState.SetAnimation(0,"idle",true);
mySkeletonGraphic2.AnimationState.Complete += delegate {
mySkeletonGraphic2.gameObject.SetActive(false);
};
mySkeletonGraphic.AnimationState.Event += HandleEvent;
for (int i = 0; i < mySkeletonGraphic.Skeleton.Data.Events.Items.Length; i++)
{
Debug.Log("检索出所有的事件: " + mySkeletonGraphic.Skeleton.Data.Events.Items[i].Name);
}
mySkeletonGraphic3 = LoadSpineAnimationFromResource("Animation/paopao/paopao1_SkeletonData", transform);
mySkeletonGraphic3.AnimationState.SetAnimation(0, "broken", true);
mySkeletonGraphic3.initialSkinName = "paopao5";
}
private void HandleEvent(TrackEntry trackEntry, Spine.Event e)
{
if (e.Data.Name == "test")
{
Debug.Log("触发事件逻辑");
}
}
private SkeletonGraphic LoadSpineAnimationFromResource(string path, Transform transform)
{
Material material = Resources.Load<Material>("Common/Spine/SkeletonGraphicDefault");
SkeletonDataAsset mySkeletonDataAsset = Resources.Load<SkeletonDataAsset>(path);
if (mySkeletonDataAsset != null)
{
SkeletonGraphic myAnimation = SkeletonGraphic.NewSkeletonGraphicGameObject(mySkeletonDataAsset, transform, material);
myAnimation.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
myAnimation.name = myAnimation.skeletonDataAsset.name;
myAnimation.material = material;
myAnimation.transform.SetAsLastSibling();
myAnimation.MatchRectTransformWithBounds();
myAnimation.raycastTarget = false;
myAnimation.Initialize(true);
return myAnimation;
}
return null;
}
SkeletonGraphic LoadSpineAnimationFromABPackage(string path, string dataAssetName, Transform transform)
{
AssetBundle bundle = AssetBundle.LoadFromFile(path, 0);
if (bundle != null)
{
Material material = Resources.Load<Material>("Common/Spine/SkeletonGraphicDefault");
SkeletonDataAsset mySkeletonDataAsset = bundle.LoadAsset<SkeletonDataAsset>(dataAssetName);
bundle.Unload(false);
SkeletonGraphic myAnimation = SkeletonGraphic.NewSkeletonGraphicGameObject(mySkeletonDataAsset, transform, material);
myAnimation.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
myAnimation.name = myAnimation.skeletonDataAsset.name;
myAnimation.material = material;
myAnimation.transform.SetAsLastSibling();
myAnimation.MatchRectTransformWithBounds();
myAnimation.raycastTarget = false;
myAnimation.Initialize(true);
return myAnimation;
}
return null;
}
public string Path()
{
string myPath = "";
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
myPath = Application.dataPath;
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
myPath = Application.persistentDataPath;
return myPath;
}
}
|