unity 3D物体添加 点击事件

 void Awake()
        {
            Instance = this;

            AddEventTrigger(YangFangTu, OnYangFangTu);
            AddEventTrigger(HongGanXiang, OnHongGanXiang);
        }

        /// 
        /// 添加 物体点击 事件
        /// 
        /// 
        /// 
        public void AddEventTrigger(GameObject obj, UnityAction action_)
        {
            if (obj.transform.GetComponent()==null)
            {
                obj.AddComponent();
            }
            EventTrigger trigger = obj.transform.GetComponent();
            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = EventTriggerType.PointerClick;
            entry.callback.AddListener(action_);
            trigger.triggers.Clear();
            trigger.triggers.Add(entry);
        }

方法一:物体 挂在 EventTrigger  选择 PointerClick  注册响应方法

相机 必须挂在 PhysicsRaycaster

场景中必须有:EventSystem(一般创建UI自动添加的)

物体 必须有 Collider

void Awake()
        {
            Instance = this;

            EventTrigger trigger = YangFangTu.transform.GetComponent();
            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = EventTriggerType.PointerClick;
            entry.callback.AddListener(OnYangFangTu);
            trigger.triggers.Clear();
            trigger.triggers.Add(entry);

        }

        public void OnYangFangTu(BaseEventData data = null)
        {
            
        }

简单粗暴版(相机 必须挂在 PhysicsRaycaster)

using HighlightPlus;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;


public class ObjClickEvent : MonoBehaviour
{
    public GameObject[] cube1; //3D物体集合

    private void Start()
    {
        //为3D物体添加点击事件
        for (int i = 0; i < cube1.Length; i++)
        {
            AddObjectClickEvent(cube1[i]);
        }
    }
    public void OnClick()
    {
        Debug.Log("OnClick");
        transform.GetComponent().highlighted = false;
    }

    public void AddObjectClickEvent(GameObject itemObject)
    {
        var box = itemObject.GetComponent();
        if (box == null)
        {
            box = itemObject.AddComponent();
        }
        var item = itemObject.GetComponent();
        if (item == null)
        {
            item = itemObject.AddComponent();
        }

        EventTrigger trigger = itemObject.GetComponent();
        if (trigger == null)
        {
            trigger = itemObject.AddComponent();
        }
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerClick;
        entry.callback.AddListener(item.OnClickItem);
        trigger.triggers.Clear();
        trigger.triggers.Add(entry);
    }
}
 public void OnClickItem(UnityEngine.EventSystems.BaseEventData data = null)
    {
        //点击对应事件
       // Debug.Log("点击物体:"+ Name_);
        if (bo)
        {
            this.transform.GetComponent().highlighted = false;
            UIChaJuZhiLing.Instance.OnZhiLing(Name_);
        }
    }
 public class TooIkitObjItme : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     public void OnClickItem(BaseEventData data = null)
     {
        // Debug.Log("name="+this.name);
         //var box = this.GetComponent();
         //if (box != null)
         //{
         //    this.GetComponent().enabled = false;
         //}
         //transform.GetComponent().highlighted = false;
         UIToolkit.Instance.Toolkit3DItmeEvent_.Invoke(this.name);
         UIExplain.Instance.SetState(false);
         this.gameObject.SetActive(false);
     }
     public void OnPointerEnter(PointerEventData eventData)
     {
        // Debug.Log("进入");
         UIExplain.Instance.SetState(true, this.name);
     }
     //鼠标离开
     public void OnPointerExit(PointerEventData eventData)
     {
        // Debug.Log("出去");
         UIExplain.Instance.SetState(false, this.name);
     }

    

 }

你可能感兴趣的:(#,unity之日常开发,unity)