[英]Loop function on button press? Unity3d C#
So, I have an object. 所以,我有一个对象。 When I press Spin Button, I want it to spin.
当我按下旋转按钮时,我希望它旋转。 When I press Stop button, I want it to stop.
当我按停止按钮时,我希望它停止。
It spins fine when its in void Update, but when its in its own function, it does it just once. 当它在void Update中时,它旋转良好,但是在它自己的函数中,它仅执行一次。 I tried using loop but still no luck.
我尝试使用循环,但仍然没有运气。 Can anyone help me please?
谁能帮我吗?
Code C#: 代码C#:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class spin : MonoBehaviour
{
public float speed = 500f;
public Button starter;
public Button stopper;
int testing = 200;
void Start () {
Button btn = starter.GetComponent<Button> ();
Button butn = stopper.GetComponent<Button> ();
butn.onClick.AddListener(FidgetSpinnerStop);
btn.onClick.AddListener(FidgetSpinnerStart);
}
void FidgetSpinnerStart ()
{
for (int i = 0; i < testing; i++) {
transform.Rotate (Vector3.up, speed * Time.deltaTime);
Debug.Log ("Test: " + i);
}
}
void FidgetSpinnerStop ()
{
transform.Rotate (Vector3.up, Time.deltaTime);
}
}
Thanks in advance! 提前致谢!
The for loop isn't working as expected because you are not waiting for a frame. for循环无法正常工作,因为您没有等待帧。 Basically, it will do all the spinning in one frame and you won't see the changes until the final spin.
基本上,它将在一帧中完成所有旋转,直到最后一次旋转,您才能看到更改。 Waiting for a frame can the done with
yield return null;
等待一个帧就可以完成,并
yield return null;
and that requires a coroutine function. 这需要协同程序功能。
This is better done with a coroutine. 这最好用协程来完成。 You can use boolean variable with a coroutine or you can just use
StartCoroutine
and StopCoroutine
. 您可以将布尔变量与协程一起使用,也可以仅使用
StartCoroutine
和StopCoroutine
。 Start coorutine that spins the Object when the start Button is clicked and then stop the coroutine when the stop Button is clicked. 单击“开始”按钮时将启动对象的coorutine,然后单击“停止”按钮时将停止协程。
public float speed = 500f;
public Button starter;
public Button stopper;
bool isSpinning = false;
IEnumerator spinnerCoroutine;
void Start()
{
//The spin function
spinnerCoroutine = spinCOR();
Button btn = starter.GetComponent<Button>();
Button butn = stopper.GetComponent<Button>();
butn.onClick.AddListener(FidgetSpinnerStop);
btn.onClick.AddListener(FidgetSpinnerStart);
}
IEnumerator spinCOR()
{
//Spin forever untill FidgetSpinnerStop is called
while (true)
{
transform.Rotate(Vector3.up, speed * Time.deltaTime);
//Wait for the next frame
yield return null;
}
}
void FidgetSpinnerStart()
{
//Spin only if it is not spinning
if (!isSpinning)
{
isSpinning = true;
StartCoroutine(spinnerCoroutine);
}
}
void FidgetSpinnerStop()
{
//Stop Spinning only if it is already spinning
if (isSpinning)
{
StopCoroutine(spinnerCoroutine);
isSpinning = false;
}
}
Following is a simple class that start and stops spinning an object using two buttons, I hope it makes a starting point of what you are trying to achieve. 以下是一个简单的类,它使用两个按钮启动和停止旋转对象,我希望它为您尝试实现的目标提供了一个起点。
public class TestSpin : MonoBehaviour
{
public float speed = 500f;
public Button starter;
public Button stopper;
bool IsRotating = false;
void Start()
{
Button btn = starter.GetComponent<Button>();
Button butn = stopper.GetComponent<Button>();
butn.onClick.AddListener(FidgetSpinnerStop);
btn.onClick.AddListener(FidgetSpinnerStart);
}
void FidgetSpinnerStart()
{
IsRotating = true;
}
void FidgetSpinnerStop()
{
IsRotating = false;
}
void Update()
{
if (IsRotating)
transform.Rotate(0, speed, 0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.