diff --git a/Examples/FloatingJoystick/FloatingJoystick.png b/Examples/FloatingJoystick/FloatingJoystick.png new file mode 100755 index 0000000..0a4df1d Binary files /dev/null and b/Examples/FloatingJoystick/FloatingJoystick.png differ diff --git a/Examples/FloatingJoystick/FloatingJoystick.png.meta b/Examples/FloatingJoystick/FloatingJoystick.png.meta new file mode 100644 index 0000000..0998ae3 --- /dev/null +++ b/Examples/FloatingJoystick/FloatingJoystick.png.meta @@ -0,0 +1,100 @@ +fileFormatVersion: 2 +guid: a11871a7396544de4950f82660bc5636 +timeCreated: 1492425391 +licenseType: Free +TextureImporter: + fileIDToRecycleName: {} + serializedVersion: 4 + mipmaps: + mipMapMode: 0 + enableMipMap: 1 + sRGBTexture: 0 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + filterMode: 1 + aniso: 16 + mipBias: -1 + wrapMode: -1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 1 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spritePixelsToUnits: 100 + alphaUsage: 1 + alphaIsTransparency: 0 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + platformSettings: + - buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + - buildTarget: Standalone + maxTextureSize: 2048 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + - buildTarget: iPhone + maxTextureSize: 2048 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + - buildTarget: Android + maxTextureSize: 2048 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + - buildTarget: WebGL + maxTextureSize: 2048 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + spritePackingTag: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/FloatingJoystick/FloatingJoystick.unity b/Examples/FloatingJoystick/FloatingJoystick.unity new file mode 100644 index 0000000..be2fa7b Binary files /dev/null and b/Examples/FloatingJoystick/FloatingJoystick.unity differ diff --git a/Examples/FloatingJoystick/FloatingJoystick.unity.meta b/Examples/FloatingJoystick/FloatingJoystick.unity.meta new file mode 100644 index 0000000..985f36e --- /dev/null +++ b/Examples/FloatingJoystick/FloatingJoystick.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2ae0e7a710bb34986a2cf96fc5918c33 +timeCreated: 1492422996 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/FloatingJoystick/FloatingJoystickTest.cs b/Examples/FloatingJoystick/FloatingJoystickTest.cs new file mode 100644 index 0000000..1b7672b --- /dev/null +++ b/Examples/FloatingJoystick/FloatingJoystickTest.cs @@ -0,0 +1,28 @@ +/// Credit Tima Zhum + +using UnityEngine; + +#if CROSS_PLATFORM_INPUT +using UnityStandardAssets.CrossPlatformInput; +#endif + +namespace UnityEngine.UI.Extensions +{ + public class FloatingJoystickTest: MonoBehaviour + { + #if CROSS_PLATFORM_INPUT + + /// + /// Update this instance + /// + void Update() + { + float _horizontalAxis = CrossPlatformInputManager.GetAxis("Horizontal"); + float _verticalAxis = CrossPlatformInputManager.GetAxis("Vertical"); + + transform.position += 5f * (Vector3.right * _horizontalAxis + Vector3.up * _verticalAxis); + } + + #endif + } +} diff --git a/Examples/FloatingJoystick/FloatingJoystickTest.cs.meta b/Examples/FloatingJoystick/FloatingJoystickTest.cs.meta new file mode 100644 index 0000000..c05e754 --- /dev/null +++ b/Examples/FloatingJoystick/FloatingJoystickTest.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: dfff1a99ac1fc4ce99fa98f9ab61f79c +timeCreated: 1492424357 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/FloatingJoystick/README.txt b/Examples/FloatingJoystick/README.txt new file mode 100644 index 0000000..424ddab --- /dev/null +++ b/Examples/FloatingJoystick/README.txt @@ -0,0 +1,10 @@ +Getting started with a floating joystick + +1. Open a FloatingJoystick scene +2. Import Unity Standard Assets/Cross Platfrom Input +- Assets -> Import Package -> CrossPlatformInput +3. Add scripting define symbols +- Edit -> Project Settings -> Player -> Other Settings -> Scripting Define symbols +- Add "CROSS_PLATFORM_INPUT;MOBILE_INPUT" (probably, there will be only "CROSS_PLATFORM_INPUT") +- Press Enter to submit changes (!important) +4. Play the scene \ No newline at end of file diff --git a/Scripts/Controls/FloatingJoystick/FloatingJoystick.cs b/Scripts/Controls/FloatingJoystick/FloatingJoystick.cs new file mode 100644 index 0000000..3510a28 --- /dev/null +++ b/Scripts/Controls/FloatingJoystick/FloatingJoystick.cs @@ -0,0 +1,246 @@ +/// Credit Tima Zhum +/// Based on Joystick.cs from Unity Standard Assets/Cross Platform Input + +using UnityEngine.EventSystems; + +#if CROSS_PLATFORM_INPUT +using UnityStandardAssets.CrossPlatformInput; +#endif + +namespace UnityEngine.UI.Extensions +{ + [RequireComponent(typeof(Image))] + [AddComponentMenu("Scripts/UnityEngine.UI.Extensions/FloatingJoystick")] + public class FloatingJoystick: MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler + { + /// + /// The joystick base point image + /// + [Tooltip("Pivot of a joystick (not required)")] + [SerializeField] private Image _joystickBasePoint = null; + + #if CROSS_PLATFORM_INPUT + + /// + /// The joystick image + /// + private Image _joystickImage = null; + + /// + /// The joystick enabled state + /// + private bool _joystickEnabled = false; + + /// + /// The previous joystick enabled state + /// + private bool _joystickPreviouslyEnabled = false; + + /// + /// The last point event data + /// + private PointerEventData _pedLast = null; + + /// + /// Start this instance + /// + protected void Awake() + { + _joystickImage = GetComponent(); + } + + /// + /// Update this instance + /// + protected void Update() + { + if (_joystickImage) _joystickImage.enabled = _joystickEnabled; + if (_joystickBasePoint) _joystickBasePoint.enabled = _joystickEnabled; + + if (_joystickEnabled) + { + if (!_joystickPreviouslyEnabled) + { + Vector3 _position = _pedLast.position; + ReinitializeAt(_position); + if (_joystickBasePoint) _joystickBasePoint.transform.position = _position; + } + } + + _joystickPreviouslyEnabled = _joystickEnabled; + } + + /// + /// Raises the pointer down event + /// + /// Ped + public void OnPointerDownHelper(PointerEventData _ped) + { + OnPointerDown(_ped); + _joystickEnabled = true; + _pedLast = _ped; + } + + /// + /// Raises the pointer up event + /// + /// Ped + public void OnPointerUpHelper(PointerEventData _ped) + { + OnPointerUp(_ped); + _joystickEnabled = false; + _pedLast = _ped; + } + + /// + /// Raises the drag event + /// + /// Ped + public void OnDragHelper(PointerEventData _ped) + { + OnDrag(_ped); + _pedLast = _ped; + } + + /// + /// Reinitializes the joystick at specific position + /// + /// Position + public void ReinitializeAt(Vector3 _position) + { + transform.position = _position; + Start(); + OnDisable(); + OnEnable(); + } + + #region STANDARDASSETSCODE + + /* + * This is a copy of the Joystick.cs script from Unity Standard Assets/Cross Platform Input + * The reason of copying: original script is not designed for inheriting (required for extending the functionalities) + */ + + public enum AxisOption + { + // Options for which axes to use + Both, // Use both + OnlyHorizontal, // Only horizontal + OnlyVertical // Only vertical + } + + private int InitialMovementRange; + public int MovementRange = 100; + public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use + public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input + public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input + + Vector3 m_StartPos; + bool m_UseX; // Toggle for using the x axis + bool m_UseY; // Toggle for using the Y axis + CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input + CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input + + + void OnEnable() + { + CreateVirtualAxes(); + } + + void Start() + { + m_StartPos = transform.position; + } + + void UpdateVirtualAxes(Vector3 value) + { + var delta = m_StartPos - value; + delta.y = -delta.y; + delta /= MovementRange; + if (m_UseX) + { + m_HorizontalVirtualAxis.Update(-delta.x); + } + + if (m_UseY) + { + m_VerticalVirtualAxis.Update(delta.y); + } + } + + void CreateVirtualAxes() + { + // set axes to use + m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal); + m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical); + + // create new axes based on axes to use + if (m_UseX) + { + m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName); + CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis); + } + if (m_UseY) + { + m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName); + CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis); + } + } + + + public void OnDrag(PointerEventData data) + { + Vector3 newPos = Vector3.zero; + + if (m_UseX) + { + int delta = (int)(data.position.x - m_StartPos.x); + delta = Mathf.Clamp(delta, - MovementRange, MovementRange); + newPos.x = delta; + } + + if (m_UseY) + { + int delta = (int)(data.position.y - m_StartPos.y); + delta = Mathf.Clamp(delta, -MovementRange, MovementRange); + newPos.y = delta; + } + + transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z); + UpdateVirtualAxes(transform.position); + } + + + public void OnPointerUp(PointerEventData data) + { + transform.position = m_StartPos; + UpdateVirtualAxes(m_StartPos); + } + + + public void OnPointerDown(PointerEventData data) { } + + void OnDisable() + { + // remove the joysticks from the cross platform input + if (m_UseX) + { + m_HorizontalVirtualAxis.Remove(); + } + if (m_UseY) + { + m_VerticalVirtualAxis.Remove(); + } + } + + #endregion + + #else + + public void OnDrag(PointerEventData data) {} + public void OnPointerUp(PointerEventData data) {} + public void OnPointerDown(PointerEventData data) {} + + #endif + } +} \ No newline at end of file diff --git a/Scripts/Controls/FloatingJoystick/FloatingJoystick.cs.meta b/Scripts/Controls/FloatingJoystick/FloatingJoystick.cs.meta new file mode 100644 index 0000000..e85b22a --- /dev/null +++ b/Scripts/Controls/FloatingJoystick/FloatingJoystick.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 54466c9d22a444d8d8e90e8d9ab83faa +timeCreated: 1492420430 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Controls/FloatingJoystick/FloatingJoystickArea.cs b/Scripts/Controls/FloatingJoystick/FloatingJoystickArea.cs new file mode 100644 index 0000000..864ac11 --- /dev/null +++ b/Scripts/Controls/FloatingJoystick/FloatingJoystickArea.cs @@ -0,0 +1,54 @@ +/// Credit Tima Zhum + +using UnityEngine.EventSystems; + +namespace UnityEngine.UI.Extensions +{ + [RequireComponent(typeof(Image))] + [AddComponentMenu("Scripts/UnityEngine.UI.Extensions/FloatingJoystickArea")] + public class FloatingJoystickArea: MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler + { + /// + /// The floating joystick controller + /// + [Tooltip("The floating joystick that should appear in the area on tap")] + [SerializeField] private FloatingJoystick _floatingJoystick = null; + + #if CROSS_PLATFORM_INPUT + + /// + /// Raises the pointer down event + /// + /// Ped + public void OnPointerDown(PointerEventData _ped) + { + if (_floatingJoystick) _floatingJoystick.OnPointerDownHelper(_ped); + } + + /// + /// Raises the pointer up event + /// + /// Ped + public void OnPointerUp(PointerEventData _ped) + { + if (_floatingJoystick) _floatingJoystick.OnPointerUpHelper(_ped); + } + + /// + /// Raises the drag event + /// + /// Ped + public void OnDrag(PointerEventData _ped) + { + if (_floatingJoystick) _floatingJoystick.OnDragHelper(_ped); + } + + #else + + public void OnDrag(PointerEventData data) {} + public void OnPointerUp(PointerEventData data) {} + public void OnPointerDown(PointerEventData data) {} + + #endif + } +} \ No newline at end of file diff --git a/Scripts/Controls/FloatingJoystick/FloatingJoystickArea.cs.meta b/Scripts/Controls/FloatingJoystick/FloatingJoystickArea.cs.meta new file mode 100644 index 0000000..a896f60 --- /dev/null +++ b/Scripts/Controls/FloatingJoystick/FloatingJoystickArea.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 4b85a1b054bce497cbe13cf45ea6d7fc +timeCreated: 1492420430 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: