目录
继Unity | Spine动画动态加载 之后,本文进一步介绍自动获取Assets中的spine资源,并制作成预制体,Editor下一键打成AB包。
一、收集SkeletonDataAsset
寻找Assets下所有的SkeletonDataAsset。
public List<SkeletonDataAsset> skeletonDataAssets = new List<SkeletonDataAsset>();
/// <summary>
/// 寻找所有的SkeletonDataAsset
/// </summary>
private void FindAllSkeletonDataAssets()
{
#if UNITY_EDITOR
string[] assetGuids = AssetDatabase.FindAssets("t:SkeletonDataAsset");
foreach (string guid in assetGuids)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
SkeletonDataAsset skeletonDataAsset = AssetDatabase.LoadAssetAtPath<SkeletonDataAsset>(assetPath);
if (skeletonDataAsset != null)
{
skeletonDataAssets.Add(skeletonDataAsset);
}
}
#endif
}
二、制作预制体
制作预制体,并设置AB标签。
public Material materialPropertySource;
void MakePrefabs(SkeletonDataAsset runtimeSkeletonDataAsset)
{
string name = runtimeSkeletonDataAsset.name;
name = name.Substring(0, name.IndexOf("_"));
GameObject gameObject = new GameObject(name);
gameObject.transform.parent = GameObject.Find("Canvas").transform;
gameObject.transform.localPosition = new Vector3(0, 0, 0);
SkeletonGraphic spineGameObject = gameObject.AddComponent<SkeletonGraphic>();
spineGameObject.skeletonDataAsset = runtimeSkeletonDataAsset;
spineGameObject.material = materialPropertySource;
spineGameObject.allowMultipleCanvasRenderers = true;
//设置skin
spineGameObject.initialSkinName = spineGameObject.Skeleton.Data.Skins.Items[0].Name;
//设置动画
List<string> animsName = new List<string>();
foreach (var