unity3d是一个比较优秀3d商业引擎,但我们在实际的开发过程中,会碰到种种问题,让我们纠结、郁闷、烦心。。。总之我被很多奇奇怪怪的问题缠绕过。一小时,两小时甚至几天。。。然后解决后了,感觉无厘头的bug,时间一久又忘了。</span>
那么这里记录下来。能看到的就会给你带来方便!
1.interface在ios中的bug(unity4.5以前的版本中存在),可以参考Unity3d IOS项目中interface的多态指向的bug,这里已经有比较详细在描述了。
2.unityeditor4.5 WWW加载时,在android或ios设备上常常会有进度不变的问题那,那么可以检测进度长时间不变的时候去停止加载,下面有代码:我这里5秒发现进度没变化就重新停掉,然后重新加载
<span style="white-space:pre"> </span>private float _currentProgress;
private float _lastProgressTime;
private const float ERR_TIMEOUT = 5f;//5s秒进度没有变化要重新开始加载
private bool IsErrorProgress(float value)
{
if (_currentProgress != value)
{
_lastProgressTime = Time.realtimeSinceStartup;
_currentProgress = value;
return false;
}
if (Time.realtimeSinceStartup - _lastProgressTime > ERR_TIMEOUT)
{
_currentProgress = -1;
return true;
}
return false;
}
<span style="white-space:pre"> </span>while (!www.isDone)
{
if (IsErrorProgress(www.progress))
{
Debug.Log("manifest加载失败aaa:" + www.url);
<span style="color:#ff0000;"> www.Dispose();//会导致线程卡住。自己要注释掉这行。</span>
www = null;
LoadRemoteManifest();
yield break;
}
yield return 1;
}
于是问题解决了。
于是有一天,公司断网了(电脑可以连接上路由器,路由器连接不上外网)又发现了一个新问题,加载的时候每隔5sunityeditor就会卡几秒种。查原因,问题出现在红色的那行代码,www.Dispose(); 因为www加载过程中Dispose会导致主线程卡住。那么要把红色的这行代码给注释掉了,注(直接拔掉网线,或关闭连接时即电脑设备连接不到路由器,不会线程卡住)于是一切正常了。
3.关于UnityEngine.Debug.Log();不加Log的程序员肯定是灰常厉害的大神,但南瓜做不到大神的地位,还是要加Log的。可是在Unity 写好的程序中,这些Log,会有不小的开销,多的时候,会让我们机器卡顿。然后,我们可能会做个封装,再做做几个开关去控制LOG是否显示。不错的一个选择,可是双击Console控制台的时候,不能直接帮我们导航到想要的地方去了。挺麻烦的。好吧,介绍个好办法:只要把自己封装的这个Debug编译成.dll就OK啦!下面是代码,附件中的MDebug.dll哦,可以试试
#define enableLog
namespace Fan
{
// public sealed class MDebug
// {
// public static bool enableLog = true;
// public static void Log(object message)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.Log(message);
// }
//#endif
// }
// public static void Log(object message, UnityEngine.Object context)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.Log(message, context);
// }
//#endif
// }
// public static void LogError(object message)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.LogError(message);
// }
//#endif
// }
// public static void LogError(object message, UnityEngine.Object context)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.LogError(message, context);
// }
//#endif
// }
// public static void LogException(System.Exception exception)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.LogException(exception);
// }
//#endif
// }
// public static void LogException(System.Exception exception, UnityEngine.Object context)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.LogException(exception, context);
// }
//#endif
// }
// public static void LogWarning(object message)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.LogWarning(message);
// }
//#endif
// }
// public static void LogWarning(object message, UnityEngine.Object context)
// {
//#if enableLog
// if (enableLog)
// {
// UnityEngine.Debug.LogWarning(message, context);
// }
//#endif
// }
// }
}