用animation管理子动画
通过OnEnable来控制动画的开启,播放。简单的TEST,根据自己需求来完善
//子脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestDoAnimator : MonoBehaviour {
//父级控制脚本
public TestAnimationScript tda;
//父级控制的动画
public Animation ani;
//动画市场
public float animfloat;
//是否执行过start播放
bool isStart = false;
//控制子动画是否播放
public bool isShow = true;
// Use this for initialization
void Start () {
ani = this.GetComponent<Animation>();
AnimationClip aniClip = ani.clip;
//获取子动画时长
AnimationEvent ae = new AnimationEvent();
ae.time = aniClip.length;
//增加子动画结束回调
ae.functionName = "Stoped";
aniClip.AddEvent(ae);
isStart = true;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.X))
{
PlayAnimation();
}
}
//播放
public void PlayAnimation()
{
ani.Play();
}
//结束回调
public void Stoped()
{
Debug.Log(this.gameObject.name + " == Stoped!");
}
//控制脚本开关来控制播放
void OnEnable()
{
if (isStart && isShow)
{
ani.Play();
//判断此子动画的时长从播放到结束是否大于其他所有动画的时长
tda.JudgeTime(ani.clip.length);
}
}
}
//父脚本
using UnityEngine;
using System.Collections;
public class TestAnimationScript : MonoBehaviour {
//父级脚本
//控制动画时长
float animTime = 0;
//停止时间
float stopTime = 0;
//是否开始播放动画
public bool isStart = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Z))
{
PlayAnimation();
isStart = true;
}
//判断播放时长是否大于最大动画时长
if (isStart)
{
if (stopTime > animTime)
{
isStart = false;
stopTime = 0;
animTime = 0;
AllStoped();
}
else
{
stopTime += Time.deltaTime;
}
}
}
void AllStoped()
{
//所有动画播放完毕回调,动画播放完毕后开始正式执行逻辑
}
void PlayAnimation()
{
this.transform.GetComponent<Animation>().Play();
animTime = this.transform.GetComponent<Animation>().clip.length + 0.1f;
}
//判断子动画时间是否大于整个动画时长
public void JudgeTime(float time)
{
//大于则刷新
if (stopTime + time > animTime)
{
animTime = stopTime + time;
}
}
}