当前位置: 首页 > news >正文

Unity中使用UnityEvent遇到Bug

UnityEvent绑定过程中,放在Start()中绑定会报错(通过脚本添加UnityEvent事件脚本,绑定)

绑定事件放在OnEnable()中不会报错,但是依然不可以立刻添加UnityEvent事件脚本紧接着立刻绑定事件。

或者在Start()添加UnityEvent事件的脚本与绑定间隔时间长一些也不会报错。

示例:

创建点击对象方法

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class My3DObjOnClick : MonoBehaviour
{public UnityEvent events;private void OnMouseUpAsButton(){// if (EventSystem.current.IsPointerOverGameObject()) return;events.Invoke();}
}

报错案例1: 

创建绑定方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class Test : MonoBehaviour
{public Transform tar;//private void Start(){add_click_listener(Redian1_Click);}public void add_click_listener( UnityAction onclick){tar.gameObject .AddComponent<My3DObjOnClick>();tar.GetComponent<My3DObjOnClick>().events.AddListener(onclick);}private void Redian1_Click(){Debug.Log("Redian1_Click");}
}

报错案例2:  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class Test : MonoBehaviour
{public Transform tar;private void Start(){  add_click_listener(Redian1_Click);}public void add_click_listener( UnityAction onclick){tar.gameObject .AddComponent<My3DObjOnClick>(); StartCoroutine("ceshi", onclick);}IEnumerator ceshi(UnityAction onclick){yield return new WaitForEndOfFrame  ();//如果将时间延长,延长到5s则不会报错//  yield return new WaitForSeconds (5);tar.GetComponent<My3DObjOnClick>().events.AddListener(onclick);}private void Redian1_Click(){Debug.Log("Redian1_Click");// panelModel1_UICtrl.Open_Redian1();}
}

 报错案例3:  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class Test : MonoBehaviour
{public Transform tar;private void OnEnable(){   add_click_listener(Redian1_Click);}public void add_click_listener( UnityAction onclick){tar.gameObject .AddComponent<My3DObjOnClick>();tar.GetComponent<My3DObjOnClick>().events.AddListener(onclick);}private void Redian1_Click(){Debug.Log("Redian1_Click");}
}

  不报错案例1:  

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Test : MonoBehaviour
{public Transform tar;private void OnEnable(){add_click_listener(Redian1_Click);}public void add_click_listener( UnityAction onclick){tar.gameObject .AddComponent<My3DObjOnClick>();StartCoroutine("ceshi", onclick);}IEnumerator ceshi(UnityAction onclick){yield return new WaitForEndOfFrame  ();tar.GetComponent<My3DObjOnClick>().events.AddListener(Redian1_Click);}private void Redian1_Click(){Debug.Log("Redian1_Click");}
}

不报错案例2:   

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class Test : MonoBehaviour
{public Transform tar;private void Start(){  add_click_listener(Redian1_Click);}public void add_click_listener( UnityAction onclick){tar.gameObject .AddComponent<My3DObjOnClick>(); StartCoroutine("ceshi", onclick);}IEnumerator ceshi(UnityAction onclick){yield return new WaitForSeconds (5);tar.GetComponent<My3DObjOnClick>().events.AddListener(onclick);}private void Redian1_Click(){Debug.Log("Redian1_Click");// panelModel1_UICtrl.Open_Redian1();}
}

 不报错案例3:   

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class Test : MonoBehaviour
{public Transform tar;private void OnEnable(){tar.gameObject .AddComponent<My3DObjOnClick>();}private void Update(){if (Input.GetKeyDown(KeyCode.W)){tar.GetComponent<My3DObjOnClick>().events.AddListener(Redian1_Click);}}private void Redian1_Click(){Debug.Log("Redian1_Click");}
}


http://www.mrgr.cn/news/56771.html

相关文章:

  • 每日一练 —— map习题
  • 软件测试学习笔记丨Selenium学习笔记:元素定位与操作
  • Mbox网关在风力发电产业:破除痛点,驱动收益
  • dump文件生成代码
  • 编程新手小白入门最佳攻略
  • 【MATLAB源码-第187期】基于matlab的人工蜂群优化算法(ABC)机器人栅格路径规划,输出做短路径图和适应度曲线。
  • PC版Windows电脑微信双开|微信分身神器|同一台电脑端微信分身微信多开
  • 高频电源模块HXT240D10直流屏充电模块HXT240D05整流器HXT120D10
  • 国产数据库正在崛起,为什么少不了OceanBase?
  • lombok 总结
  • 1208. 尽可能使字符串相等
  • 杂项 基础知识整体
  • 使用皮尔逊相关系数矩阵进行特征筛选
  • element 按钮变形 el-button样式异常
  • 川菜出海平台国际市场系统功能开发分析
  • (自用复习题)常微分方程06
  • Nodejs访问.env配置文件
  • 可转债连载
  • 解决 @Scope 注解失效问题:深入理解与排查方法
  • 基于SSM的教务信息平台【附源码】