Merged in floatingjoystick (pull request #8)
Added a floating joystick (appears in a tap position)release
commit
2fb1a35698
Binary file not shown.
After Width: | Height: | Size: 8.1 KiB |
|
@ -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:
|
Binary file not shown.
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2ae0e7a710bb34986a2cf96fc5918c33
|
||||
timeCreated: 1492422996
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Update this instance
|
||||
/// </summary>
|
||||
void Update()
|
||||
{
|
||||
float _horizontalAxis = CrossPlatformInputManager.GetAxis("Horizontal");
|
||||
float _verticalAxis = CrossPlatformInputManager.GetAxis("Vertical");
|
||||
|
||||
transform.position += 5f * (Vector3.right * _horizontalAxis + Vector3.up * _verticalAxis);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dfff1a99ac1fc4ce99fa98f9ab61f79c
|
||||
timeCreated: 1492424357
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// The joystick base point image
|
||||
/// </summary>
|
||||
[Tooltip("Pivot of a joystick (not required)")]
|
||||
[SerializeField] private Image _joystickBasePoint = null;
|
||||
|
||||
#if CROSS_PLATFORM_INPUT
|
||||
|
||||
/// <summary>
|
||||
/// The joystick image
|
||||
/// </summary>
|
||||
private Image _joystickImage = null;
|
||||
|
||||
/// <summary>
|
||||
/// The joystick enabled state
|
||||
/// </summary>
|
||||
private bool _joystickEnabled = false;
|
||||
|
||||
/// <summary>
|
||||
/// The previous joystick enabled state
|
||||
/// </summary>
|
||||
private bool _joystickPreviouslyEnabled = false;
|
||||
|
||||
/// <summary>
|
||||
/// The last point event data
|
||||
/// </summary>
|
||||
private PointerEventData _pedLast = null;
|
||||
|
||||
/// <summary>
|
||||
/// Start this instance
|
||||
/// </summary>
|
||||
protected void Awake()
|
||||
{
|
||||
_joystickImage = GetComponent<Image>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update this instance
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises the pointer down event
|
||||
/// </summary>
|
||||
/// <param name="_ped">Ped</param>
|
||||
public void OnPointerDownHelper(PointerEventData _ped)
|
||||
{
|
||||
OnPointerDown(_ped);
|
||||
_joystickEnabled = true;
|
||||
_pedLast = _ped;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises the pointer up event
|
||||
/// </summary>
|
||||
/// <param name="_ped">Ped</param>
|
||||
public void OnPointerUpHelper(PointerEventData _ped)
|
||||
{
|
||||
OnPointerUp(_ped);
|
||||
_joystickEnabled = false;
|
||||
_pedLast = _ped;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises the drag event
|
||||
/// </summary>
|
||||
/// <param name="_ped">Ped</param>
|
||||
public void OnDragHelper(PointerEventData _ped)
|
||||
{
|
||||
OnDrag(_ped);
|
||||
_pedLast = _ped;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reinitializes the joystick at specific position
|
||||
/// </summary>
|
||||
/// <param name="_position">Position</param>
|
||||
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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 54466c9d22a444d8d8e90e8d9ab83faa
|
||||
timeCreated: 1492420430
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// The floating joystick controller
|
||||
/// </summary>
|
||||
[Tooltip("The floating joystick that should appear in the area on tap")]
|
||||
[SerializeField] private FloatingJoystick _floatingJoystick = null;
|
||||
|
||||
#if CROSS_PLATFORM_INPUT
|
||||
|
||||
/// <summary>
|
||||
/// Raises the pointer down event
|
||||
/// </summary>
|
||||
/// <param name="_ped">Ped</param>
|
||||
public void OnPointerDown(PointerEventData _ped)
|
||||
{
|
||||
if (_floatingJoystick) _floatingJoystick.OnPointerDownHelper(_ped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises the pointer up event
|
||||
/// </summary>
|
||||
/// <param name="_ped">Ped</param>
|
||||
public void OnPointerUp(PointerEventData _ped)
|
||||
{
|
||||
if (_floatingJoystick) _floatingJoystick.OnPointerUpHelper(_ped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises the drag event
|
||||
/// </summary>
|
||||
/// <param name="_ped">Ped</param>
|
||||
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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4b85a1b054bce497cbe13cf45ea6d7fc
|
||||
timeCreated: 1492420430
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue