FairyGUI插⼊Spine
最近FairyGui新添了对Spine动画功能的⽀持,然后项⽬中就想⽤Spine动画做⼀些表现。然后按照官⽅⽂档上介绍的步骤在Unity中添加对Spine 的⽀持,搞了半天,项⽬中运⾏动态加载Spine,要么就是完全看不到,要么就是⼀个⿊⽚。通过查看官⽅问答和询问其它⼈,也没到解决⽅案。最终放弃直接使⽤FairyGUI的Loader3D加载Spine,改⽤FairyGui嵌套UGUI加载Spine。Spine对Unity原⽣的UGUI有更好的⽀持。操作步骤如下:
1、⾸先Spine官⽹下载Spine-unity包(我使⽤的是spine-unity-3.8),导⼊Unity;
2、添加做好的Spine动画资源到Unity中;
导⼊动画资源时要注意,美术给的资源包含三个⽂件,如图2-1所⽰,⼀个图⽚,⼀个图集⽂本⽂件,⼀个skel⽂件。导⼊Unity前需要将skel⽂件添加.bytes后缀,然后拖⼊Unity才能正常⽣成运⾏需要的⽂件。最终⽣成的⽂件如图2-2所⽰,会⽣成⼀个图集对应的asset,⼀个skeletondata对应的asset,⼀个材质。
图2-1
图2-2
3、⾸先在FairyGUI添加⼀个Graph组件,作为容器,添加UGUI的Canvas组件;
4、然后添加SkeletonGraphic组件,设置SkeletonGraphic组件使⽤材质为SkeletonGraphicDefault.material;
5、加载SkeletonDataAsset资源,设置SkeletonGraphic
代码如下:
public static void AddSpine(GGraph holder, string spineName, string aniName, float posX, float posY, float scale = 1)
{
SkeletonGraphic sg;
GameObject ngo = new GameObject("Canvas");
Canvas canvas = ngo.AddComponent<Canvas>();
canvas.worldCamera = StageCamera.main;  //相机使⽤FairyGUI的StageCamera
GoWrapper gw = new GoWrapper(canvas.gameObject);
holder.SetNativeObject(gw);
GameObject sgGo = new GameObject("SkeletonGraphic");
sg = sgGo.AddComponent<SkeletonGraphic>();
ansform.localScale = ;
ansform.localPosition = ;
sg.raycastTarget = false;
sg.material = Resources.Load<Material>("spine/materials/SkeletonGraphicDefault");
sg.skeletonDataAsset = Resources.Load<SkeletonDataAsset>($"spine/{spineName}");  //加载skel资源          /*执⾏以下两步保证资源替换后,能正确渲染
sg.Initialize(true);
sg.Rebuild(UnityEngine.UI.CanvasUpdate.PreRender);
/*重新设置动画
sg.Skeleton.SetToSetupPose();
sg.AnimationState.ClearTracks();
sg.AnimationState.SetAnimation(0, aniName, true);
/*设置偏移和缩放
holder.SetScale(scale, scale);
holder.SetPosition(posX, posY, holder.position.z);
unity3d animationholder.visible = true;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。