Unity-NGUI爆错以后-导致不能多次点击,UI假卡死问题解决方法

发布于:2024-05-13 ⋅ 阅读:(181) ⋅ 点赞:(0)

太久没用,忘了,NGUI好像易出错,就再次点击不了

导致打开了UI关闭不了,每次都要重启就比较烦(说的就是那种美术团队,一个 UI 打开几十层)

就好比【左上角,箭头】点第二次是退出不了了

断电之后发现是【UICamra】

  1. UiCamera一直使用 Update 做监听,
  2. 就是使用 gameObject.SendMessage();//基于Raycast Collider等,已失传的3D转2D-UI的实现
  3. 发送"OnClick“等事件
  4. 而UIButton刚好又写了 void OnClick(){}
  5. 所以正好就在UIButton接收了

要怎么办呢,如果想更个人定制化一点,

其实自己做过UIManager的都知道(Update()事件,Event.Dispatch()事件都是很麻烦的)

如果想要好用,就是加上 try catch 

因为在这个Event 派送的过程中,就很可能,报错,导致,后面的代码执行不了,就不能再次点击,这就是为什么NGUI只要报错一次,点击事件就失效了

原代码如下:

	/// <summary>
	/// Call the listener function.
	/// </summary>

	protected virtual void OnClick ()
	{
		if (current == null && isEnabled)
		{
			current = this;
			
			if (UiButtonClickEvent != null)
			{
				UiButtonClickEvent(this);
			}
			
			EventDelegate.Execute(onClick);
			current = null;
		}
	}

最终代码如下

/// <summary>
	/// 类:UIButton.cs
	/// Call the listener function.
	/// </summary>
	protected virtual void OnClick ()
	{
		if (current == null && isEnabled)
		{
			current = this;
			try //加了一个Try Catch 保证即时,Event()内部错了,后面还是可以正常Set Null
			{
				if (UiButtonClickEvent != null)
				{
					UiButtonClickEvent(this);
				}
			
				EventDelegate.Execute(onClick);
			}
			catch (Exception e)
			{
				Debug.LogError(e);	
			}
			
			current = null;
		}
	}

写上上面的代码后,即时出错了,还是会正常能多次操作UI,保证容错性,至于如何定位log(毕竟多包了一层,就你懂的,各施各法,任君选择,只要不是强制把最顶层的log抓上来就行了)


网站公告

今日签到

点亮在社区的每一天
去签到