纳金网
标题:
Animation 播放动画的一些操作
[打印本页]
作者:
烟雨
时间:
2015-12-25 07:13
标题:
Animation 播放动画的一些操作
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NpcAnimation : MonoBehaviour
{
Animation animation;
public string playingAnim;//正在播放的动画名字
public bool nextStart =true;//是否开始播放下一个动画
public float playingAnimTimeProgress;//正在播放的动画的播放进度,=1时播放完毕(动画播放模式为WrapMode.Once才有效,不确定)
public bool isPlayAnim =true;//是否播放动画
void Awake()
{
this.animation = GetComponent<Animation> ();
SetAllAnimtionWrapMode (WrapMode.Loop);
//0.1秒后调用Update01f,然后一直间隔0.1秒后在调用
InvokeRepeating ("Update01f", 0.1f, 0.1f);
}
void Update01f()
{
if(isPlayAnim)
{
if(nextStart)
{
playingAnim = RangeAll ();
animation.CrossFade(playingAnim);
nextStart =false;
}
playingAnimTimeProgress = animation [playingAnim].normalizedTime;
//如果动画播放完了,切换下一个
if(animation[playingAnim].normalizedTime>=0.97f)
{
nextStart =true;
}
}
}
/// <summary>
/// 从全部动画里随机一个动画
/// </summary>
/// <returns>The all.</returns>
string RangeAll()
{
int cout = 0;
List<AnimationState> list = new List<AnimationState> ();
foreach (AnimationState state in animation)
{
cout++;
list.Add(state);
}
int index = Random.Range (0,cout);
return list[index].name;
}
/// <summary>
/// 设置全部动画循环状态wrap mode.
/// </summary>
/// <param name="wm">Wm.</param>
void SetAllAnimtionWrapMode(WrapMode wm)
{
foreach (AnimationState state in animation)
{
state.wrapMode = wm;
}
}
/// <summary>
///有多少个动画
/// </summary>
/// <returns>The animation count.</returns>
int GetAnimationCount()
{
int cout = 0;
foreach (AnimationState state in animation)
{
cout++;
}
return cout;
}
/// <summary>
/// 得到正在播放的动画名字.
/// </summary>
/// <returns>如果当前没有播放动画则返回空字符串.</returns>
string GetPlayAnimation()
{
foreach (AnimationState state in animation)
{
if(animation.isPlaying)
{
if(state.normalizedTime!=0)
{
return state.name;
}
}
else
{
return "";
}
}
return "";
}
}
复制代码
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5