简体   繁体   中英

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

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!

Advice for those who will experience that kind of issue:

As I've found out Unity is not Thread safe. Unity limits us on calling their API from another Thread, which causes this issue.

So you have to call your functions/methods from main thread instead of another thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM