简体   繁体   English

Unity - 我的脚本在编辑器上停止工作,但在我的构建(android,ios)上工作

[英]Unity - My script stops working on Editor but works on my builds(android, ios)

Installed Unity Version is 2020.2.1f1安装的 Unity 版本是 2020.2.1f1

I'm developing an online multiplayer game using Unity.我正在使用 Unity 开发在线多人游戏。 I have 2 different connection types;我有 2 种不同的连接类型; Http and Websocket(using Websocketsharp: https://github.com/sta/websocket-sharp , I've installed it using Nuget). Http 和 Websocket(使用 Websocketsharp: https://github.com/sta/websocket-sharp ,我已经使用 Nuget 安装了它)。 Http get/post, Websocket connect/send/close works totally fine but when I call function (directly or by sending events) after response, most of the time my code inside that called function is not completed on editor(works fine on builds-android,ios). Http get/post, Websocket connect/send/close works totally fine but when I call function (directly or by sending events) after response, most of the time my code inside that called function is not completed on editor(works fine on builds-安卓、iOS)。 I'm not sure if this problem is related to http/websocket but it's related to online connection because I've experienced the same issue when I was developing a real-time multiplayer game using Unity & Gamesparks.我不确定这个问题是否与 http/websocket 有关,但它与在线连接有关,因为我在使用 Unity & Gamesparks 开发实时多人游戏时遇到了同样的问题。

I can do basic functions like print etc. until I call one of the following functions loadscene, update UI, or call another function in same/different script(only first one of these works, everything after that won't run).我可以执行打印等基本功能,直到我调用以下函数之一加载场景、更新 UI 或在相同/不同的脚本中调用另一个 function(只有其中第一个有效,之后的所有内容都不会运行)。 Console doesn't show any error and game doesn't pause or crash.控制台不显示任何错误,游戏不会暂停或崩溃。 It runs on same fps but that part of the code won't continue.它以相同的 fps 运行,但那部分代码不会继续。 Other scripts/updates works fine.其他脚本/更新工作正常。 I've searched a lot but couldn't even find a related topic.我搜索了很多,但甚至找不到相关的主题。

WebSocket Client: WebSocket 客户端:

void OnBattleResponse(object sender, MessageEventArgs messageEventArgs) {
        string message = messageEventArgs.Data;

        if (messageEventArgs.Data.Equals(Constants.WEBSOCKET_ERROR))
            return;

        var  webSocketData = JsonUtility.FromJson<BattleResponseSerializable>(message);

        switch (webSocketData.type) {
            case 0:
                    seed         = webSocketData.user1.seed;
                    opponentSeed = webSocketData.user2.seed;
                    _connectionManager.BattleGetStart(seed, opponentSeed);
                break;
    .
    .
    .
}

Connection Manager:连接管理器:

    public void BattleGetStart(int userSeed, int opponentSeed) {
        _gameManager.StartWave(userSeed, opponentSeed);
    }

Game Manager游戏管理器

    public void StartWave(int userSeed, int opponentSeed)
    {
        UserSeed = userSeed;
        OpponentSeed = opponentSeed;
        UserRandom = new System.Random(UserSeed);
        OpponentRandom = new System.Random(OpponentSeed);
        StartWaveTimer(); // After this nothing will be run, it's not related to startWaveTimer, any function that I've mentioned causes the same issue.
        print("DOESN'T WORK"); // This doesn't work on Editor but works on builds
        _spawner.ActivateSpawner(true); // doesn't work
    }

I've found a trick to handle this situation but I didn't like it as a proper solution and don't wanna spend resources for Update calls.我找到了处理这种情况的技巧,但我不喜欢将其作为适当的解决方案,也不想将资源用于更新调用。 When I call function (directly or by sending events) after response, inside the called function I set bool to true and in Update call I call the necessary function when the bool is true and the code works totally fine.当我在响应后调用 function(直接或通过发送事件)时,在被调用的 function 内部,我将 bool 设置为 true,在 Update 调用中,我调用必要的 ZC1C425268E68385D1AB4ZZC1C425268E68385D1AB4ZZ5074C17A94F 时代码完全正常。

---The Trick--- ---诀窍---

Game Manager游戏管理器

    bool startWave;

    public void StartWave(int userSeed, int opponentSeed)
    {
        UserSeed       = userSeed;
        OpponentSeed   = opponentSeed;
        UserRandom     = new System.Random(UserSeed);
        OpponentRandom = new System.Random(OpponentSeed);
        startWave      = true;
    }

    void Update() {
        if (startWave) {
            startWave = false;
            StartWaveTimer();
            print("WORKS"); // With this trick, it works on editor too.
            _spawner.ActivateSpawner(true); // works
        }
    }

What can be the reason of my script working on builds but not on unity editor?我的脚本在构建而不是在统一编辑器上工作的原因可能是什么?

Thank you!谢谢!

Advice for those who will experience that kind of issue:给那些会遇到这种问题的人的建议:

As I've found out Unity is not Thread safe.我发现 Unity 不是线程安全的。 Unity limits us on calling their API from another Thread, which causes this issue. Unity 限制我们从另一个线程调用他们的 API,这会导致此问题。

So you have to call your functions/methods from main thread instead of another thread.所以你必须从主线程而不是另一个线程调用你的函数/方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity阴影在编辑器中有效,但在构建中无效 - Unity shadows working in editor but not working in builds Unity 我的回放脚本不起作用? - Unity My Replay Script not working? OnTriggerEnter可在Unity编辑器中使用,但无法在Android或iOS上检测到任何冲突 - OnTriggerEnter works in Unity Editor, but does not detect any collision on Android or iOS StartCoroutine函数在android上不起作用,但是在Unity编辑器中可以正常工作 - StartCoroutine function not working on android, but works fine in Unity editor Admob 在 android 版本中不起作用。 在统一编辑器中完美运行 - Admob not working in android build. Works perfectly in unity editor Sqlite C#代码可在Unity编辑器中使用,但在编译后会停止工作 - Sqlite C# code works in Unity Editor, but stops working after compiling 应用程式可在Unity编辑器中运作,但无法在Android上运作 - App works in Unity editor but not on Android Unity3d:我的击退脚本不起作用 - Unity3d: My knockback script is not working 为什么我的Resharper NUnit会话停止工作,但在调试模式下工作 - Why my Resharper NUnit session stops working, but works in debug mode Unity 视频播放器无法在我的 android 上运行 - Unity Video player not working on my android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM