unity3d 线程安全问题
gui相关的操作必须在Update 里调用,所以一般的解决办法是使用一个隐藏的MonoBehaviour,非UI线程里想做的操作都注册到这个隐藏的MonoBehaviour里
using UnityEngine;using System;using System.Collections;using System.Collections.Generic;namespace wlgame{ public delegate void GUITask (); public class TaskExecutorScript : MonoBehaviour { private QueueTaskQueue = new Queue (); private object _queueLock = new object (); // Update is called once per frame void Update () { lock (_queueLock) { if (TaskQueue.Count > 0) TaskQueue.Dequeue () (); } } public void ScheduleTask (GUITask newTask) { lock (_queueLock) { if (TaskQueue.Count < 100) TaskQueue.Enqueue (newTask); } } }}
AfterResponseFuntion afterFun = delegate(System.Object result) { taskExecutor.ScheduleTask (new GUITask (delegate { EditorUtility.DisplayDialog ("title", "aaaaaaa", "abc", "edf"); })); }; Communication.SendScore2Server (afterFun);