Unity协程没有运行到最后 [英] Unity coroutine not running to the end

查看:148
本文介绍了Unity协程没有运行到最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用协程时遇到了一个奇怪的问题。



基本上,当我将鼠标悬停在精灵上时,只要鼠标指针停留在精灵上,它就会淡入淡出,效果很好,但是当鼠标退出精灵时,我希望精灵淡出直到其alpha值达到0。



这样做是因为与OnMouseOver函数不同当鼠标悬停在对撞机上时,每一帧都被调用,我在OnMouseExit函数中使用了一个协程。



下面的代码是我使用的代码,但是鼠标退出精灵,精灵的Alpha值立即设置为0,而不会褪色,我不知道为什么,因此我发布了这篇文章。



您会在OnMouseExit中注意到函数最后一行被注释掉了,我尝试使用这两种方法调用协程,两次都被调用,但是两次调用都不会出现淡出。

 使用System.Collections; 
使用System.Collections.Generic;
使用UnityEngine;

公共类Fader:MonoBehaviour {

public float min_fade_speed;
public float max_fade_speed;

私人SpriteRenderer子画面;
私人浮动float_speed;

//用于初始化
void Start(){
fade_speed = Random.Range(min_fade_speed,max_fade_speed);
sprite = GetComponent< SpriteRenderer> ();
reset_color();
}

void reset_color(){
//最初是透明的
sprite.color = new Color(1,1,1,0.0f);
}

//每帧调用一次更新-当前未使用
void Update(){
}

void FadeInAndOut( ){
sprite.color = new Color(1,1,1,Mathf.SmoothStep(0.0f,1.0f,Mathf.PingPong(Time.time / fade_speed,1f))));
}

IEnumerator FadeOut(float alpha_start){
Debug.Log( Alpha是: + alpha_start);
while(sprite.color.a> 0.0f){
sprite.color =新颜色(1、1、1,Mathf.SmoothStep(alpha_start,0.0f,Time.time / fade_speed)) ;
收益回报为null;
}
}

void onMouseEnter(){
reset_color();
}

无效OnMouseOver(){
FadeInAndOut();
}

void OnMouseExit(){
float alpha = sprite.color.a;
Debug.Log( Alpha是: + alpha);
StartCoroutine( FadeOut,alpha);
// StartCoroutine(FadeOut(alpha));
}
}

问候



Crouz

解决方案

您需要在Coroutine函数中使用一个计数器变量,该变量从 0开始计数 fade_speed 。您可以在 while 循环中使用 Time.deltaTime 来递增此变量。当该变量> = fade_speed 时,退出 while 循环。要在整个循环中获取新颜色,可以执行 new Color(1、1、1 Mathf.Lerp(startingAlpha,0,counter / fade_speed)); .. / p>

您新的 FadeOut 函数:

  IEnumerator FadeOut(float alpha_start)
{
//这将从
的位置开始减少float startingAlpha = sprite.color.a;

浮动计数器= 0;

而(counter< fade_speed)
{
counter = counter + Time.deltaTime;
sprite.color = new Color(1,1,1,Mathf.Lerp(startingAlpha,0,counter / fade_speed));
收益回报为null;
}
}


I am having a weird problem with a coroutine.

Basically when I mouse over a sprite it fades in and out as long as the mouse pointer is over it, this works fine, but when the mouse exits the sprite I want the sprite to fade out until its alpha value reaches 0.

To do so and because unlike the OnMouseOver function which is called every frame while the mouse is over the collider, I use a coroutine called in my OnMouseExit function.

The code below is what I use, but as soon as the mouse exits the sprite, the sprite's alpha is set to 0 straight away, without fading out, I have no idea why, hence my post.

You will notice in the OnMouseExit function the last line is commented out, I have tried to call the coroutine using both methods, it gets called both times but the fade out does not happen in either call.

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

public class Fader : MonoBehaviour {

    public float min_fade_speed;
    public float max_fade_speed;

    private SpriteRenderer sprite;
    private float fade_speed;

    // Use this for initialization
    void Start () {
        fade_speed = Random.Range (min_fade_speed, max_fade_speed);
        sprite = GetComponent<SpriteRenderer> ();
        reset_color ();
    }

    void reset_color() {
        //Initially transparent
        sprite.color = new Color(1, 1, 1, 0.0f);
    }

    // Update is called once per frame -- currently unused
    void Update () {
    }

    void FadeInAndOut() {
        sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (0.0f, 1.0f, Mathf.PingPong(Time.time/fade_speed, 1f)));
    }

    IEnumerator FadeOut(float alpha_start) {
        Debug.Log ("Alpha is: " + alpha_start);
        while (sprite.color.a > 0.0f) {
            sprite.color = new Color (1, 1, 1, Mathf.SmoothStep (alpha_start, 0.0f, Time.time / fade_speed));
            yield return null;
        }
    }

    void onMouseEnter() {
        reset_color ();
    }

    void OnMouseOver() {
        FadeInAndOut ();
    }

    void OnMouseExit() {
        float alpha = sprite.color.a;
        Debug.Log ("Alpha is: " + alpha);
        StartCoroutine ("FadeOut", alpha);
        // StartCoroutine(FadeOut(alpha));
    }
}

Regards

Crouz

解决方案

You need a counter variable in the Coroutine function that counts from 0 to fade_speed. You increment this variable in the while loop with Time.deltaTime. When that variable is >= fade_speed, exit the while loop. To get the new colorin the whole loop, you can do new Color(1, 1, 1, Mathf.Lerp(startingAlpha, 0, counter / fade_speed));.

Your new FadeOut function:

IEnumerator FadeOut(float alpha_start)
{
    //This will start decreasing from where it is
    float startingAlpha = sprite.color.a;

    float counter = 0;

    while (counter < fade_speed)
    {
        counter = counter + Time.deltaTime;
        sprite.color = new Color(1, 1, 1, Mathf.Lerp(startingAlpha, 0, counter / fade_speed));
        yield return null;
    }
}

这篇关于Unity协程没有运行到最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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