1

Installed Unity Version is 2020.2.1f1

I'm developing an online multiplayer game using Unity. I have 2 different connection types; Http and Websocket(using Websocketsharp: https://github.com/sta/websocket-sharp, I've installed it using 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). 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.

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). 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. Other scripts/updates works fine. I've searched a lot but couldn't even find a related topic.

WebSocket Client:

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.

---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!

4

1 回答 1

0

给那些会遇到这种问题的人的建议:

我发现 Unity 不是线程安全的。Unity 限制我们从另一个线程调用他们的 API,这导致了这个问题。

所以你必须从主线程而不是另一个线程调用你的函数/方法。

于 2021-02-09T11:57:51.507 回答