using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.Events; using UnityEngine.EventSystems; using System; namespace MoreMountains.Tools { /// /// Add this component to a UI rectangle and it'll act as a detection zone for a joystick. /// Note that this component extends the MMTouchJoystick class so you don't need to add another joystick to it. It's both the detection zone and the stick itself. /// [AddComponentMenu("More Mountains/Tools/Controls/MMTouchRepositionableJoystick")] public class MMTouchRepositionableJoystick : MMTouchJoystick, IPointerDownHandler { [Header("Dynamic Joystick")] public CanvasGroup KnobCanvasGroup; public CanvasGroup BackgroundCanvasGroup; protected Vector3 _initialPosition; protected Vector3 _newPosition; protected CanvasGroup _knobCanvasGroup; /// /// On Start, we instantiate our joystick's image if there's one /// protected override void Start() { base.Start(); // we store the detection zone's initial position _initialPosition = GetComponent().localPosition; } public override void Initialize() { base.Initialize(); SetKnobTransform(KnobCanvasGroup.transform); _canvasGroup = KnobCanvasGroup; _initialOpacity = _canvasGroup.alpha; } /// /// When the zone is pressed, we move our joystick accordingly /// /// Data. public virtual void OnPointerDown(PointerEventData data) { // if we're in "screen space - camera" render mode if (ParentCanvasRenderMode == RenderMode.ScreenSpaceCamera) { _newPosition = TargetCamera.ScreenToWorldPoint(data.position); } // otherwise else { _newPosition = data.position; } _newPosition.z = this.transform.position.z; // we define a new neutral position BackgroundCanvasGroup.transform.position = _newPosition; SetNeutralPosition(_newPosition); } /// /// When the player lets go of the stick, we restore our stick's position if needed /// /// Event data. public override void OnEndDrag(PointerEventData eventData) { base.OnEndDrag(eventData); } } }