using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
public class PlayAnimation : Editor
{
public float m_CurTime;
private bool m_HasBake;
private bool m_Playing;
private float m_RunningTime;
private double m_PreviousTime;
private double nowTime;
private float stopTime;
private float m_RecorderStopTime;
private type _type=type.forward;
public float kDuration = 0.0f;
private Animation m_Animator;
private GameObject select;
public List<GameObject> childs=new List<GameObject>();
private Animation animation
{
get { return m_Animator ?? (m_Animator = select.GetComponent<Animation>()); }
}
enum type
{
forward,
reverse,
repeat,
stop,
}
void OnEnable()
{
m_PreviousTime = EditorApplication.timeSinceStartup;
EditorApplication.update += inspectorUpdate;
}
void OnDisable()
{
EditorApplication.update -= inspectorUpdate;
}
// //检查所有子物体
// private void checkChild(GameObject select)
// {
//
// for(int i=0;i<select.transform.childCount;i++)
// {
// if(select.transform.GetChild(i).GetComponent<Animation>()!=null)
// {
// //select.transform.GetChild(i).particleSystem.Simulate(5.0f);
// childs.Add(select.transform.GetChild(i).gameObject);
//
// }
//
// if(select.transform.GetChild(i).transform.childCount!=0)
// {
// checkChild(select.transform.GetChild(i).gameObject);
// }
//
//
// }
//
// }
// private void selectChild()
// {
// childs.Clear ();
// select=Selection.activeGameObject;
// if(select.GetComponent<Animation>()!=null)
// childs.Add (select);
// checkChild(select);
//
// if(childs.Count==0)
// {
//
// ArtToolWindow.ErrorShow("动画");
//
// }
// else
// {
//
// for(int i=0;i<childs.Count;i++)
// {
//
// if(childs[i].animation.clip!=null)
// {
// if(childs[i].animation.clip.length>kDuration)
// {
// kDuration=childs[i].animation.clip.length;
// }
// }
// else
// {
// ArtToolWindow.ErrorAnimationClipShow(childs[i].name);
// m_Playing=false;
// }
// }
//
// }
//
// }
public void play()
{
if (Application.isPlaying)
{
return;
}
// selectChild ();
m_Playing=true;
nowTime=EditorApplication.timeSinceStartup;
_type = type.forward;
}
/// <summary>
/// 停止预览播放
/// </summary>
public void playback()
{
if (Application.isPlaying)
{
return;
}
m_Playing=true;
///selectChild ();
nowTime=EditorApplication.timeSinceStartup;
_type = type.reverse;
}
public void Stop()
{
//selectChild ();
_type = type.stop;
}
public void repeatPlay()
{
//selectChild ();
m_Playing=true;
nowTime=EditorApplication.timeSinceStartup;
_type = type.repeat;
}
// <summary>
/// 预览播放状态下的更新
/// </summary>
private void update()
{
if (Application.isPlaying)
{
return;
}
for(int i=0;i<childs.Count;i++)
{
m_CurTime = (float)(m_PreviousTime - nowTime);
if(childs[i]!=null)
{
if( childs[i].GetComponent<Animation>().clip!=null)
{
if (_type == type.forward)
{
childs[i].GetComponent<Animation>().clip.SampleAnimation (childs[i], stopTime+m_CurTime);
if (m_CurTime > childs[i].GetComponent<Animation>().clip.length)
{
m_Playing = false;
stopTime=0;
return;
}
}
else if (_type == type.reverse)
{
childs[i].GetComponent<Animation>().clip.SampleAnimation (childs[i], childs[i].GetComponent<Animation>().clip.length - m_CurTime);
if (childs[i].GetComponent<Animation>().clip.length - m_CurTime < 0)
{
m_Playing = false;
return;
}
}
else if (_type == type.stop)
{
childs[i].GetComponent<Animation>().clip.SampleAnimation (childs[i], m_CurTime);
if( childs[i].GetComponent<Animation>().clip!=null)
{
childs[i].GetComponent<Animation>().clip.SampleAnimation (childs[i], m_CurTime);
m_Playing = false;
}
}
else if (_type == type.repeat)
{
childs[i].GetComponent<Animation>().clip.SampleAnimation (childs[i], m_CurTime);
if (m_CurTime > childs[i].GetComponent<Animation>().clip.length)
{
nowTime=EditorApplication.timeSinceStartup;
}
}
}
}
}
}
/// <summary>
/// 非预览播放状态下,通过滑杆来播放当前动画帧
/// </summary>
public void manualUpdate()
{
//selectChild ();
for(int i=0;i<childs.Count;i++)
{
if (childs[i].GetComponent<Animation>().clip != null)
{
childs[i].GetComponent<Animation>().clip.SampleAnimation (childs[i], m_CurTime);
}
}
}
private void inspectorUpdate()
{
var delta = EditorApplication.timeSinceStartup - m_PreviousTime;
m_PreviousTime = EditorApplication.timeSinceStartup;
if (!Application.isPlaying && m_Playing)
{
m_RunningTime = Mathf.Clamp(m_RunningTime + (float)delta, 0f, kDuration);
update();
}
}
private void InitPlay()
{
if(select.GetComponent<Animation>().clip!=null)
{
select=Selection.activeGameObject;
kDuration=select.GetComponent<Animation>().clip.length;
m_Playing=true;
nowTime=EditorApplication.timeSinceStartup;
}
}
}