C# Unity3D未处理的异常未记录
我使用下面的lowe脚本在Unity3D中执行一些客户机/服务器通信。我的问题是,当服务器上发生错误时,我使用异常来通知客户机,如果这些异常没有通过Loom脚本处理,那么这些异常当前只会退出线程。如果异常未经处理,如何调试.Log()异常以通知开发人员 绝缘线束脚本:C# Unity3D未处理的异常未记录,c#,exception,unity3d,C#,Exception,Unity3d,我使用下面的lowe脚本在Unity3D中执行一些客户机/服务器通信。我的问题是,当服务器上发生错误时,我使用异常来通知客户机,如果这些异常没有通过Loom脚本处理,那么这些异常当前只会退出线程。如果异常未经处理,如何调试.Log()异常以通知开发人员 绝缘线束脚本: using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Threading
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using System.Linq;
public class Loom : MonoBehaviour
{
public static int maxThreads = 8;
static int numThreads;
private static Loom _current;
private int _count;
public static Loom Current
{
get
{
Initialize();
return _current;
}
}
void Awake()
{
_current = this;
initialized = true;
}
static bool initialized;
static void UnhandledHandler(object sender, UnhandledExceptionEventArgs args) {
Exception e = (Exception) args.ExceptionObject;
Debug.Log("MyHandler caught : " + e.Message);
Debug.Log("Runtime terminating: {0}" + args.IsTerminating);
}
static void Initialize()
{
if (!initialized)
{
if(!Application.isPlaying)
return;
initialized = true;
var g = new GameObject("Loom");
_current = g.AddComponent<Loom>();
}
}
private List<Action> _actions = new List<Action>();
public struct DelayedQueueItem
{
public float time;
public Action action;
}
private List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>();
List<DelayedQueueItem> _currentDelayed = new List<DelayedQueueItem>();
public static void QueueOnMainThread(Action action)
{
QueueOnMainThread( action, 0f);
}
public static void QueueOnMainThread(Action action, float time)
{
if(time != 0)
{
lock(Current._delayed)
{
Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = action});
}
}
else
{
lock (Current._actions)
{
Current._actions.Add(action);
}
}
}
public static Thread RunAsync(Action a)
{
Initialize();
while(numThreads >= maxThreads)
{
Thread.Sleep(1);
}
Interlocked.Increment(ref numThreads);
ThreadPool.QueueUserWorkItem(RunAction, a);
return null;
}
private static void RunAction(object action)
{
try
{
((Action)action)();
}
catch
{
}
finally
{
Interlocked.Decrement(ref numThreads);
}
}
void OnDisable()
{
if (_current == this)
{
_current = null;
}
}
// Use this for initialization
void Start()
{
}
List<Action> _currentActions = new List<Action>();
// Update is called once per frame
void Update()
{
lock (_actions)
{
_currentActions.Clear();
_currentActions.AddRange(_actions);
_actions.Clear();
}
foreach(var a in _currentActions)
{
a();
}
lock(_delayed)
{
_currentDelayed.Clear();
_currentDelayed.AddRange(_delayed.Where(d=>d.time <= Time.time));
foreach(var item in _currentDelayed)
_delayed.Remove(item);
}
foreach(var delayed in _currentDelayed)
{
delayed.action();
}
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用制度;
使用系统线程;
使用System.Linq;
公共类织机:单一行为
{
公共静态int maxThreads=8;
静态int numThreads;
专用静态织机_电流;
私人国际单位计数;
公共静态织机电流
{
收到
{
初始化();
返回电流;
}
}
无效唤醒()
{
_电流=这个;
初始化=真;
}
静态布尔初始化;
静态void UnhandledHandler(对象发送方,UnhandledExceptionEventArgs){
异常e=(异常)args.ExceptionObject;
Log(“MyHandler捕获:”+e.Message);
Log(“运行时终止:{0}”+args.IsTerminating);
}
静态void初始化()
{
如果(!已初始化)
{
如果(!Application.isPlaying)
回来
初始化=真;
var g=新游戏对象(“织布机”);
_当前=g.AddComponent();
}
}
私有列表_actions=新列表();
公共结构DelayedQueueItem
{
公共浮动时间;
公共行动;
}
私有列表_delayed=新列表();
列表_currentDelayed=新列表();
公共静态void QueueOnMainThread(操作)
{
QueueOnMainThread(操作,0f);
}
公共静态void QueueOnMainThread(操作、浮动时间)
{
如果(时间!=0)
{
锁定(当前。\u延迟)
{
当前._delayed.Add(新的DelayedQueueItem{time=time.time+time,action=action});
}
}
其他的
{
锁定(当前动作)
{
当前动作。添加(动作);
}
}
}
公共静态线程RunAsync(操作a)
{
初始化();
while(numThreads>=maxThreads)
{
睡眠(1);
}
联锁增量(参考numThreads);
QueueUserWorkItem(RunAction,a);
返回null;
}
私有静态void运行操作(对象操作)
{
尝试
{
((行动)行动)();
}
接住
{
}
最后
{
联锁减量(参考numThreads);
}
}
无效可禁用()
{
如果(_current==此)
{
_电流=零;
}
}
//用于初始化
void Start()
{
}
列表_currentActions=新列表();
//每帧调用一次更新
无效更新()
{
锁定(_操作)
{
_currentActions.Clear();
_currentActions.AddRange(_actions);
_动作。清除();
}
foreach(当前动作中的var a)
{
a();
}
锁定(_延迟)
{
_currentDelayed.Clear();
_currentDelayed.AddRange(_delayed.Where(d=>d.time)您可以使用。通常它捕获未处理的错误,并将堆栈跟踪、屏幕截图和日志作为电子邮件发送给开发人员。但您也可以触发手动发送电子邮件,即服务器错误