Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Last active August 3, 2018 04:01
Show Gist options
  • Save smallrice45/bf1417ae9be1a176e8d2 to your computer and use it in GitHub Desktop.
Save smallrice45/bf1417ae9be1a176e8d2 to your computer and use it in GitHub Desktop.
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);
}
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment