【问题标题】:I have a co routine that once selected plays through. However If i go to select it again nothing happens. It does work the first time however我有一个合作例程,一旦选择就会播放。但是,如果我再次选择它,则不会发生任何事情。但是它确实第一次起作用
【发布时间】:2019-08-29 09:44:37
【问题描述】:

我有一个合作例程,一旦选择就会播放。 co 例程将对象放大。它选择的第二个对象会缩小对象。

但是,如果我再次选择它,则不会发生任何事情。不过,它确实第一次起作用。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class square : MonoBehaviour
{

    public Transform Button;
    float ElapsedTime = 0.0f;
    float TotalTime = 0.4f;

    private bool _isenlargingcanvas;

    public void enlargecanvas()
    {
        if (_isenlargingcanvas)
            return;
        _isenlargingcanvas = true;
        StartCoroutine(transitionscale());
        _isenlargingcanvas = false;
    }

    IEnumerator transitionscale()

    {

        while (ElapsedTime < TotalTime)
        {
            ElapsedTime += Time.deltaTime;
            Button.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new
            Vector3(9, 7, 7), (ElapsedTime / TotalTime));
            yield return null;
        }
    }

    private bool _isshrinkingcanvas;

    public void shrinkcanvas()
    {
        if (_isshrinkingcanvas)
            return;
        _isshrinkingcanvas = true;
        StartCoroutine(transitionscaledown());
        _isshrinkingcanvas = false;
    }

    IEnumerator transitionscaledown()

    {

        while (ElapsedTime < TotalTime)
        {
            ElapsedTime += Time.deltaTime;
            Button.localScale = Vector3.Lerp(new Vector3(9, 7, 7), new
            Vector3(0, 0, 0), (ElapsedTime / TotalTime));
            yield return null;
        }
    }

}

我有一个合作例程,一旦选择就会播放。 co 例程将对象放大。它选择的第二个对象会缩小对象。

但是,如果我再次选择它,则不会发生任何事情。不过,它确实第一次起作用。

【问题讨论】:

  • 所以添加一些调试语句 - 事实上你的 _isshrink 可能在协程真正开始之前再次设置为 false,这无济于事。

标签: c# unity3d co routines


【解决方案1】:

在我看来,您没有重置ElapsedTime 字段,因此如果您要重新输入transitionscale() 方法,while 语句中的条件已经为假,因此该方法不执行任何操作就退出。

对此可能的解决方案是,在每次调用方法之前重置变量...如下

// Inside the elarge canvas method
ElapsedTime = 0.0f;
StartCoroutine(transitionscale());

或者,您可以在转换比例方法中将其重置,而且工作量更少。

IEnumerator transitionscale()

{
    ElapsedTime = 0.0f;

    while (ElapsedTime < TotalTime)
    {
        ElapsedTime += Time.deltaTime;
        Button.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new
        Vector3(9, 7, 7), (ElapsedTime / TotalTime));
        yield return null;
    }
}

您需要对trasitionscaledown() 方法执行相同的操作。考虑其中一项更改,看看是否能解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多