当前位置: 首页> 财经> 创投人物 > 优秀企业宣传ppt_ps网页设计培训_竞价推广托管公司价格_站长工具名称查网站

优秀企业宣传ppt_ps网页设计培训_竞价推广托管公司价格_站长工具名称查网站

时间:2025/7/13 6:51:58来源:https://blog.csdn.net/Monkey_Xuan/article/details/143179752 浏览次数:0次
优秀企业宣传ppt_ps网页设计培训_竞价推广托管公司价格_站长工具名称查网站

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

关键字:优秀企业宣传ppt_ps网页设计培训_竞价推广托管公司价格_站长工具名称查网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: