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");}
}