C#停止并播放动画 [英] C# To stop and play an animation

查看:147
本文介绍了C#停止并播放动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



这是我的程序:



 使用 UnityEngine; 
使用 System.Collections;

public class 动画:MonoBehaviour {

// 用于初始化
void 开始(){

}

// 每帧调用一次更新
void 更新(){

int cpt = 0 ;
if (Input.GetKey( b )){
cpt ++;

if ((cpt% 2 )== 0
GameObject.Find( Presse-1)。animation.Stop();

else
GameObject.Find( Presse-1)。animation.Play();

}
}
}





我想停下来,不用重新开始当我使用GetKey时的动画。

停止运行,但是当我第二次使用GetKey时,没有。



你能帮我吗?拜托?



谢谢:)

解决方案

假设这个会做你想做的。如果没有,请告诉我,我会尝试相应更改。



 使用 UnityEngine; 
使用 System.Collections;

public class 动画:MonoBehaviour
{
private int cpt = 0 ;

// 用于初始化
void Start()
{
}

// 每帧调用一次更新
void 更新()
{
< span class =code-keyword> if (Input.GetKey( b ))
{
cpt ++;

// if((cpt%2)== 0)
// GameObject.Find(Presse-1)。animation.Stop();
// else
/ / GameObject.Find(Presse-1)。animation.Play();

var anim = GameObject.Find( Presse-1)。动画;

float speed;
if ((cpt% 2 )== 0
speed = 0 .0f;
else
speed = 1 .0f;

foreach var state in anim)
state.speed = speed;
}
}
}


 使用 UnityEngine; 
使用 System.Collections;

public class 动画:MonoBehaviour
{
private int cpt = 0 ;


// 用于初始化
void Start()
{
}

// 每帧调用一次更新
void 更新()
{
if (Input.GetKey( b ))
{
animation [ Take 001] .speed = 1 .0f;
}
if (Input.GetKey( n))
{
animation [ Take 001 ]。speed = 0 .0f;
}
}
}


Hello !

Here's my programme :

using UnityEngine;
using System.Collections;

public class Anim : MonoBehaviour {
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		int cpt = 0;
		if (Input.GetKey ("b")) {
			cpt ++;

			if ((cpt % 2) == 0) 
				GameObject.Find ("Presse-1").animation.Stop ();

			else 
				GameObject.Find ("Presse-1").animation.Play ();
							
	}
}
}



I want to stop and start whithout restarting an animation when I use a GetKey.
Stop runs but when I use a second time the GetKey, nothing.

Could you help me please ?

Thank you :)

解决方案

I assume that this will do what you want. If not, please tell, and I'll try to change it accordingly.

using UnityEngine;
using System.Collections;

public class Anim : MonoBehaviour
{
    private int cpt = 0;

    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKey ("b"))
        {
            cpt++;

            //if ((cpt % 2) == 0)
            //    GameObject.Find ("Presse-1").animation.Stop ();
            //else
            //    GameObject.Find ("Presse-1").animation.Play ();

            var anim = GameObject.Find("Presse-1").animation;

            float speed;
            if ((cpt % 2) == 0)
                  speed = 0.0f;
            else
                  speed = 1.0f;

            foreach(var state in anim)
                  state.speed = speed;
        }
    }
}


using UnityEngine;
using System.Collections;

public class Anim : MonoBehaviour
{
    private int cpt = 0;


    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKey ("b"))
        {
            animation["Take 001"].speed = 1.0f;
        }
        if (Input.GetKey ("n"))
        {
            animation["Take 001"].speed = 0.0f;
        }
    }
}


这篇关于C#停止并播放动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆