【问题标题】:Unity2D C# Reloading progress bar not working propertlyUnity 2D C#重新加载进度条无法正常工作
【发布时间】:2015-09-03 11:36:06
【问题描述】:

所以我正在制作一款自上而下的坦克射击游戏,我想制作一个比以前更好的装填系统。所以我想到我需要一些进度条之王。我知道怎么做,所以我开始做。问题是它不能正常工作。正如我在上面的 .gif 中显示的那样,当您第二次拍摄时,进度条不会下降。因为我是新来的统一,我仍然不知道一切都很好。所以我来了,也许有人可以帮忙。

编辑: 我刚刚发现了另一个问题,也许是我遇到这个问题的答案。我的脚本第二次尝试重新加载时,我的“needTimer”布尔值为假,因此当它为假时进度条不会下降。新的问题是为什么它会变成假而不是真的? 我的重载脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Reload : MonoBehaviour {

    public float ammo;
    public Image progress;
    public bool alreadyReloading;
    public bool needTimer;

    void Start () {
        alreadyReloading = false;
    }
    IEnumerator needtimertime(){
        yield return new WaitForSeconds (6.5f);
        needTimer = false;
    }
    IEnumerator uztaisyt(){
        Debug.Log ("REEELOUUUDING!");
        yield return new WaitForSeconds(6.5f);
        ammo += 1;
        alreadyReloading = false;
    }

    void Update () {
        if (needTimer == true) {
            timer ("");
        }
        if (ammo < 5) {
            if(alreadyReloading == false){
                needTimer = true;
                StartCoroutine(uztaisyt());
                alreadyReloading = true;
            }
        }
        if (progress.fillAmount <= 0) {
            progress.fillAmount = 1.0f; 
        }
    }

    void timer(string tipas){
        progress.fillAmount -=  Time.deltaTime / 6.5f;
        StartCoroutine (needtimertime ());
    }
}

【问题讨论】:

  • 第二次射击时弹药变量的值是多少?
  • @Pawel Marecki 4 每次拍摄时都会摔倒。它显示在 gif 中
  • 请澄清:alreadyReloadingneedTimer 是公开的,它们是从场景中的任何其他对象调用的吗?另外,ammo 字段在哪里以及如何减少?如果减少 ammo 的时间与 Reload Update() 函数不同步,这可能是罪魁祸首。
  • 这两个 bool 都没有在任何其他脚本中使用,我将它们设置为 public 以进行调试。您可以删除 public 并且什么都不会改变。弹药在其他脚本中被调用,称为射击,但只取值,以便于调用。 (如 value1 = ...GetComponent....value2;)。
  • Unity 编辑器中是否设置了 ammo 的初始值?

标签: c# user-interface unity3d progress-bar unity3d-gui


【解决方案1】:

当您在第一次拍摄后(在动画中)将 uztaisyt() 作为协程启动时,needTimer 设置为 true 并在 next Update() 调用,needtimertime() 协程将启动。由于 uztaisyt()needtimertime() 都具有相同的 6.5 秒等待时间,因此它们将不会都返回相同的帧更新,因为 needtimertime() 将始终在 uztaisyt() 之后的下一帧开始。而且,由于无法保证 Update() 调用之间的时间间隔(请参阅 Time and Frame Managment),此间隔可能超出预期,needtimertime() 可能在第二次触发后调用 uztaisyt() 后立即在帧中返回 false。

为了确保 needtimertime() 总是在调用 uztaisyt() 之后立即启动(如果尚未运行)(并在同一帧更新中调用) ,您可以尝试对 Reload 脚本进行以下更新,(基本上更改了 Update() 方法以及何时/如何设置 _isTimerRunning)。

public class Reload : MonoBehaviour {

  public float ammo;
  public Image progress;

  private bool _alreadyReloading;
  private bool _isTimerRunning;

  void Start () {
      _alreadyReloading = false;
      _isTimerRunning = false;
  }

  IEnumerator needtimertime(){
      yield return new WaitForSeconds (6.5f);
      _needTimer = false;
  }

  IEnumerator uztaisyt(){
      Debug.Log ("REEELOUUUDING!");
      yield return new WaitForSeconds(6.5f);
      ammo += 1;
      _alreadyReloading = false;
  }

  void Update () {
      if (ammo < 5) {
          if(_alreadyReloading == false){                                        
              StartCoroutine(uztaisyt());
              _alreadyReloading = true;

              //this will check for and start the progress bar timer in the same udate call
              //so both coroutines finish on the same frame update
              if(!_isTimerRunning){
                _isTimerRunning = true;
                timer ("");
              }
          }
      }
      if (progress.fillAmount <= 0) {
          progress.fillAmount = 1.0f; 
      }
  }

  void timer(string tipas){
      progress.fillAmount -=  Time.deltaTime / 6.5f;
      StartCoroutine (needtimertime ());
  }
}

【讨论】:

  • 我发现这个方法不起作用,因为(我认为,我认为)timer() 只被调用一次,没有给进度条足够的时间,去下去. (而不是下降,它得到值 0.9967943 并停止)。我认为需要像我之前调用的那样调用它( if (needTimer == true) { timer (""); } ),但是使用这种时间方法来同步开始时间。
【解决方案2】:

我发现了我的问题并解决了它。 问题是 needTimer 变得错误。所以我找到了它并删除了它。

我的新代码:

using UnityEngine;

使用 UnityEngine.UI; 使用 System.Collections;

公共类重载:MonoBehaviour {

public float ammo;
public Image progress;
public bool alreadyReloading;
public bool needTimer;

void Start () {
    alreadyReloading = false;
    needTimer = false;
}

IEnumerator uztaisyt(){
    Debug.Log ("REEELOUUUDING!");
    yield return new WaitForSeconds(6.5f);
    ammo += 1;
    alreadyReloading = false;
}

void Update () {
    if (ammo < 5.0f) {
        if(alreadyReloading == false){
            progress.fillAmount = 1.0f;
            needTimer = true;
            StartCoroutine(uztaisyt());
            alreadyReloading = true;
        }
        if (needTimer == true) {
            timer ("");
        }
    }
}

void timer(string tipas){
    progress.fillAmount -=  Time.deltaTime / 6.5f;
}

}

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多