加载场景时协程错误的行为 [英] Coroutine wrong Behavior when scene is loaded

查看:29
本文介绍了加载场景时协程错误的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这个协程:

 IEnumerator ShowCharTraits()
    {

        while(!hasPlayerChosen)
        {
            yield return null;
            traitPanel.SetActive(true);
        }
        hasPlayerChosen = false;
        traitPanel.SetActive(false);
    //    Debug.Log("Got called! job done");

    }

它是从我的 GameManager 中的唤醒方法中这样调用的:

It's being called like this from the awake method in my GameManager:

players = GameObject.FindGameObjectsWithTag("Player");
                foreach (GameObject g in players)
                {
                    ui_Controller.StartShowCharTraits();
                    g.GetComponent<PlayerToken>().isTurn = false;
                }

StartShowCharTraits() 是一个简单的方法:

StartShowCharTraits() is a simple method that does this :

  public void StartShowCharTraits()
    {
        StartCoroutine("ShowCharTraits");
    }

现在,我检查了标签,没有空引用异常,实际上没有抛出错误或警告.如果我在编辑器中加载场景然后播放它一切正常.traitPanel.SetActive(true); 被调用并显示我的面板.但是,当我使用 SceneManager.LoadScene(1); 从另一个场景加载我的场景时,永远不会到达上述行.任何想法为什么会发生这种情况?

Now, I have checked the tags, no null reference exception, actually no errors or warnings are being thrown. If i load the scene in the editor and then play it everything works fine. traitPanel.SetActive(true); get called and my panel shows up. However when I load my scene from another scene using SceneManager.LoadScene(1); the above mentioned line is never reached. Any ideas why this is happening ?

推荐答案

假设您想要一个像单身人士"的中心位置;在 Unity 项目中.示例,

Say you want to have one central place that is "like a singleton" in a Unity project. Example,

SoundEffects
LapTimer
Scores
SocialMediaConnections

你所做的就是这个.

  1. 制作您的脚本 SoundEffects.cs

  1. make your script SoundEffects.cs

回想一下,每个 Unity 项目必须预加载"场景.(不可能没有)

recall that every Unity project must have a "preload" scene. (it's impossible to not have one)

在预加载场景中,有一个名为holder"的空游戏对象.确保它被标记为DontDestroyOnLoad"

in the preload scene, have a empty game object called "holder". make sure it is marked "DontDestroyOnLoad"

将 SoundEffects.cs 附加到该支架

attach SoundEffects.cs to that holder

你已经完成了.

仅此而已.

你已经完成了.

就这么简单"

因此,在任何特定场景中,碰巧附加到任何特定对象的任何特定脚本可能需要访问SoundEffects"...

So, any particular script, which happens to be attached to any particular object, in any particular scene, may need to access "SoundEffects"...

为此,只需在 Awake 中执行此操作:

To do so, simply do this in Awake:

 SoundEffects soundEffects = Object.FindObjectOfType<SoundEffects>();

然后只需在该脚本的任何位置使用 soundEffects,例如 soundEffects.PlayBoom(13) 等等.

then just use soundEffects anywhere in that script, for example soundEffects.PlayBoom(13) etc etc.

所以在 Awake() 中

So in Awake()

  Laps laps = Object.FindObjectOfType<Laps>();
  Social social = Object.FindObjectOfType<Social>();
  AI ai = Object.FindObjectOfType<AI>();

什么的.

统一很简单.非常简单.就是这么简单.

Unity is simple. Incredibly simple. It's just that easy.

  1. 编写您的脚本,LapTimer.cs 或其他任何

把它放在你的预加载场景中的一个支架对象上(它还能在哪里?)

put it on a holder object in your preload scene (where else could it be?)

没有3",仅此而已.就是这么简单.

there's no "3", that's all there is. It's just that ridiculously simple.

在 Unity 的早期,有人不幸提到了单身人士";在某个地方的 QA 板上.(在 ECS 系统中不能有单身人士",这是毫无意义的.)当时,这引发了关于如何制作类似单身人士"的东西的大量讨论.在 Unity 中,这是在混乱中堆积混乱.不幸的是,从那天到今天,人们开始学习 Unity,他们看到了单身".网络上到处提到,导致无休止的混乱.

In the early days of Unity, someone unfortunately mentioned "singletons" on a QA board somewhere. (You can't have "singletons" in an ECS system, it's meaningless.) At the time, this led to huge discussions about how you can make a "singleton-like thingy" in Unity, which is piling confusion on confusion. Unfortunately from that day to this, you get people learning Unity, who see "singleton" mentioned here and there on the net, and it leads to endless confusion.

再次注意经理"的概念.在 Unity 中简直是不可能的简单 - 上面已经解释过.这是微不足道的.再简单不过了.

注意,上面我提到你可能想要语法糖果来避免输入Laps laps = Object.FindObjectOfType();

Note, above I mention you might want syntactic candy to avoid the incredible chore of typing Laps laps = Object.FindObjectOfType<Laps>();

遗憾的是,Unity/c# 中没有宏,因此您需要另一种方式.

There are very unfortunately no macros in Unity/c# so you need another way.

您所做的只是使用网格" 脚本http://answers.unity3d.com/answers/1124859/view.html 在 Unity 中已经流行多年.

All you do is use a "Grid" script http://answers.unity3d.com/answers/1124859/view.html which has been popular for years in Unity.

但老实说,使用 Scores score = Object.FindObjectOfType(); 非常简单,我现在通常只是这样做.

But honestly, it's so simple to use Scores scores = Object.FindObjectOfType<Scores>(); that I usually just do that nowadays.

这篇关于加载场景时协程错误的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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