统一播放后调用代码

call a code when done playing in unity

我在 JAVA 中得到了一个 tcp server ,在统一脚本中得到了一个 class 叫做 ClientClient 是一个 singleton,它连接到位于 constructor 的服务器。当我按下播放按钮时,它会连接到服务器,但当我再次按下以停止游戏时,客户端不会自动断开连接,所以当我再次 运行 游戏时,它会堆栈,因为他试图从他已经连接的计算机连接连接的。我试图在 destructor 处断开连接,但它从未被调用过。我该怎么做?我也尝试实现 IDisposable 但我不知道您是否需要调用它或 garbage collector 调用它(对我来说不是)。 这是客户的代码(read writeloop 对于这个问题并不重要,但我留下了它以防有人需要它):

using Assets.Scripts.Tools;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
using System;

public class Client : System.Object, IDisposable{
    private TcpClient tcp;
    private Stream stream;
    private IClientCallback callback;
    private Thread recvThread;

    private static Client instance = null;

    public static Client Instance
    {
        get
        {
            if (instance == null)
                instance = new Client();
            return instance;
        }
    }

    private Client()
    {
        Init();
    }

    public void Init()
    {
        Log.D("Init the client");
        tcp = new TcpClient();
        tcp.Connect(Main.IP, Main.PORT);
        stream = tcp.GetStream();
        recvThread = new Thread(loop);
        recvThread.Start();
    }

    public void setCallback(IClientCallback callback)
    {
        this.callback = callback;
    }

    // recving loop
    private void loop()
    {
        while (true)
        {
            Message msg = Read();
            if (callback != null)
            {
                callback.Callback(msg);
            }
            else
            {
                Log.E("callbackPointer is null, msg not handheld");
            }
        }
    }

    public void disconnect()
    {
        tcp.Close();
        stream.Close();
    }

    // read a message from the server (will wait until a message is recved
    private Message Read()
    {
        try
        {
            int val;
            string res = "";
            do
            {
                val = stream.ReadByte();
                res += (char)val;
            } while (val != Code.END_MESSAGE && val != -1);
            return new Message(res);
        }
        catch (IOException)
        {
            Log.E("Could not read from the server, disconnecting");
            throw new System.Exception("could not read from the server");
        }
    }

    // Write a message to the server
    public void Write(Message msg)
    {
        try
        {
            Log.D("Write:", msg.getDebug());
            stream.Write(msg.getBytes(), 0, msg.getBytes().Length);
            stream.Flush();
        }
        catch (IOException)
        {
            Log.E("Could not send message to the client");
            throw new System.Exception("could not write to the server: " + msg.getCode().ToString() + msg.toString());
        }
    }

    public void Dispose()
    {
        Log.D("never been called");
    }

    ~Client()
    {
        Log.D("never been called");
    }
}

使用 OnApplicationQuit() 回调函数并断开与您的 TCP 服务器的连接或在其中调用您的 disconnect() 方法。此回调将 运行 在应用程序关闭之前,在编辑器和内置的独立播放器中都是如此。
OnApplicationQuit()