iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1091229 虛擬搖桿
by 黃國哲 2020-12-29 08:58:41, Reply(0), Views(693)


使用
https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631




EventTriggerListener
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
public delegate void VoidDelegate (GameObject go);
public delegate void VoidDelegateData (GameObject go, PointerEventData eventData);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;

public VoidDelegateData onDrag;
public VoidDelegate onBeginDrag;
public VoidDelegate onEndDrag;

public VoidDelegate onUpdateSelect;
static public EventTriggerListener Get (GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if(onClick != null) onClick(gameObject);
}
public override void OnPointerDown (PointerEventData eventData){
if(onDown != null) onDown(gameObject);
}
public override void OnPointerEnter (PointerEventData eventData){
if(onEnter != null) onEnter(gameObject);
}
public override void OnPointerExit (PointerEventData eventData){
if(onExit != null) onExit(gameObject);
}
public override void OnPointerUp (PointerEventData eventData){
if(onUp != null) onUp(gameObject);
}
public override void OnSelect (BaseEventData eventData){
if(onSelect != null) onSelect(gameObject);
}
public override void OnUpdateSelected (BaseEventData eventData){
if(onUpdateSelect != null) onUpdateSelect(gameObject);
}

public override void OnDrag (PointerEventData eventData){
if(onDrag != null) onDrag(gameObject,eventData);
}
public override void OnBeginDrag (PointerEventData eventData){
if(onBeginDrag != null) onBeginDrag(gameObject);
}
public override void OnEndDrag (PointerEventData eventData){
if(onEndDrag != null) onEndDrag(gameObject);
}
}
uGUIJoystick
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class uGUIJoystick : MonoBehaviour {
private RectTransform m_JoystickControoler;
private Vector3 startPos;
public float joystickX;
public float joystickY;
private float clampPos;
private float fixPos;
// Use this for initialization
void Start () {
m_JoystickControoler = transform.Find("Controller").GetComponent<RectTransform>();
startPos = m_JoystickControoler.position;

clampPos = GetComponent<RectTransform>().sizeDelta.x/2;
fixPos = 1/GetComponent<RectTransform>().sizeDelta.x*2;

EventTriggerListener.Get(m_JoystickControoler.gameObject).onDrag = OnDrag;
EventTriggerListener.Get(m_JoystickControoler.gameObject).onEndDrag = OnEndDrag;
}
// DragController
public void OnDrag (GameObject go,PointerEventData eventData) {
// Set ControllerPos by MousePos
m_JoystickControoler.position = eventData.position;
// Clamp ControllerPos(LocalPos)
m_JoystickControoler.localPosition = Vector3.ClampMagnitude( m_JoystickControoler.localPosition, clampPos);
joystickX = m_JoystickControoler.localPosition.x * fixPos;
joystickY = m_JoystickControoler.localPosition.y * fixPos;
//Debug.Log(eventData.pointerId);
}
// EndDragController
public void OnEndDrag (GameObject go) {
m_JoystickControoler.position = startPos;
joystickX = 0;
joystickY = 0;
}
}




Reply