张金锋 发表于 2019-3-1 13:46:10

U3D事件系统用于3D物体

U3D的事件系统用于3D物体,首先需要太场景中要有EventSystem(引物体在UI中有)这个物体,然后摄像机上需要挂载组件
///此脚本挂载在要被点击的物体上,凡是挂载此脚本的物体都可以接收点击事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class NewBehaviourScript : MonoBehaviour, IPointerClickHandler
{
    public class My_UnityEvent : UnityEvent<string, PointerEventData> { }
    //声明静态的字段
    public static My_UnityEvent M_UnityEvent = new My_UnityEvent();
    //实现点击接口
    public void OnPointerClick(PointerEventData eventData)
    {
      //点击时,调用事件
      M_UnityEvent.Invoke(gameObject.name, eventData);
    }
}

///此脚本随便挂载在一个物体上。
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class NewBehaviourScript1 : MonoBehaviour {
       
        void Start () {
      NewBehaviourScript.M_UnityEvent.AddListener((str, pointerevent) => { Debug.Log(str+pointerevent.ToString()); });
        }
       
}

点击物体效果如下,



非法昵称12138 发表于 2019-3-1 14:10:42

元素帖子强,满满正能量!

有点淡定 发表于 2019-3-1 20:17:44

路过看看 感谢分享

有点淡定 发表于 2019-3-1 20:17:46

路过看看 感谢分享

有点淡定 发表于 2019-3-1 20:17:49

路过看看 感谢分享

有点淡定 发表于 2019-3-3 08:04:39

路过看看 感谢分享

有点淡定 发表于 2019-3-3 08:04:41

路过看看 感谢分享

MasterZjy 发表于 2019-3-12 08:57:29

我觉得此贴可以解决窝目前遇到的问题

张前进 发表于 2019-3-24 19:41:05

超级棒棒棒

温柔致死@L 发表于 2019-3-25 12:16:02

看着像是unity的监听

有点淡定 发表于 2019-3-25 20:51:42

路过看看 感谢分享

有点淡定 发表于 2019-3-25 20:51:44

路过看看 感谢分享

peet07 发表于 2019-3-26 20:06:49


感谢楼主分享资源

peet07 发表于 2019-3-26 20:06:59


感谢楼主分享资源

有点淡定 发表于 2019-3-26 20:51:02

路过看看 感谢分享

有点淡定 发表于 2019-3-26 20:51:03

路过看看 感谢分享

haha123 发表于 2019-3-27 08:39:10

谢谢分享

模模糊糊day 发表于 2019-3-27 09:02:59

分享快乐,谢谢分享这么好的资源

ZCK706 发表于 2021-6-17 09:44:21

想要成大触,天天上元素!

ZCK706 发表于 2021-6-17 09:45:09

想要成大触,天天上元素!
页: [1] 2
查看完整版本: U3D事件系统用于3D物体