【问题标题】:Show and hide gameobject when condition achieved in Unity?在Unity中达到条件时显示和隐藏游戏对象?
【发布时间】:2019-06-10 17:33:57
【问题描述】:

我有一个统一的登录场景,我正在获取用户的详细信息然后对其进行处理,并根据我想要显示和隐藏 gameobject 的条件。

我有一个 Loading 游戏对象,它只是加载动画。

这就是我正在做的(并尝试过):

void Start()  //Start Of Scene
{
Loading = GameObject.Find("CircleGlass");
Loading.SetActive(false);
}

点击按钮时:

public void Clicked()
{
Loading.SetActive(true);
//Rest of code fetching data from api using UnityEngine.Networking
if(condition)
 {
 Loading.SetActive(false);
 //Redirect to another page
 }
}

问题Loading gameobject 只是在我单击按钮时不显示。它正在花费处理时间,但不显示加载程序。 这是随附的屏幕截图:

任何帮助将不胜感激。谢谢!!

【问题讨论】:

  • 请编辑您的问题并附上显示CircleGlass 对象层次结构的屏幕截图。
  • @Ruzihm 更新了它。

标签: c# unity3d


【解决方案1】:

根据您发布的代码,我假设代码是同步执行的。这意味着在执行 SetActive(true) 之后,在执行 SetActive(false) 之前没有更新帧,因此没有时间更新实际对象的状态。

你可以通过使用协程来改变它:

public void Clicked()
{
    Loading.SetActive(true);
    StartCoroutine(FetchAndDisableLoading());
}

IEnumerator FetchAndDisableLoading(){
    //Rest of code fetching data from api using UnityEngine.Networking
    //If you make a UnityWebRequest here you should do:
    //yield return webRequest.SendWebRequest();
    //This will wait for the web request to finish before proceeding

    //Finally, when all code is executed you should disable the object:
    if(condition)
    {
        Loading.SetActive(false);
       //Redirect to another page
    }
}

【讨论】:

  • 非常感谢您的帮助。请让我试试看。
  • 谢谢老兄!!它正在工作。如果我不使用UnityWebRequest 而不是使用HttpWebRequest,该怎么办?
  • 不确定,我认为您必须使用异步方法并等待其中的响应。我不确定它是否也受所有平台的支持。如果可能,我会坚持使用 UnityWebRequest,因为它专为 Unity 中的使用而设计和优化。
猜你喜欢
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多