[Unity] U3D事件系统用于3D物体

查看:869 |回复:20 | 2019-3-1 13:46:10

您需要 登录 才可以下载或查看,没有账号?注册

x
U3D的事件系统用于3D物体,首先需要太场景中要有EventSystem(引物体在UI中有)这个物体 440455c78c5979b135.png ,然后摄像机上需要挂载组件 179105c78c57ab8603.png
///此脚本挂载在要被点击的物体上,凡是挂载此脚本的物体都可以接收点击事件
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()); });
        }
       
}

点击物体效果如下,



2019-3-1 13:46:10  
 赞 赞 0

使用道具 登录

20个回答,把该问题分享到群,邀请大神一起回答。
2#
元素帖子强,满满正能量!
回复 收起回复
2019-3-1 14:10:42   回复
 赞 赞 0

使用道具 登录

3#
路过看看 感谢分享
回复 收起回复
2019-3-1 20:17:44   回复
 赞 赞 0

使用道具 登录

4#
路过看看 感谢分享
回复 收起回复
2019-3-1 20:17:46   回复
 赞 赞 0

使用道具 登录

5#
路过看看 感谢分享
回复 收起回复
2019-3-1 20:17:49   回复
 赞 赞 0

使用道具 登录

6#
路过看看 感谢分享
回复 收起回复
2019-3-3 08:04:39   回复
 赞 赞 0

使用道具 登录

7#
路过看看 感谢分享
回复 收起回复
2019-3-3 08:04:41   回复
 赞 赞 0

使用道具 登录

8#
我觉得此贴可以解决窝目前遇到的问题
回复 收起回复
2019-3-12 08:57:29   回复
 赞 赞 0

使用道具 登录

9#
超级棒棒棒
回复 收起回复
2019-3-24 19:41:05   回复
 赞 赞 0

使用道具 登录

10#
看着像是unity的监听
回复 收起回复
2019-3-25 12:16:02   回复
 赞 赞 0

使用道具 登录

11#
路过看看 感谢分享
回复 收起回复
2019-3-25 20:51:42   回复
 赞 赞 0

使用道具 登录

12#
路过看看 感谢分享
回复 收起回复
2019-3-25 20:51:44   回复
 赞 赞 0

使用道具 登录

13#

感谢楼主分享资源
回复 收起回复
2019-3-26 20:06:49   回复
 赞 赞 0

使用道具 登录

14#

感谢楼主分享资源
回复 收起回复
2019-3-26 20:06:59   回复
 赞 赞 0

使用道具 登录

15#
路过看看 感谢分享
回复 收起回复
2019-3-26 20:51:02   回复
 赞 赞 0

使用道具 登录

16#
路过看看 感谢分享
回复 收起回复
2019-3-26 20:51:03   回复
 赞 赞 0

使用道具 登录

17#
谢谢分享
回复 收起回复
2019-3-27 08:39:10   回复
 赞 赞 0

使用道具 登录

18#
分享快乐,谢谢分享这么好的资源
回复 收起回复
2019-3-27 09:02:59   回复
 赞 赞 0

使用道具 登录

19#
想要成大触,天天上元素!
回复 收起回复
2021-6-17 09:44:21   回复
 赞 赞 0

使用道具 登录

20#
想要成大触,天天上元素!
回复 收起回复
2021-6-17 09:45:09   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

手机游戏引擎手游引擎Unity3D技术
12下一页
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表