Unity 3d重复闪烁图像

问题描述 投票:0回答:2

我正在从事汽车游戏项目。我制作了一个UI面板,其中有两个UI图像(Left_Image和Right_Image)。当我按下“左”按钮时,Left_Image开始闪烁,当我按下“右”按钮时,Right_Image开始闪烁。但是我想要的是,如果Right_Image已经闪烁并且按下“向左”按钮,则Left_Image开始闪烁,但是Right_Images应该停止。我尝试了所有技巧,但没有运气。请帮助。

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

public class Indicators_UI : MonoBehaviour {

    public GameObject flash_right;
    public GameObject flash_left;

    public float interval;

    void Start()
    {
        InvokeRepeating("RightFlash", 0, interval);
        InvokeRepeating("LeftFlash", 0, interval);
    }

    void Update ()
    {
        if (ControlFreak2.CF2Input.GetAxis ("right") != 0) {

            if (!IsInvoking("RightFlash"))
                InvokeRepeating("RightFlash", 0.35f, 0.35f);
        } 
        else
        {
            CancelInvoke("RightFlash");
        }
        if (ControlFreak2.CF2Input.GetAxis ("left") != 0) {
            if (!IsInvoking("LeftFlash"))
                InvokeRepeating("LeftFlash", 0.35f, 0.35f);
        } 
        else
        {
            CancelInvoke("LeftFlash");
        }
    }

    void RightFlash()
    {
        if(flash_right.activeSelf)
            flash_right.SetActive(false);
        else
            flash_right.SetActive(true);
    }

    void LeftFlash()
    {
        if(flash_left.activeSelf)
            flash_left.SetActive(false);
        else
            flash_left.SetActive(true);
    }
}
c# unity3d user-interface 3d repeat
2个回答
0
投票

通常不要InvokeRepeating等与string一起使用!通过名称调用方法不是很容易维护。如果要至少使用它们,请使用nameof以确保名称没有拼写错误。

那你就不能做

nameof

在您的情况下,我实际上宁愿使用简单的计时器,因为您仍然需要void Update () { if (ControlFreak2.CF2Input.GetAxis ("right") != 0) { if (!IsInvoking(nameof(RightFlash))) { if(IsInvoking(nameof(LeftFlash)) CancelInvoke(nameof(LeftFlash)); InvokeRepeating(nameof(RightFlash), 0.35f, 0.35f); } } else { CancelInvoke(nameof(RightFlash)); } if (ControlFreak2.CF2Input.GetAxis ("left") != 0) { if (!IsInvoking(nameof(LeftFlash))) { if(IsInvoking(nameof(RightFlash)) CancelInvoke(nameof(RightFlash)); InvokeRepeating(nameof(LeftFlash), 0.35f, 0.35f); } } else { CancelInvoke(nameof(LeftFlash)); } } // Btw: These you can implement way shorter this way ;) void RightFlash() { flash_right.SetActive(!flash_right.activeSelf); } void LeftFlash() { flash_left.SetActive(!flash_left.activeSelf); } 方法来获取用户输入,所以为什么要过于复杂:

Update

-1
投票

感谢derHugo ...我认为问题在于切换状态,如果出现以下情况,则可以解决该问题:

如果(ControlFreak2.CF2Input.GetAxis(“ left”)!= 0){

ControlFreak2.CF2Input.GetAxis(“ right”)!= 1

}但出现错误:只能将赋值,调用,递增,递减,等待和新对象表达式用作语句

© www.soinside.com 2019 - 2024. All rights reserved.