Unity中使用UnityEvent遇到Bug

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

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

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

示例:

创建点击对象方法

cs 复制代码
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:

创建绑定方法:

cs 复制代码
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:

cs 复制代码
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:

cs 复制代码
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:

cs 复制代码
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:

cs 复制代码
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:

cs 复制代码
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");
    }
}
相关推荐
北海651642 分钟前
Dots 常用操作
unity
YY-nb8 小时前
Unity Apple Vision Pro 开发教程:物体识别跟踪
unity·游戏引擎·apple vision pro
Cool-浩8 小时前
Unity 开发Apple Vision Pro物体识别追踪ObjectTracking
unity·ar·apple vision pro·mr·物体识别·vision pro教程·objecttracking
向宇it19 小时前
【从零开始入门unity游戏开发之——C#篇23】C#面向对象继承——`as`类型转化和`is`类型检查、向上转型和向下转型、里氏替换原则(LSP)
java·开发语言·unity·c#·游戏引擎·里氏替换原则
Cool-浩1 天前
Unity 开发Apple Vision Pro空间锚点应用Spatial Anchor
unity·游戏引擎·apple vision pro·空间锚点·spatial anchor·visionpro开发
吴梓穆1 天前
unity 最小后监听键盘输入
unity
一个程序员(●—●)1 天前
四元数旋转+四元数和向量相乘+音频相关
unity·游戏引擎
YY-nb1 天前
Apple Vision Pro 开发教程:通过 TestFlight 把开发的程序安装到其他的设备上测试
unity·apple vision pro
Ke-Di1 天前
Unity-URP设置单独渲染UI相机
ui·unity
冒泡P1 天前
【Lua热更新】上篇
开发语言·数据结构·unity·c#·游戏引擎·lua