• unity 3D物体使用EventSystem响应事件


    在ugui中创建一个canvas 之后会自动创建一个EventSystem,用来处理UI上的时间响应。(可以通过UI>EventSystem创建EventSystem)

    EventSystem

    有三个组件:EventSystem、StandaloneInputModule、TouchInputModule,后面两个组件都继承自BaseInputModule。

    EventSystem组件主要负责处理输入、射线投射以及发送事件。一个场景中只能有一个EventSystem组件,并且需要BaseInputModule类型组件的协助才能工作。EventSystem在一开始的时候会把自己所属对象下的BaseInputModule类型组件加到一个内部列表,并且在每个Update周期通过接口UpdateModules接口调用这些基本输入模块的UpdateModule接口,然后BaseInputModule会在UpdateModule接口中将自己的状态修改成'Updated',之后BaseInputModule的Process接口才会被调用。

    BaseInputModule是一个基类模块,负责发送输入事件(点击、拖拽、选中等)到具体对象。EventSystem下的所有输入模块都必须继承自BaseInputModule组件。StandaloneInputModule和TouchInputModule组件是系统提供的标准输入模块和触摸输入模块,我们可以通过继承BaseInputModule实现自己的输入模块。

       除了以上两个组件,还有一个很重要的组件通过EventSystem对象我们看不到,它是BaseRaycaster组件。BaseRaycaster也是一个基类,前面说的输入模块要检测到鼠标事件必须有射线投射组件才能确定目标对象。系统实现的射线投射类组件有PhysicsRaycaster, Physics2DRaycaster, GraphicRaycaster。这个模块也是可以自己继承BaseRaycaster实现个性化定制。

      总的来说,EventSystem负责管理,BaseInputModule负责输入,BaseRaycaster负责确定目标对象,目标对象负责接收事件并处理,然后一个完整的事件系统就有.

      上边是从别的地方复制的...    

    下边是场景中的3D物体怎么用EventSystem响应事件

    1.首先确保场景中有一个EventSystem,添加方式最开始有提到。

    2.因为3d物体上没有canvas上边的GraphicRaycaster所以要Main Camera上添加Physics Raycaster,就相当于UI上的GraphicRaycaster

    3.创建一个Cube(或者其他什么随便你,但是确保物体上有collion组件,因为没有碰撞器无法检测射线),作为事件触发的3d物体

    4.在3D物体上(你创建的那个cube或者什么)挂上EventTrigger,然后指定怎么触发,指定要触发的方法,如下。

                

    5.在要触发事件的物体上(你创建的Cube或者其他什么),挂上如下脚本

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class CubeClickTest : MonoBehaviour//, IPointerDownHandler
    {
    
    	// Use this for initialization
    	void Start () {
    		
    	}
    	
    	// Update is called once per frame
    	void Update () {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log(Input.mousePosition);
            }
    	}
        public void PointDownHandler()//在cube上挂上EventTrigger并指定该方法
         {
            transform.position += new Vector3(1,1,1);
         }
    
        //public void OnPointerDown(PointerEventData eventData)//实现接口调用该方法
        //{
           // Debug.Log(eventData.button);//这里如果用鼠标左键触发就打印life,右键触发就打印right
           // Debug.Log(eventData);//你可以通过eventData获取更多信息,这里是打印鼠标的位置(跟Input.mousePosition输出结果是一样的)以及delta(0.0,0.0),不知道是啥...
           // Debug.Log(Input.mousePosition);
           //Debug.Log(eventData.pointerPressRaycast.gameObject.name);
           // Debug.Log("press");
        //}
    }
    

     当然也可以不挂EventTrigger,直接实现接口,返回第三步。

     4.引用接口(你想用什么方式触发就实现对应的接口,这里以鼠标按下为例),要引用UnityEngine.EventSystems然后实现接口方法。

      5.同样挂上上边脚本,将脚本中红色部分的注释取消,cube移除EventTrigger组件,然后就可以了。

    
    

     

    
    
    
  • 相关阅读:
    HDU 1556 Color the ball
    ECJTUACM16 Winter vacation training #4 题解&源码
    深入理解树状数组
    Codeforces 712C Memory and De-Evolution
    Codeforces 712B Memory and Trident
    Codeforces 708A Letters Cyclic Shift
    也谈matlab中读取视频的一个重要函数mmreader
    What are the advantages of different classification algorithms?
    机器学习算法中如何选取超参数:学习速率、正则项系数、minibatch size
    若心觉醒 自然平静--记我的大三
  • 原文地址:https://www.cnblogs.com/lanrenqilanming/p/7001983.html
Copyright © 2020-2023  润新知