unity 手指旋轉(zhuǎn)角色的功能
1. 需求說明
1.1 可以用手指進行旋轉(zhuǎn)。
1.2 點擊角色時可以觸發(fā)事件
2. Unity 鼠標點擊3D物體的方法
2.1 第一種 OnMouse
在需要檢測的物體上 首先物體要有Collider 添加腳本 使用OnMouse系列方法(腳本的enable不影響OnMouse的調(diào)用)
//當鼠標懸停在網(wǎng)格上時敢茁,
void OnMouseEnter()
{
}
// ...當鼠標懸浮在物體上
void OnMouseOver()
{
}
// ...當鼠標移開時
void OnMouseExit()
{
}
// ...當鼠標點擊
void OnMouseDown()
{
}
// ...當鼠標抬起
void OnMouseUp()
{
}
2.2 第二種方法 射線檢測(首先物體要有Collider 然后掛載腳本)
using UnityEngine;
using System.Collections;
public class RayCastHitControl : MonoBehaviour
{
Ray ray;
RaycastHit hit;
GameObject obj;
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("BeiJiuChuan"))
{
Debug.Log("點中"+obj.name);
}
//通過標簽
if (obj.tag == "move")
{
Debug.Log("點中" + obj.name);
}
}
}
}
}
2.3 第三種方法 游戲物體 EventTrigger 物體動態(tài)事件監(jiān)聽
先說一下 有哪些封裝好的公共方法
OnBeginDrag //在拖動開始之前調(diào)用稍刀。
OnCancel //發(fā)生Cancel事件時由EventSystem調(diào)用快骗。
OnDeselect //在選擇新對象時由EventSystem調(diào)用。
ondragDown //每次在拖動期間移動指針時由EventSystem調(diào)用。
OnDrop //當對象接受丟棄時由EventSystem調(diào)用唁影。
OnEndDrag //拖動結(jié)束后由EventSystem調(diào)用耕陷。
OnInitializePotentialDrag //在找到拖動時,但在有效開始拖動之前由EventSystem調(diào)用据沈。
OnMove //發(fā)生Move事件時由EventSystem調(diào)用哟沫。
OnPointerClick //發(fā)生Click事件時由EventSystem調(diào)用。
OnPointerDown //發(fā)生PointerDown事件時由EventSystem調(diào)用锌介。
OnPointerEnter //當指針進入與此EventTrigger關(guān)聯(lián)的對象時由EventSystem調(diào)用嗜诀。
OnPointerExit //當指針退出與此EventTrigger關(guān)聯(lián)的對象時,由EventSystem調(diào)用孔祸。
OnPointerUp //發(fā)生PointerUp事件時由EventSystem調(diào)用隆敢。
OnScroll //當Scroll事件發(fā)生時由EventSystem調(diào)用。
ONSELECT //當Select事件發(fā)生時由EventSystem調(diào)用崔慧。
OnUpdateSelected //更新與此EventTrigger關(guān)聯(lián)的對象時由EventSystem調(diào)用拂蝎。
然后說一下使用方法
1.通過編輯器中添加
在Inspector中添加EventTrigger的componnent組件,然后點擊Add New Event Type按鈕添加一個基本的事件
當我們選擇一個事件的時候惶室,就相當于將要于此事件綁定了温自,例如當我們選擇Pointer Click的時候,綁定了這個組件的物體將接受Pointer Click 事件的消息皇钞,當我們在物體上按下鼠標的時候悼泌,(當然空間中的物體我們是無法通過鼠標點擊的,但是當我們可以通過射線碰撞的方法鹅士,來實現(xiàn)Pointer Enter券躁,Pointer Exit等事件的觸發(fā))這里我們用來綁定這個EventTrigger 的是一個UI Panel 這樣便可以讓它接受我們自定義的單擊事件了
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.Events;
public class OnClickMouseDown : MonoBehaviour
{
void Start()
{
//獲取或添加EventTrigger組件
EventTrigger trigger = transform.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = transform.gameObject.AddComponent<EventTrigger>();
}
//初始化EventTrigger.Entry的數(shù)組
trigger.triggers = new List<EventTrigger.Entry>();
//創(chuàng)建各種 EventTrigger.Entry的類型
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;//設(shè)置Entry的eventID類型 即EventTriggerType的各種枚舉(比如鼠標點擊,滑動掉盅,拖動等)
UnityAction<BaseEventData> callback =new UnityAction<BaseEventData>(OnEventTrigger); //注冊代理
entry.callback.AddListener(callback);//添加代理事件到EventTrigger.Entry
EventTrigger.Entry entry2 = new EventTrigger.Entry();
entry2.eventID = EventTriggerType.PointerDown;
UnityAction<BaseEventData> callback1 =new UnityAction<BaseEventData>(OnMouseDown);
entry2.callback.AddListener(callback1);
//在EventTrigger.Entry的數(shù)組添加EventTrigger.Entry
trigger.triggers.Add(entry);
trigger.triggers.Add(entry2);
}
private void OnMouseDown(BaseEventData arg0)
{
Debug.Log("OnMouseDown");
}
private void OnEventTrigger(BaseEventData arg0)
{
Debug.Log("OnEventTrigger");
}
}
最后 別忘了 在攝像機下添加一個Physics Raycaster的組件也拜,用來檢測鼠標射線
3. 具體實現(xiàn)
考慮到以后的擴展,我們使用第三種方式來實現(xiàn)具體的功能趾痘。
using System;
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
namespace Tuyoo
{
public class MoveTarget : EventTrigger
{
//對外提供的點擊回調(diào)
public Action clickAct = null;
public bool isClick = false;
void Start()
{
EventTriggerListener.GetListener(gameObject).onDrag += onDrag;
EventTriggerListener.GetListener(gameObject).onBeginDrag += onBeginDrag;
EventTriggerListener.GetListener(gameObject).onPointerClick += onPointerClick;
EventTriggerListener.GetListener(gameObject).onPointerUp += onPointerUp;
}
private float beginfloat = 0;
public void onBeginDrag(GameObject go, PointerEventData eventData)
{
beginfloat = eventData.position.x;
}
public void onDrag(GameObject go, PointerEventData eventData)
{
isClick = false;
transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles.x, transform.localRotation.eulerAngles.y - (eventData.position.x - beginfloat), transform.localRotation.eulerAngles.z);
beginfloat = eventData.position.x;
}
public void onPointerUp(GameObject go, PointerEventData eventData)
{
// 點擊容錯處理慢哈,只有在0.1秒內(nèi),松開手指并且沒有移動事件觸發(fā)永票,才算點擊卵贱。
StartCoroutine(delayTime(0.1f));
}
private IEnumerator delayTime(float numtimes)
{
yield return WaitForTimeMgr.Instance.GetWaitForSeconds(numtimes);
isClick = true;
}
public void onPointerClick(GameObject go, PointerEventData eventData)
{
if (clickAct != null && isClick)
{
clickAct();
}
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : EventTrigger
{
#region 變量
//帶參數(shù)是為了方便取得綁定了UI事件的對象
public delegate void PointerEventDelegate(GameObject go, PointerEventData eventData);
public delegate void BaseEventDelegate(GameObject go, BaseEventData eventData);
public delegate void AxisEventDelegate(GameObject go, AxisEventData eventData);
public event PointerEventDelegate onPointerEnter;
public event PointerEventDelegate onPointerExit;
public event PointerEventDelegate onPointerDown;
public event PointerEventDelegate onPointerUp;
public event PointerEventDelegate onPointerClick;
public event PointerEventDelegate onInitializePotentialDrag;
public event PointerEventDelegate onBeginDrag;
public event PointerEventDelegate onDrag;
public event PointerEventDelegate onEndDrag;
public event PointerEventDelegate onDrop;
public event PointerEventDelegate onScroll;
public event BaseEventDelegate onUpdateSelected;
public event BaseEventDelegate onSelect;
public event BaseEventDelegate onDeselect;
public event AxisEventDelegate onMove;
public event BaseEventDelegate onSubmit;
public event BaseEventDelegate onCancel;
#endregion
public static EventTriggerListener GetListener(GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
#region 方法
public override void OnPointerEnter(PointerEventData eventData)
{
if (onPointerEnter != null) onPointerEnter(gameObject, eventData);
}
public override void OnPointerExit(PointerEventData eventData)
{
if (onPointerExit != null) onPointerExit(gameObject, eventData);
}
public override void OnPointerDown(PointerEventData eventData)
{
if (onPointerDown != null) onPointerDown(gameObject, eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
if (onPointerUp != null) onPointerUp(gameObject, eventData);
}
public override void OnPointerClick(PointerEventData eventData)
{
if (onPointerClick != null) onPointerClick(gameObject, eventData);
}
public override void OnInitializePotentialDrag(PointerEventData eventData)
{
if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject, eventData);
}
public override void OnBeginDrag(PointerEventData eventData)
{
if (onBeginDrag != null) onBeginDrag(gameObject, eventData);
}
public override void OnDrag(PointerEventData eventData)
{
if (onDrag != null) onDrag(gameObject, eventData);
}
public override void OnEndDrag(PointerEventData eventData)
{
if (onEndDrag != null) onEndDrag(gameObject, eventData);
}
public override void OnDrop(PointerEventData eventData)
{
if (onDrop != null) onDrop(gameObject, eventData);
}
public override void OnScroll(PointerEventData eventData)
{
if (onScroll != null) onScroll(gameObject, eventData);
}
public override void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelected != null) onUpdateSelected(gameObject, eventData);
}
public override void OnSelect(BaseEventData eventData)
{
if (onSelect != null) onSelect(gameObject, eventData);
}
public override void OnDeselect(BaseEventData eventData)
{
if (onDeselect != null) onDeselect(gameObject, eventData);
}
public override void OnMove(AxisEventData eventData)
{
if (onMove != null) onMove(gameObject, eventData);
}
public override void OnSubmit(BaseEventData eventData)
{
if (onSubmit != null) onSubmit(gameObject, eventData);
}
public override void OnCancel(BaseEventData eventData)
{
if (onCancel != null) onCancel(gameObject, eventData);
}
#endregion
}
將腳本掛到要旋轉(zhuǎn)的物體上就可以進行旋轉(zhuǎn)了。
效果如下圖: