刚开始想使用把spine动画做成预设体加载出来,但是加载之后无法脚本控制spine的动画片段,整个spine会维持在做成预设体的设置.
后来改变想法把动画所需的AssetData和Material动态加载,然后用着两个资源文件去生成spine动画,代码如下:
IEnumerator getABPack()
{
var unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(path, 0);
yield return unityWebRequest.SendWebRequest();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
SkeletonDataAsset skeletonDataAsset = bundle.LoadAsset<SkeletonDataAsset>("skeleton_SkeletonData");
Material material = bundle.LoadAsset<Material>("SkeletonGraphicDefault");
SkeletonGraphic skeletonGraphic = SkeletonGraphic.NewSkeletonGraphicGameObject(skeletonDataAsset, transform.parent, material);
// 要把spine引入unity的Runtime库下面的SkeletonGraphicDefault材质球也打包,并且加载之后设置shader才可以正确显示,否则会丢失材质
skeletonGraphic.material = bundle.LoadAsset<Material>("SkeletonGraphicDefault");
skeletonGraphic.material.shader = Shader.Find("Spine/SkeletonGraphic");
skeletonGraphic.AnimationState.SetAnimation(0, "run", true);
bundle.Unload(false);
}
// SkeletonAnimation的动态创建方法
// SkeletonAnimation.NewSkeletonAnimationGameObject(skeletonDataAsset);
// 获取动画原尺寸 sizeDelta
// skeletonGraphic.MatchRectTransformWithBounds()