版本: Unity2019 Spine3.8
1.预制体的动态加载
预制体已经挂载了SkeletonAnimation组件,并包含了SkeletonDataAsset数据,
设置初始动画,是否循环等
代码上,只需要实例化预制体、获取SkeletonAnimation 组件等
GameObject go = Object.Instantiate(spinePrefab, spawnLoaclPosition, Quaternion.identity);
go.transform.localPosition = spawnLoaclPosition;
go.transform.SetParent(this.transform, false);
SkeletonAnimation sa = spinePrefab.GetComponentInChildren<SkeletonAnimation>();
var sda = sa.skeletonDataAsset;
if (sda == null) return null;
sda.GetSkeletonData(false);
go.name = sda.name.ToString();
var spineAnimation = sda.GetSkeletonData(false).FindAnimation(defaultAnimation);
DoExtraStuff(sa, spineAnimation);
2.Export文件的动态加载
Unity已经导入三个文件
xxxx.atlas.txt
xxxx.json
xxxx.png
通过这三个文件可以依次获取SkeletonDataAsset数据、SpineAtlasAsset数据
然后在通过脚本来初始化游戏对象,并挂载SkeletonAnimation组件
部分代码如下
//变量
public TextAsset skeletonJson;
public TextAsset atlasText;
public Texture2D[] textures;
public Material materialPropertySource;
private SpineAtlasAsset runtimeSAA;
private SkeletonDataAsset runtimeSDA;
private SkeletonAnimation runtimeSA;
//逻辑
runtimeSAA = SpineAtlasAsset.CreateRuntimeInstance(atlasText, textures, materialPropertySource, true);
runtimeSDA = SkeletonDataAsset.CreateRuntimeInstance(skeletonJson, runtimeSAA, true);
runtimeSDA.GetSkeletonData(false); //预加载
runtimeSDA.name = skeletonJson.name + "_SkeletonData";
runtimeSA = SkeletonAnimation.NewSkeletonAnimationGameObject(runtimeSDA); //初始化游戏对象
var spineAnimation = runtimeSDA.GetSkeletonData(false).FindAnimation(defaultAnimation);
runtimeSA.transform.localPosition = spawnLoaclPosition;
runtimeSA.transform.SetParent(this.transform, false);
DoExtraStuff(runtimeSA, spineAnimation); //此方法做一些额外的工作
runtimeSA.transform.gameObject.name = runtimeSDA.name.ToString();