1. 程式人生 > >unity EventTrigger與模型

unity EventTrigger與模型

最近要做個東西,裡面需要用多點觸控的形式跟模型進行互動

首先,

  在MainCamera上新增 Physics Raycaster 

然後,

  在模型上新增EventTrigger

  在模型上新增指令碼ShiYe

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

public class ShiYe : MonoBehaviour {

    
// Use this for initialization void Start () { AddTriggersListener(this.gameObject, EventTriggerType.PointerEnter, logoSelectFunc); } private void logoSelectFunc(BaseEventData arg0) { Debug.Log("aaa"); } // Update is called once per frame void Update () { }
private void AddTriggersListener(GameObject obj, EventTriggerType eventID, UnityAction<BaseEventData> action) { EventTrigger trigger = obj.GetComponent<EventTrigger>(); if (trigger == null) { trigger = obj.AddComponent<EventTrigger>(); }
if (trigger.triggers.Count == 0) { trigger.triggers = new List<EventTrigger.Entry>(); } UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action); EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = eventID; entry.callback.AddListener(callback); trigger.triggers.Add(entry); } }

 

現在再拿滑鼠從模型上划過去 就會看到輸出aaa了