当前位置:网站首页>Unity3d click events added to 3D objects in the scene

Unity3d click events added to 3D objects in the scene

2022-07-07 16:07:00 Le_ Sam

 

Unity3D - Scene 3D Add mouse click event to the object

Mouse click 3D Object trigger ,Unity In essence, there are two : One is through events (event) Trigger , One is through rays (ray) Judge whether the passing object triggers . The principles of these two triggers are different , No matter what kind of trigger, it must meet the trigger requirements , Since the principle is different , The requirements for triggering are also different , Let's elaborate on 3 Different ways .
 
Unity edition 5.6.2f  Unity API edition 2019.1 
 
 
One 、 Through the event , Implementation interface IPointerClickHandler 
First understand the interface IPointerClickHandler, This connection is   UnityEngine.EventSystems.Interfaces.IPointerClickHandler, Now let's see what other interfaces it can implement
Unity3D- Scene 3D Object add click event
Here we use IPointerClickHandler give an example (api It's like this ):
Unity3D- Scene 3D Object add click event
That is, after implementing the interface in our clicked class , In the implementation method OnPointerClick, Operate the post click processing in this method .
 
Since it's an event , First, meet the requirements of event triggering :
 
1. To the in the scene Camera add to PhysicsRaycaster Script .
Unity3D- Scene 3D Object add click event
 
 
2. Add an event system to the scene EventSystem, Operate as shown in the figure :
Unity3D- Scene 3D Object add click event
 
Click on Add Default... Then it appears as shown in the picture :
Unity3D- Scene 3D Object add click event
 
3. The key is coming. , Set the clicked object (3D) object , Be sure to bring a trigger (Collider), Then hang the post-processing script on the clicked object .
Unity3D- Scene 3D Object add click event

Come here , There is only one script we need to develop ,Demo Inside EventClick.cs 
 
 
using UnityEngine;
using UnityEngine.EventSystems;

public class EventClick : MonoBehaviour,IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        print(" Click. ::"+this.name);
    }
}

 

 
Run Click cube:
Unity3D- Scene 3D Object add click event
 
 
Two 、 Through the event , Attach script EventTrigger
 
This is also achieved through the same event , Also follow the One Meet the requirements of event triggering ( Don't understand, ! Look up ):
1. To the in the scene Camera add to PhysicsRaycaster Script .
2. Add an event system to the scene EventSystem;
After completing the above two steps ,3 It's different from Yili's .
3. There are two ways , Namely 3.1 and 3.2 , It's just different in usage , The principle is the same .
First, add EventTrigger Script .
Unity3D- Scene 3D Object add click event
 
3.1, It is triggered by statically added events
to EventTrigger Add event type , Select as needed ,
Unity3D- Scene 3D Object add click event
 
Demo Select click event PointerClick
Unity3D- Scene 3D Object add click event
 
to PointerClick Add post trigger execution function
Unity3D- Scene 3D Object add click event
 
The execution function here , It is the class we developed below ClickEventTrigger.cs, Add this script to the clicked object .
 
 
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class ClickEventTrigger : MonoBehaviour 
{
public void OnClick()
    {
        print("MyOnClick  Click. ::"+this.name);
    }
}
 
 
Come here , Adding events statically is complete , Running effect :
Unity3D- Scene 3D Object add click event
 
3.2, It is triggered by dynamically adding events
Dynamic trigger , stay Unity It is much more convenient to operate in the editor , Just add EventTrigger That's all right. , Then put our script ClickEventTrigger.cs Add to the clicked object , The rest is done in code .
 
 
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class ClickEventTrigger : MonoBehaviour {

// Use this for initialization
void Start () {

        // Get or add EventTrigger Components 
        EventTrigger trigger = transform.GetComponent();
        if (trigger == null)
        {
            trigger = transform.gameObject.AddComponent();
        }
        // initialization EventTrigger.Entry Array of   If the event trigger array is initialized here , So in ide Statically added events will be lost 
        //trigger.triggers = new List();
        // Create a variety of  EventTrigger.Entry The type of 
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerEnter;// Set up Entry Of eventID type   namely EventTriggerType Various enumerations of ( For example, mouse click , slide , Drag, etc )
        UnityAction callback = new UnityAction(OnPointerEnter);  // Registration agent 
        entry.callback.AddListener(callback);// Add proxy events to EventTrigger.Entry

        EventTrigger.Entry entry2 = new EventTrigger.Entry();
        entry2.eventID = EventTriggerType.PointerDown;
        UnityAction callback1 = new UnityAction(OnPointerDown);
        entry2.callback.AddListener(callback1);

        // stay EventTrigger.Entry Add to the array of 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");
    }
}

 

 
Running results :
Unity3D- Scene 3D Object add click event
 
3、 ... and 、 By ray ,RayCastHitControl
This method is mainly through , Ray emission , Judge the object through which the ray passes , Judge whether the object is clicked according to the specific sign ( such as : The label of the object Tag, And the name of the object )
All we need here is 3d object , And there is a clear and unique identification , Plus what we developed ClickRayCastHitControl.cs Can .
Unity3D- Scene 3D Object add click event
 
1. Create an object in the scene (Cube), And then click Inspector Under the panel Tag - AddTag...- stay Tags In the list , Click on +, Enter the you want to add Tag Name is OK . establish Tag After completion , Go back to the scene and give this Cube Choose the Tag.
Unity3D- Scene 3D Object add click event
 
2. Creating a Cube,demo Medium name (BeiJiChaun), This name is the symbol of ray passing judgment , This object is created .
Unity3D- Scene 3D Object add click event
 
3. Create a separate layer , Just mount the script we developed .
Unity3D- Scene 3D Object add click event
 
Running results :
Unity3D- Scene 3D Object add click event
 
Complete code :
 
 
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(" Left click ");
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log(hit.collider.gameObject.name);
                obj = hit.collider.gameObject;
                // By name 
                if (obj.name.Equals("BeiJiChuan"))
                {
                    Debug.Log(" Point in " + obj.name);
                }
                // Through the label 
                if (obj.tag == "ClicObj")
                {
                    Debug.Log(" Point in " + obj.name);
                }
            }
        }
    }
}

 

原网站

版权声明
本文为[Le_ Sam]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130609270505.html