项目里需要在Unity里装入Spine插件
下载地址: http://zh.esotericsoftware.com/
通过代码动态加载Spine动画文件
private SkeletonGraphic LoadSpineAnimationFromResource(string path, Transform transform)
{
Material material = Resources.Load<Material>("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;
}
mySkeletonGraphic = LoadSpineAnimationFromResource("Animation/test_SkeletonData", transform);
mySkeletonGraphic.AnimationState.SetAnimation(0, "run", true);
mySkeletonGraphic.AnimationState.Complete += delegate
{
mySkeletonGraphic.AnimationState.TimeScale = 0;
};
通过代码动态加载Spine动画资源文件(AB包格式)
SkeletonGraphic LoadSpineAnimationFromABPackage(string path, string dataAssetName, Transform transform)
{
AssetBundle bundle = AssetBundle.LoadFromFile(path, 0);
if (bundle != null)
{
Material material = Resources.Load<Material>("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;
}
mySkeletonGraphic2 = LoadSpineAnimationFromABPackage(Path() + "/Resources/Animation/iOS/spine.test", "test_SkeletonData", transform);
mySkeletonGraphic2.AnimationState.SetAnimation(0,"run",true);
mySkeletonGraphic2.AnimationState.Complete += delegate {
mySkeletonGraphic2.gameObject.SetActive(false);
};
|