Reprint: Application on click event

 

Original Address: http: //blog.sina.com.cn/s/blog_c3e21e750102yfnm.html

Unity3D- 3D objects in the scene to add a click event

 
label: 

it

Category:  Unity
Unity3D- 3D objects in the scene to add a click event

Unity3D - 3D objects in the scene to add a mouse click event
3D mouse to click on objects to trigger, Unity In essence, there are two: one is through events (event) is triggered, one is triggered by radiation passing through the object (ray) judgment. These two principles trigger is different, no matter what the trigger must meet before they can trigger, since different principles, requirements triggers are not the same, the following detailed descriptions of three different ways.
 
Unity version 5.6.2f Unity API version 2019.1 
 
 
First, through the event, implement the interface IPointerClickHandler 
Interface IPointerClickHandler at first understand, then this tree relationship is UnityEngine.EventSystems.Interfaces.IPointerClickHandler, look at it below what other interfaces can be achieved
Unity3D- 3D objects in the scene to add a click event
Examples used here IPointerClickHandler (explained in api):
Unity3D- 3D objects in the scene to add a click event
After implements an interface that is clicked in our class, in the implementation OnPointerClick, treatment after the operation In this method click.
 
Since it is an event, first of all meet the requirements of event-triggered:
 
1. Add PhysicsRaycaster script to the scene Camera.
Unity3D- 3D objects in the scene to add a click event

 
2. Add EventSystem system event in the scene, as shown in operation:
Unity3D- 3D objects in the scene to add a click event

Figure appears after clicking Add Default ...:
Unity3D- 3D objects in the scene to add a click event

3. The focus here, the setting is click on the object (3D) object, be sure to bring flip-flops (Collider), and then click scripting hanging on the object is clicked.
Unity3D- 3D objects in the scene to add a click event

到这里,需要我们开发的只有一个脚本,Demo里的EventClick.cs 
 
 
using UnityEngine;
using UnityEngine.EventSystems;
 
public class EventClick : MonoBehaviour,IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        print("点击了::"+this.name);
    }
}
 
运行点击cube:
Unity3D- 3D objects in the scene to add a click event

 
二、通过事件,附加脚本EventTrigger
 
这里也是同过事件来实现的,也要按照 一 里满足事件触发的要求(不懂!往上翻看下):
1.给场景中的Camera添加PhysicsRaycaster脚本。
2.在场景中添加事件系统EventSystem;
完成以上两步后,3就和一里的不一样了。
3.这里有两种做法,分别是3.1 和 3.2 ,只是在用法上不同,原理是一样的。
首先给被点击的物体添加EventTrigger脚本。
Unity3D- 3D objects in the scene to add a click event

3.1,是静态添加事件触发
给EventTrigger添加事件类型,根据需要选择,
Unity3D- 3D objects in the scene to add a click event

Demo里选择点击事件PointerClick
Unity3D- 3D objects in the scene to add a click event

给PointerClick添加触发后执行函数
Unity3D- 3D objects in the scene to add a click event

这里的执行函数,是下面我们开发的类ClickEventTrigger.cs,把这个脚本添加到被点击的物体上即可。
 
 
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
 
public class ClickEventTrigger : MonoBehaviour 
{
public void OnClick()
    {
        print("MyOnClick 点击了::"+this.name);
    }
}
 
到这里,静态添加事件就完成了,运行效果:
Unity3D- 3D objects in the scene to add a click event

3.2,是动态添加事件触发
动态触发,在Unity编辑器中操作就方便多了,只需要在被点击物体上添加EventTrigger就可以了,然后再把我们的脚本ClickEventTrigger.cs添加到被点击物体上,剩下的都在代码中完成。
 
 
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
 
public class ClickEventTrigger : MonoBehaviour {
 
// Use this for initialization
void Start () {
 
        //获取或添加EventTrigger组件
        EventTrigger trigger = transform.GetComponent();
        if (trigger == null)
        {
            trigger = transform.gameObject.AddComponent();
        }
        //初始化EventTrigger.Entry的数组 如果这里初始化了事件触发数组,那么在ide静态添加的事件会丢失
        //trigger.triggers = new List();
        //创建各种 EventTrigger.Entry的类型
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerEnter;//设置Entry的eventID类型 即EventTriggerType的各种枚举(比如鼠标点击,滑动,拖动等)
        UnityAction callback = new UnityAction(OnPointerEnter);  //注册代理
        entry.callback.AddListener(callback);//添加代理事件到EventTrigger.Entry
 
        EventTrigger.Entry entry2 = new EventTrigger.Entry();
        entry2.eventID = EventTriggerType.PointerDown;
        UnityAction callback1 = new UnityAction(OnPointerDown);
        entry2.callback.AddListener(callback1);
 
        //在EventTrigger.Entry的数组添加EventTrigger.Entry
        trigger.triggers.Add(entry);
        trigger.triggers.Add(entry2);
    }
 
    private void OnPointerDown(BaseEventData arg0)
    {
        Debug.Log("OnPointerDown");
    }
 
    private void OnPointerEnter(BaseEventData arg0)
    {
        Debug.Log("OnPointerEnter");
    }
}
 
运行结果:
Unity3D- 3D objects in the scene to add a click event

三、通过射线,RayCastHitControl
这个方法主要是通过,射线发射,判断射线穿过的物体,根据特定的标志来判断是否点击到了该物体(比如:物体的标签Tag,还有物体的名称)
这里只需要3d物体,并且有明确的唯一标识,再加上我们开发的ClickRayCastHitControl.cs就可以。
Unity3D- 3D objects in the scene to add a click event

1.在场景中创建一个物体(Cube),然后点击Inspector面板下Tag - AddTag...- 在Tags列表中,点击+,输入你要添加的Tag名称就可以。创建Tag完成后,回到场景给这个Cube选择刚才创建的Tag。
Unity3D- 3D objects in the scene to add a click event

2.在创建一个Cube,demo中起名(BeiJiChaun),这个名字就是射线穿过判断的标识,这个物体就创建完成了。
Unity3D- 3D objects in the scene to add a click event

3.创建单独层,挂载我们开发的脚本即可。
Unity3D- 3D objects in the scene to add a click event

运行结果:
Unity3D- 3D objects in the scene to add a click event

完整代码:
 
 
using UnityEngine;
 
public class ClickRayCastHitControl : MonoBehaviour {
 
    Ray ray;
    RaycastHit hit;
    GameObject obj;
    // Use this for initialization
    void Start () {
 
}
 
    // Update is called once per frame
    
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("点击鼠标左键");
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log(hit.collider.gameObject.name);
                obj = hit.collider.gameObject;
                //通过名字
                if (obj.name.Equals("BeiJiChuan"))
                {
                    Debug.Log("点中" + obj.name);
                }
                // By label
                if (obj.tag == "ClicObj")
                {
                    Debug.Log ( "point" + obj.name);
                }
            }
        }
    }
}
 
 

Guess you like

Origin www.cnblogs.com/zbyglls/p/11356418.html