public void LinkLoop(Frame last) { for (int i = 0; i < bonesinfo.Count; i++) { bonesinfo[i].Tag(last.bonesinfo[i]); } }
static void CreateFlyItem() { var obj = ScriptableObject.CreateInstance <FB.PosePlus.AniClip>(); var f = new FB.PosePlus.Frame(); f.box_key = true; f.dot_key = true; f.key = true; obj.frames = new List <FB.PosePlus.Frame>(); obj.frames.Add(f); string outpath = GetCreatePath(); AssetDatabase.CreateAsset(obj, outpath); }
public Frame(Frame last, int _fid, IList<Transform> trans) { //Debug.LogWarning("bones=" + trans.Length); this.fid = _fid; this.key = true; //bonesinfo = new PoseBoneMatrix[trans.Count]; for (int i = 0; i < trans.Count; i++) { PoseBoneMatrix b = new PoseBoneMatrix(); bonesinfo.Add(b); bonesinfo[i].Record(trans[i], last == null ? null : last.bonesinfo[i]); } }
public static Frame Lerp(Frame left, Frame right, float lerp) { Frame f = new Frame(); f.key = false; f.fid = left.fid; if (left.boneinfo != null && right.boneinfo != null && left.bonehash != right.bonehash) { Dictionary<string, int3> rebone = new Dictionary<string, int3>(); for (int i = 0; i < left.boneinfo.Count; i++) { string b = left.boneinfo[i]; int3 it = new int3(rebone.Count, i, -1); rebone[b] = it; } for (int i = 0; i < right.boneinfo.Count; i++) { string b = right.boneinfo[i]; if (rebone.ContainsKey(b) == false) { int3 it = new int3(rebone.Count, -1, i); rebone[b] = it; } else { rebone[b].boneright = i; } } //Debug.LogWarning("bone mix:"+rebone.Count); f.boneinfo = new List<string>(rebone.Keys); f.bonesinfo = new List<PoseBoneMatrix>(new PoseBoneMatrix[rebone.Count]); foreach (var v in rebone.Values) { if (v.boneleft == -1) f.bonesinfo[v.idnew] = right.bonesinfo[v.boneright].Clone() as PoseBoneMatrix; else if (v.boneright == -1) f.bonesinfo[v.idnew] = left.bonesinfo[v.boneleft].Clone() as PoseBoneMatrix; else { //Debug.Log("bc:" + v.idnew + "/" + f.bonesinfo.Count); //Debug.Log("lb:" + v.boneleft + "/" + left.bonesinfo.Count); //Debug.Log("rb:" + v.boneright + "/" + right.bonesinfo.Count); f.bonesinfo[v.idnew] = PoseBoneMatrix.Lerp(left.bonesinfo[v.boneleft], right.bonesinfo[v.boneright], lerp); } } } else {//single mix //f.bonesinfo = new PoseBoneMatrix[left.bonesinfo.Length]; f.bonesinfo = new List<PoseBoneMatrix>(left.bonesinfo); if (left.boneinfo != null) { f.boneinfo = left.boneinfo; f.bonehash = left.bonehash; } for (int i = 0; i < f.bonesinfo.Count; i++) { f.bonesinfo[i] = PoseBoneMatrix.Lerp(left.bonesinfo[i], right.bonesinfo[i], lerp); } } return f; }
//[SerializeField] //public List<AniBoxCollider> boxesinfo = new List<AniBoxCollider>(); //[SerializeField] //public List<Dot> dotesinfo = new List<Dot>(); //[SerializeField] //public List<Effect> effectList = new List<Effect>(); //[SerializeField] //public List<String> aduioList = new List<string>(); public object Clone() { Frame fnew = new Frame(); fnew.fid = this.fid; fnew.key = this.key; fnew.bonesinfo = new List<PoseBoneMatrix>(bonesinfo); return fnew; }
public static FB.PosePlus.AniClip ReadAniClip(System.IO.Stream s) { var buf4 = new byte[4]; var clip = new FB.PosePlus.AniClip(); //name int slen = s.ReadByte(); byte[] buf = new byte[slen]; s.Read(buf, 0, slen); clip.name = System.Text.Encoding.UTF8.GetString(buf); //fps s.Read(buf4, 0, 4); clip.fps = BitConverter.ToSingle(buf4, 0); //loop clip.loop = s.ReadByte() > 0; { //boneinfo s.Read(buf4, 0, 4); int bcount = BitConverter.ToInt32(buf4, 0); for (int i = 0; i < bcount; i++) { slen = s.ReadByte(); buf = new byte[slen]; s.Read(buf, 0, slen); string bone = System.Text.Encoding.UTF8.GetString(buf); clip.boneinfo.Add(bone); } } { //subclips s.Read(buf4, 0, 4); int scount = BitConverter.ToInt32(buf4, 0); for (int i = 0; i < scount; i++) { FB.PosePlus.SubClip sc = new FB.PosePlus.SubClip(); slen = s.ReadByte(); buf = new byte[slen]; s.Read(buf, 0, slen); sc.name = System.Text.Encoding.UTF8.GetString(buf); sc.loop = s.ReadByte() > 0; s.Read(buf4, 0, 4); sc.startframe = BitConverter.ToUInt32(buf4, 0); s.Read(buf4, 0, 4); sc.endframe = BitConverter.ToUInt32(buf4, 0); clip.subclips.Add(sc); } } {//frame s.Read(buf4, 0, 4); int fcount = BitConverter.ToInt32(buf4, 0); for (int i = 0; i < fcount; i++) { FB.PosePlus.Frame f = new FB.PosePlus.Frame(); s.Read(buf4, 0, 4); f.fid = BitConverter.ToInt32(buf4, 0); f.key = s.ReadByte() > 0; clip.frames.Add(f); for (int ib = 0; ib < clip.boneinfo.Count; ib++) { clip.frames[i].bonesinfo.Add(new FB.PosePlus.PoseBoneMatrix()); clip.frames[i].bonesinfo[ib].Load(s, i > 0 ? clip.frames[i - 1].bonesinfo[ib] : null); } } } return(clip); }
public static FB.PosePlus.AniClip ReadAniClip(System.IO.Stream s) { var buf4 = new byte[4]; var clip = new FB.PosePlus.AniClip(); //name int slen = s.ReadByte(); byte[] buf = new byte[slen]; s.Read(buf, 0, slen); clip.name = System.Text.Encoding.UTF8.GetString(buf); //fps s.Read(buf4, 0, 4); clip.fps = BitConverter.ToSingle(buf4, 0); //loop clip.loop = s.ReadByte() > 0; { //boneinfo s.Read(buf4, 0, 4); int bcount = BitConverter.ToInt32(buf4, 0); for (int i = 0; i < bcount; i++) { slen = s.ReadByte(); buf = new byte[slen]; s.Read(buf, 0, slen); string bone = System.Text.Encoding.UTF8.GetString(buf); clip.boneinfo.Add(bone); } } { //subclips s.Read(buf4, 0, 4); int scount = BitConverter.ToInt32(buf4, 0); for (int i = 0; i < scount; i++) { FB.PosePlus.SubClip sc = new FB.PosePlus.SubClip(); slen = s.ReadByte(); buf = new byte[slen]; s.Read(buf, 0, slen); sc.name = System.Text.Encoding.UTF8.GetString(buf); sc.loop = s.ReadByte() > 0; s.Read(buf4, 0, 4); sc.startframe = BitConverter.ToUInt32(buf4,0); s.Read(buf4, 0, 4); sc.endframe = BitConverter.ToUInt32(buf4,0); clip.subclips.Add(sc); } } {//frame s.Read(buf4, 0, 4); int fcount = BitConverter.ToInt32(buf4, 0); for (int i = 0; i < fcount; i++) { FB.PosePlus.Frame f = new FB.PosePlus.Frame(); s.Read(buf4, 0, 4); f.fid = BitConverter.ToInt32(buf4, 0); f.key = s.ReadByte() > 0; clip.frames.Add(f); for (int ib = 0; ib < clip.boneinfo.Count; ib++) { clip.frames[i].bonesinfo.Add(new FB.PosePlus.PoseBoneMatrix()); clip.frames[i].bonesinfo[ib].Load(s, i > 0 ? clip.frames[i - 1].bonesinfo[ib] : null); } } } return clip; }
void MoveData(Operation opera) { int count = clip_a.frames.Count - clip_b.frames.Count; string str = ""; string plan = ""; if (opera == Operation.left_box || opera == Operation.left_dot || opera == Operation.left_effct || opera == Operation.left_adudio) { if (count > 0) { str = "左边多于右边" + Math.Abs(count).ToString() + "帧.\n 是否移动到右边?"; plan = "\n 覆盖:将增加右边帧 复制:复制右边帧数内容,帧数不变"; } else if (count < 0) { str = "左边少于右边" + Math.Abs(count).ToString() + "帧.\n 是否移动到右边?"; plan = "\n 覆盖:将减少右边帧 复制:仅复制"; } else { str = "左边等于于右边帧.\n 是否移动到右边?"; plan = "\n 覆盖:帧数不变 复制:帧数不变"; } } else if (opera == Operation.right_box || opera == Operation.right_dot || opera == Operation.right_audio || opera == Operation.right_effct) { if (count > 0) { str = "右边多于左边" + Math.Abs(count).ToString() + "帧.\n 是否移动到左边?"; plan = "\n 覆盖:将增加左边帧 复制:复制左边帧数内容,帧数不变"; } else if (count < 0) { str = "右边少于左边" + Math.Abs(count).ToString() + "帧.\n 是否移动到左边?"; plan = "\n 覆盖:将减少左边帧 复制:仅复制"; } else { str = "右边等于左边帧.\n 是否移动到左边?"; plan = "\n 覆盖:帧数不变 复制:帧数不变"; } } // if (EditorUtility.DisplayDialog("提示", str + plan, "覆盖", "复制")) { if (count >= 0) { for (int i = 0; i < count; i++) { FB.PosePlus.Frame f = new FB.PosePlus.Frame(); if (opera == Operation.left_box || opera == Operation.left_dot || opera == Operation.left_effct || opera == Operation.left_adudio) { clip_b.frames.Add(f); } else if (opera == Operation.right_box || opera == Operation.right_dot || opera == Operation.right_audio || opera == Operation.right_effct) { if (clip_a.frames.Count > 0) { clip_a.frames.RemoveAt(clip_a.frames.Count - 1); } } } } else if (count < 0) { for (int i = 0; i < Math.Abs(count); i++) { FB.PosePlus.Frame f = new FB.PosePlus.Frame(); if (opera == Operation.left_box || opera == Operation.left_dot || opera == Operation.left_effct || opera == Operation.left_adudio) { if (clip_b.frames.Count > 0) { clip_b.frames.RemoveAt(clip_b.frames.Count - 1); } else if (opera == Operation.right_box || opera == Operation.right_dot || opera == Operation.right_audio || opera == Operation.right_effct) { clip_a.frames.Add(f); } } } } switch (opera) { case Operation.left_box: { lastoperation = Operation.left_box; for (int i = 0; i < clip_a.frames.Count; i++) { clip_b.frames[i].box_key = clip_a.frames[i].box_key; clip_b.frames[i].boxesinfo = new List <FB.PosePlus.AniBoxCollider>(clip_a.frames[i].boxesinfo); } } break; case Operation.left_dot: { lastoperation = Operation.left_dot; for (int i = 0; i < clip_a.frames.Count; i++) { clip_b.frames[i].dotesinfo = new List <FB.PosePlus.Dot>(clip_a.frames[i].dotesinfo); } } break; case Operation.left_effct: { lastoperation = Operation.left_effct; for (int i = 0; i < clip_a.frames.Count; i++) { clip_b.frames[i].effectList = new List <FB.PosePlus.Effect>(clip_a.frames[i].effectList); } } break; case Operation.left_adudio: { lastoperation = Operation.left_adudio; for (int i = 0; i < clip_a.frames.Count; i++) { clip_b.frames[i].aduioList = new List <string>(clip_a.frames[i].aduioList); } } break; case Operation.right_box: { lastoperation = Operation.right_box; for (int i = 0; i < clip_b.frames.Count; i++) { clip_a.frames[i].box_key = clip_b.frames[i].box_key; clip_a.frames[i].boxesinfo = new List <FB.PosePlus.AniBoxCollider>(clip_b.frames[i].boxesinfo); } } break; case Operation.right_dot: { lastoperation = Operation.right_dot; for (int i = 0; i < clip_b.frames.Count; i++) { clip_a.frames[i].dotesinfo = new List <FB.PosePlus.Dot>(clip_b.frames[i].dotesinfo); } } break; case Operation.right_effct: { lastoperation = Operation.right_effct; for (int i = 0; i < clip_a.frames.Count; i++) { clip_a.frames[i].effectList = new List <FB.PosePlus.Effect>(clip_b.frames[i].effectList); } } break; case Operation.right_audio: { lastoperation = Operation.right_audio; for (int i = 0; i < clip_a.frames.Count; i++) { clip_a.frames[i].aduioList = new List <string>(clip_b.frames[i].aduioList); } } break; default: break; } } else { switch (opera) { case Operation.left_box: { lastoperation = Operation.left_box; for (int i = 0; i < clip_b.frames.Count; i++) { if (i > clip_a.frames.Count - 1) { break; } clip_b.frames[i].boxesinfo = new List <FB.PosePlus.AniBoxCollider>(clip_a.frames[i].boxesinfo); } } break; case Operation.left_dot: { lastoperation = Operation.left_dot; for (int i = 0; i < clip_b.frames.Count; i++) { if (i > clip_a.frames.Count - 1) { break; } clip_b.frames[i].dotesinfo = new List <FB.PosePlus.Dot>(clip_a.frames[i].dotesinfo); } } break; case Operation.left_effct: { lastoperation = Operation.left_effct; for (int i = 0; i < clip_b.frames.Count; i++) { if (i > clip_a.frames.Count - 1) { break; } clip_b.frames[i].effectList = new List <FB.PosePlus.Effect>(clip_a.frames[i].effectList); } } break; case Operation.left_adudio: { lastoperation = Operation.left_adudio; for (int i = 0; i < clip_b.frames.Count; i++) { if (i > clip_a.frames.Count - 1) { break; } clip_b.frames[i].aduioList = new List <string>(clip_a.frames[i].aduioList); } } break; case Operation.right_box: { lastoperation = Operation.right_box; for (int i = 0; i < clip_a.frames.Count; i++) { if (i > clip_b.frames.Count - 1) { break; } clip_a.frames[i].box_key = clip_b.frames[i].box_key; clip_a.frames[i].boxesinfo = new List <FB.PosePlus.AniBoxCollider>(clip_b.frames[i].boxesinfo); } } break; case Operation.right_dot: { lastoperation = Operation.right_dot; for (int i = 0; i < clip_b.frames.Count; i++) { if (i > clip_b.frames.Count - 1) { break; } clip_a.frames[i].dotesinfo = new List <FB.PosePlus.Dot>(clip_b.frames[i].dotesinfo); } } break; case Operation.right_effct: { lastoperation = Operation.right_effct; for (int i = 0; i < clip_a.frames.Count; i++) { if (i > clip_b.frames.Count - 1) { break; } clip_a.frames[i].effectList = new List <FB.PosePlus.Effect>(clip_b.frames[i].effectList); } } break; case Operation.right_audio: { lastoperation = Operation.right_audio; for (int i = 0; i < clip_a.frames.Count; i++) { if (i > clip_b.frames.Count - 1) { break; } clip_a.frames[i].aduioList = new List <string>(clip_b.frames[i].aduioList); } } break; default: break; } } }
public static Frame Lerp(Frame left, Frame right, float lerp) { Frame f = new Frame(); f.key = false; f.fid = left.fid; //f.bonesinfo = new PoseBoneMatrix[left.bonesinfo.Length]; f.bonesinfo = new List<PoseBoneMatrix>(left.bonesinfo); for (int i = 0; i < f.bonesinfo.Count; i++) { f.bonesinfo[i] = PoseBoneMatrix.Lerp(left.bonesinfo[i], right.bonesinfo[i], lerp); } return f; }
public void Play(AniClip clip, SubClip clipsub = null, float crosstimer = 0) { if (clipsub != null) { bLooped = clipsub.loop; startframe = (int)clipsub.startframe; endframe = (int)clipsub.endframe; if (_fps < 0) { _fps = clip.fps; } } else if (clip != null) { bLooped = clip.loop; startframe = 0; endframe = (clip.aniFrameCount - 1); if (_fps < 0) { _fps = clip.fps; } } if (crosstimer <= 0) { this._crossTimer = -1; crossFrame = null; lastClip = clip; lastframe = startframe; SetPose(clip, startframe, true); frameNow = lastClip.frames[lastframe]; } else { if (lastClip != null && lastframe >= 0 && lastframe < lastClip.frames.Count) { RecCrossFrame(); lastClip = clip; lastframe = startframe; } else { lastClip = clip; lastframe = startframe; SetPose(clip, startframe, true); frameNow = lastClip.frames[lastframe]; } this._crossTimerTotal = this._crossTimer = crosstimer; } }
public void SetPoseLerp(Frame src, Frame dest, float lerp) { var frame = Frame.Lerp(src, dest, lerp); SetPose(frame); }
public void SetPose(Frame frame) { if (frame.bonehash != transcode) { trans = new Transform[frame.boneinfo.Count]; for (int i = 0; i < frame.boneinfo.Count; i++) { trans[i] = this.transform.Find(frame.boneinfo[i]); } transcode = frame.bonehash; } for (int i = 0; i < trans.Length; i++) { frame.bonesinfo[i].UpdateTran(trans[i], true); } }
public void _OnUpdate(float delta) { //帧推行 if (lastClip == null) return; //打中暂停机制 if (pauseframe > 0) { pauseTimer += delta; int pid = (int)((timer + pauseTimer) * _fps); if (pid - lastframeid >= pauseframe) { pauseframe = 0; pauseTimer = 0; } else { return; } } timer += delta; bool crossend = false; if (_crossTimer >= 0) { _crossTimer -= delta; if (_crossTimer <= 0) crossend = true; } int frameid = (int)(timer * _fps);//这里要用一个稳定的fps,就用播放的第一个动画的fps作为稳定fps if (frameid == lastframeid) return; if (frameid > lastframeid + 1)//增加一个限制,不准动画跳帧 { frameid = lastframeid + 1; timer = (float)frameid / _fps; } lastframeid = frameid; //帧前行 int frame = lastframe + 1; if (frame > endframe) { if (bLooped) { frame = startframe; } else { frame = endframe; } } //设置动作或者插值 if (crossend) { crossFrame = null; SetPose(lastClip, frame, true); return; } if (_crossTimer >= 0) { //_crossTimer -= delta; //if (_crossTimer < 0) //{ // crossFrame = null; // SetPose(lastClip, frame, true); // return; //} if (crossFrame != null) { float l = 1.0f - _crossTimer / _crossTimerTotal; lastframe = frame; frameNow = lastClip.frames[frame]; frameNow.boneinfo = lastClip.boneinfo; frameNow.bonehash = lastClip.bonehash; SetPoseLerp(crossFrame, frameNow, l); } } else { if (frame != lastframe) { SetPose(lastClip, frame); frameNow = lastClip.frames[frame]; } } //更新角色闪烁 if (flashTime > 0) { flashTime--; UpdateFlash(); } }
void RecCrossFrame() { if (this._crossTimer >= 0 && crossFrame != null) { Frame f = new Frame(); float l = 1.0f - _crossTimer / _crossTimerTotal; lastClip.frames[lastframe].boneinfo = lastClip.boneinfo; lastClip.frames[lastframe].bonehash = lastClip.bonehash; crossFrame = Frame.Lerp(crossFrame, lastClip.frames[lastframe], l); } else { crossFrame = lastClip.frames[lastframe]; crossFrame.boneinfo = lastClip.boneinfo; crossFrame.bonehash = lastClip.bonehash; } }
public void SetPoseLerp(Frame src, Frame dest, float lerp) { for (int i = 0; i < trans.Length; i++) { src.bonesinfo[i].UpdateTranLerp(trans[i], dest.bonesinfo[i], lerp); } }
void CloneAni(AnimationClip clip, float fps) { var ani = target as Animator; //创建CleanData.Ani FB.PosePlus.AniClip _clip = ScriptableObject.CreateInstance <FB.PosePlus.AniClip>(); _clip.boneinfo = new List <string>();//也增加了每个动画中的boneinfo信息. //这里重新检查动画曲线,找出动画中涉及的Transform部分,更精确 List <Transform> cdpath = new List <Transform>(); var curveDatas = AnimationUtility.GetCurveBindings(clip); //AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(clip, true); foreach (var dd in curveDatas) { Transform tran = ani.transform.Find(dd.path); if (cdpath.Contains(tran) == false) { _clip.boneinfo.Add(dd.path); cdpath.Add(tran); } } Debug.LogWarning("curve got path =" + cdpath.Count); string path = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(clip.GetInstanceID())); _clip.name = clip.name; _clip.frames = new List <FB.PosePlus.Frame>(); _clip.fps = fps; _clip.loop = clip.isLooping; float flen = (clip.length * fps); int framecount = (int)flen; if (flen - framecount > 0.0001) { framecount++; } //if (framecount < 1) framecount = 1; framecount += 1; FB.PosePlus.Frame last = null; //ani.StartPlayback(); //逐帧复制 //ani.Play(_clip.name, 0, 0); for (int i = 0; i < framecount; i++) { ani.Play(_clip.name, 0, (i * 1.0f / fps) / clip.length); ani.Update(0); last = new FB.PosePlus.Frame(last, i, cdpath); _clip.frames.Add(last); } if (_clip.loop) { _clip.frames[0].LinkLoop(last); } Debug.Log("FrameCount." + framecount); FB.PosePlus.AniPlayer con = ani.GetComponent <FB.PosePlus.AniPlayer>(); List <FB.PosePlus.AniClip> clips = null; if (con.clips != null) { clips = new List <FB.PosePlus.AniClip>(con.clips); } else { clips = new List <FB.PosePlus.AniClip>(); } foreach (var c in clips) { if (c.name == _clip.name + ".FBAni") { clips.Remove(c); break; } } //ani.StopPlayback(); string outpath = path + "/" + clip.name + ".FBAni.asset"; AssetDatabase.CreateAsset(_clip, outpath); var src = AssetDatabase.LoadAssetAtPath(outpath, typeof(FB.PosePlus.AniClip)) as FB.PosePlus.AniClip; //设置clip //FB.CleanData.AniController con = ani.GetComponent<FB.CleanData.AniController>(); clips.Add(src); con.clips = clips; }
public static void AddClip(AnimationClip clip, float fps, Animation ani) { FB.PosePlus.AniClip _clip = ScriptableObject.CreateInstance <FB.PosePlus.AniClip>(); _clip.boneinfo = new List <string>();//也增加了每个动画中的boneinfo信息. //这里重新检查动画曲线,找出动画中涉及的Transform部分,更精确 List <Transform> cdpath = new List <Transform>(); var curveDatas = AnimationUtility.GetCurveBindings(clip); FB.PosePlus.AniPlayer con = ani.GetComponent <FB.PosePlus.AniPlayer>(); UpdateCdpath(ani, curveDatas, _clip, con, cdpath); string path = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(clip.GetInstanceID())); _clip.name = clip.name; _clip.frames = new List <FB.PosePlus.Frame>(); _clip.fps = fps; _clip.loop = clip.isLooping; float flen = (clip.length * fps); int framecount = (int)flen; if (flen - framecount > 0.0001) { framecount++; } for (int i = 0, l = clip.events.Length; i < l; i++) { var aniEvent = clip.events[i]; FrameEvent evt = new FrameEvent(); evt.name = aniEvent.functionName; evt.position = aniEvent.time; evt.intVariable = aniEvent.intParameter; evt.floatVariable = aniEvent.floatParameter; evt.stringVariable = aniEvent.stringParameter; _clip.events.Add(evt); } framecount += 1; FB.PosePlus.Frame last = null; for (int i = 0; i < framecount; i++) { ani[_clip.name].time = (i * 1.0f / fps) / clip.length; ani[_clip.name].enabled = true; ani[_clip.name].weight = 1; ani.Sample(); ani[_clip.name].enabled = false; last = new FB.PosePlus.Frame(_clip, con, last, i, ani.transform, cdpath); _clip.frames.Add(last); } Dictionary <string, int> clipcache = new Dictionary <string, int>(); if (con.clips != null) { for (int i = 0, l = con.clips.Count; i < l; i++) { if (con.clips[i]) { clipcache[con.clips[i].name] = i; Debug.Log(con.clips[i].name); } else { con.clips.RemoveAt(i); i--; l--; } } } con.clipcache = clipcache; string outpath = path + "/" + clip.name + ".FBAni.asset"; FB.PosePlus.AniClip src = null; /*if (Pretreatment.AnimatorCache.ContainsKey(outpath)) * { * src = Pretreatment.AnimatorCache[outpath]; * } * else * { * AssetDatabase.CreateAsset(_clip, outpath); * src = AssetDatabase.LoadAssetAtPath(outpath, typeof(FB.PosePlus.AniClip)) as FB.PosePlus.AniClip; * Pretreatment.AnimatorCache[outpath] = src; * }*/ AssetDatabase.CreateAsset(_clip, outpath); src = AssetDatabase.LoadAssetAtPath(outpath, typeof(FB.PosePlus.AniClip)) as FB.PosePlus.AniClip; Pretreatment.AnimatorCache[outpath] = src; con.AddAni(src); }
//从一个Animator中获取所有的Animation public static void CloneAni(UnityEngine.AnimationClip clip, float fps, Animator ani) { //创建CleanData.Ani FB.PosePlus.AniClip _clip = ScriptableObject.CreateInstance <FB.PosePlus.AniClip>(); _clip.boneinfo = new List <string>();//也增加了每个动画中的boneinfo信息. //这里重新检查动画曲线,找出动画中涉及的Transform部分,更精确 List <Transform> cdpath = new List <Transform>(); var curveDatas = AnimationUtility.GetCurveBindings(clip); //AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(clip, true); foreach (var dd in curveDatas) { Transform tran = ani.transform.Find(dd.path); if (tran == null) { Debug.LogWarning("trans not found:" + dd.path); //丢弃无法被找到的动画通道 } else { if (cdpath.Contains(tran) == false) { _clip.boneinfo.Add(tran.name); cdpath.Add(tran); } } } FB.PosePlus.AniPlayer con = ani.gameObject.GetComponent <FB.PosePlus.AniPlayer>(); foreach (var b in con.bones) { //if (b.bone.GetComponent<asbone>() != null) { //特别关注的骨骼 if (_clip.boneinfo.Contains(b.bone.name) == false) { _clip.boneinfo.Add(b.bone.name); cdpath.Add(b.bone); } } } Debug.LogWarning("curve got path =" + cdpath.Count); string path = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(clip.GetInstanceID())); _clip.name = clip.name; _clip.frames = new List <FB.PosePlus.Frame>(); _clip.fps = fps; _clip.loop = clip.isLooping; float flen = (clip.length * fps); int framecount = (int)flen; if (flen - framecount > 0.0001) { framecount++; } //if (framecount < 1) framecount = 1; for (int i = 0, l = clip.events.Length; i < l; i++) { var aniEvent = clip.events[i]; FrameEvent evt = new FrameEvent(); evt.name = aniEvent.functionName; evt.position = aniEvent.time; evt.intVariable = aniEvent.intParameter; evt.floatVariable = aniEvent.floatParameter; evt.stringVariable = aniEvent.stringParameter; _clip.events.Add(evt); } framecount += 1; FB.PosePlus.Frame last = null; //ani.StartPlayback(); //逐帧复制 //ani.Play(_clip.name, 0, 0); for (int i = 0; i < framecount; i++) { ani.Play(mapClip2State[_clip.name], 0, (i * 1.0f / fps) / clip.length); ani.Update(0); last = new FB.PosePlus.Frame(_clip, con, last, i, ani.transform, cdpath); _clip.frames.Add(last); } //特殊处理:写文件后,clips中的内容会丢失 //这里用clipcache保存clips的内容 //Debug.Log(con.clips[0].name); Dictionary <string, int> clipcache = new Dictionary <string, int>(); if (con.clips != null) { for (int i = 0; i < con.clips.Count; i++) { if (con.clips[i]) { clipcache[con.clips[i].name] = i; } else { con.clips.RemoveAt(i); } } } con.clipcache = clipcache; string outpath = PathHelper.CheckFileName(path + "/" + ani.gameObject.name + "_" + clip.name + ".FBAni.asset"); /*FB.PosePlus.AniClip src = null; * if (Pretreatment.AnimatorCache.ContainsKey(outpath)) * { * src = Pretreatment.AnimatorCache[outpath]; * } * else * { * AssetDatabase.CreateAsset(_clip, outpath); * src = AssetDatabase.LoadAssetAtPath(outpath, typeof(FB.PosePlus.AniClip)) as FB.PosePlus.AniClip; * Pretreatment.AnimatorCache[outpath] = src; * }*/ AssetDatabase.CreateAsset(_clip, outpath); var src = AssetDatabase.LoadAssetAtPath(outpath, typeof(FB.PosePlus.AniClip)) as FB.PosePlus.AniClip; Pretreatment.AnimatorCache[outpath] = src; src.clipName = PathHelper.CheckFileName(clip.name); con.AddAni(src); }