C#/Unity - 无法从异步方法调用 Unity 方法

我正在使用 Unity3D。这是我的异步方法:


private void Receive(IAsyncResult ar)

{

    try

    {

        IPEndPoint ipEndPoint = null;

        byte[] data = udpClient.EndReceive(ar, ref ipEndPoint);

        string receivedMessage = Encoding.UTF8.GetString(data);


        JsonData json = JsonConvert.DeserializeObject<JsonData>(receivedMessage);

        string prefix = json.header.Substring(0, 2);


        Debug.Log("UDP World: " + receivedMessage);


        if (prefix != "3x")

        {

            Debug.Log("Unknown packet: " + receivedMessage + "\n");

        }

        else

        {

            string header = json.header.Substring(2);

            int conId = json.connectionId;

            switch (header)

            {

                default:

                    Debug.Log("Unknown packet: " + receivedMessage + "\n");

                    break;

                case "001":

                    Debug.Log("Data received: " + receivedMessage + "\n");

                    break;

                case "002":

                    CharacterPosition position = new CharacterPosition();

                    position.x = float.Parse(json.data["position.x"].ToString());

                    position.y = float.Parse(json.data["position.y"].ToString());

            }

        }

    } 

我在 Unity 控制台中看到的只是以下输出:


Debug.Log("Position 2: X: " + position.x + " Y: " + position.y + " Z: " + position.z + " Character: " + characterId + " CnnID: " + cnnId);

使用上面显示的 Debug.Log,我可以看到我拥有所需的所有信息。Debug.Log 中的所有内容都在那里。下一行:


Debug.Log("GameObject FIND: " + GameObject.Find("CharactersOnline").name);

似乎是一切刹车的地方。我没有从该行收到任何输出,也没有任何错误。它看起来就像它留在那里没有给出任何输出或错误。


当updateCharacterPosition从另一个非异步方法调用时,内部的所有内容都updateCharacterPosition按缩进方式工作。


知道为什么会发生这种奇怪的行为。我该如何解决这个问题,以便我可以调用updateCharacterPosition哪个ReceiveAsync 函数?


喵喵时光机
浏览 221回答 1
1回答

茅侃侃

试试这个:/* 将它附加到场景中的任何对象上,以使其工作 */using UnityEngine;using System.Collections;using System.Collections.Generic;public class MainThread : MonoBehaviour {&nbsp; &nbsp;class CallInfo&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;public Function func;&nbsp; &nbsp; &nbsp;public object parameter;&nbsp; &nbsp; &nbsp;public CallInfo(Function Func, object Parameter)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;func = Func;&nbsp; &nbsp; &nbsp; &nbsp;parameter = Parameter;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;public void Execute()&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;func(parameter);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;public delegate void Function(object parameter);&nbsp; &nbsp;public delegate void Func();&nbsp; &nbsp;static List<CallInfo> calls = new List<CallInfo>();&nbsp; &nbsp;static List<Func> functions = new List<Func>();&nbsp; &nbsp;static Object callsLock = new Object();&nbsp; &nbsp;static Object functionsLock = new Object();&nbsp; &nbsp;void Start()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;calls = new List<CallInfo>();&nbsp; &nbsp; &nbsp;functions = new List<Func>();&nbsp; &nbsp; &nbsp;StartCoroutine(Executer());&nbsp; &nbsp;}&nbsp; &nbsp;public static void Call(Function Func, object Parameter)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;lock(callsLock)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;calls.Add(new CallInfo(Func, Parameter));&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;public static void Call(Func func)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;lock(functionsLock)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;functions.Add(func);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;IEnumerator Executer()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;while(true)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;yield return new WaitForSeconds(0.01f);&nbsp; &nbsp; &nbsp; &nbsp;while(calls.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;calls[0].Execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lock(callsLock)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;calls.RemoveAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;while(functions.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;functions[0]();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lock(functionsLock)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;functions.RemoveAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}像这样打电话MainThread.Call(YourFunction);MainThread.Call(YourFunction, parameters);我认为您的问题是您正在从另一个不允许的线程调用 Unity 方法
打开App,查看更多内容
随时随地看视频慕课网APP