按下按钮时是否具有循环功能? Unity3d C# [英] Loop function on button press? Unity3d C#

查看:93
本文介绍了按下按钮时是否具有循环功能? 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.

在void Update中旋转良好,但是在其自身功能中仅旋转一次.我尝试使用循环,但仍然没有运气.有人可以帮我吗?

It spins fine when its in void Update, but when its in its own function, it does it just once. I tried using loop but still no luck. Can anyone help me please?

代码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);
    }
}

提前谢谢!

推荐答案

for循环未按预期工作,因为您没有等待帧.基本上,它将在一帧中完成所有旋转,直到最后一次旋转,您才能看到更改.等待帧可以用yield return null;完成,并且需要协程函数.

The for loop isn't working as expected because you are not waiting for a frame. 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; and that requires a coroutine function.

这最好用协程来完成.您可以将布尔变量与协程一起使用,也可以只使用StartCoroutineStopCoroutine.单击开始"按钮时启动coorutine,该对象将旋转对象,然后单击停止"按钮时,将停止协程.

This is better done with a coroutine. You can use boolean variable with a coroutine or you can just use StartCoroutine and StopCoroutine. Start coorutine that spins the Object when the start Button is clicked and then stop the coroutine when the stop Button is clicked.

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;
    }
}

这篇关于按下按钮时是否具有循环功能? Unity3d C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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