Updated ColorSampler and TiltWindow with multi-touch support.

Also resolved an annoying warning from the BoxSlider (Resolves #234)
pull/413/head
Simon (Darkside) Jackson 2018-04-29 12:10:34 +01:00
parent 75bcf05f32
commit 4e6f14118f
4 changed files with 103 additions and 30 deletions

View File

@ -137,7 +137,7 @@ namespace UnityEngine.UI.Extensions
SetX(m_ValueX, false); SetX(m_ValueX, false);
SetY(m_ValueY, false); SetY(m_ValueY, false);
// Update rects since other things might affect them even if value didn't change. // Update rects since other things might affect them even if value didn't change.
UpdateVisuals(); if(!Application.isPlaying) UpdateVisuals();
var prefabType = UnityEditor.PrefabUtility.GetPrefabType(this); var prefabType = UnityEditor.PrefabUtility.GetPrefabType(this);
if (prefabType != UnityEditor.PrefabType.Prefab && !Application.isPlaying) if (prefabType != UnityEditor.PrefabType.Prefab && !Application.isPlaying)

View File

@ -1,3 +1,9 @@
/// Credit judah4
/// Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
/// Updated by SimonDarksideJ - Updated to use touch position rather than mouse for multi-touch
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions.ColorPicker namespace UnityEngine.UI.Extensions.ColorPicker
{ {
/// <summary> /// <summary>
@ -8,10 +14,13 @@ namespace UnityEngine.UI.Extensions.ColorPicker
/// ///
/// This does not work well with a world space UI as positioning is working with screen space. /// This does not work well with a world space UI as positioning is working with screen space.
/// </summary> /// </summary>
public class ColorSampler : MonoBehaviour public class ColorSampler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{ {
private Vector2 m_screenPos;
[SerializeField] [SerializeField]
protected Button sampler; protected Button sampler;
private RectTransform sampleRectTransform;
[SerializeField] [SerializeField]
protected Outline samplerOutline; protected Outline samplerOutline;
@ -25,6 +34,7 @@ namespace UnityEngine.UI.Extensions.ColorPicker
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
screenCapture = ScreenCapture.CaptureScreenshotAsTexture(); screenCapture = ScreenCapture.CaptureScreenshotAsTexture();
sampleRectTransform = sampler.GetComponent<RectTransform>();
sampler.gameObject.SetActive(true); sampler.gameObject.SetActive(true);
sampler.onClick.AddListener(SelectColor); sampler.onClick.AddListener(SelectColor);
} }
@ -41,8 +51,8 @@ namespace UnityEngine.UI.Extensions.ColorPicker
if (screenCapture == null) if (screenCapture == null)
return; return;
sampler.transform.position = Input.mousePosition; sampleRectTransform.position = m_screenPos;
color = screenCapture.GetPixel((int)Input.mousePosition.x, (int)Input.mousePosition.y); color = screenCapture.GetPixel((int)m_screenPos.x, (int)m_screenPos.y);
HandleSamplerColoring(); HandleSamplerColoring();
} }
@ -66,5 +76,20 @@ namespace UnityEngine.UI.Extensions.ColorPicker
enabled = false; enabled = false;
} }
public void OnPointerDown(PointerEventData eventData)
{
m_screenPos = eventData.position;
}
public void OnPointerUp(PointerEventData eventData)
{
m_screenPos = Vector2.zero;
}
public void OnDrag(PointerEventData eventData)
{
m_screenPos = eventData.position;
}
} }
} }

View File

@ -1,26 +1,29 @@
///Credit judah4 ///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/ ///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
public class TiltWindow : MonoBehaviour public class TiltWindow : MonoBehaviour, IDragHandler
{ {
public Vector2 range = new Vector2(5f, 3f); public Vector2 range = new Vector2(5f, 3f);
Transform mTrans; private Transform mTrans;
Quaternion mStart; private Quaternion mStart;
Vector2 mRot = Vector2.zero; private Vector2 mRot = Vector2.zero;
private Vector2 m_screenPos;
void Start ()
void Start()
{ {
mTrans = transform; mTrans = transform;
mStart = mTrans.localRotation; mStart = mTrans.localRotation;
} }
void Update () void Update()
{ {
Vector3 pos = Input.mousePosition; Vector3 pos = m_screenPos;
float halfWidth = Screen.width * 0.5f; float halfWidth = Screen.width * 0.5f;
float halfHeight = Screen.height * 0.5f; float halfHeight = Screen.height * 0.5f;
@ -30,5 +33,11 @@ namespace UnityEngine.UI.Extensions
mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f); mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
} }
}
public void OnDrag(PointerEventData eventData)
{
m_screenPos = eventData.position;
}
}
} }

View File

@ -22,5 +22,44 @@ namespace UnityEngine.UI.Extensions
return parentCanvas; return parentCanvas;
} }
public static Vector2 TransformInputBasedOnCanvasType(this Vector2 input, Canvas canvas)
{
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
return canvas.GetEventCamera().ScreenToWorldPoint(input);
}
else
{
return input;
}
}
public static Vector3 TransformInputBasedOnCanvasType(this Vector2 input, RectTransform rt)
{
var canvas = rt.GetParentCanvas();
if (input == Vector2.zero || canvas.renderMode == RenderMode.ScreenSpaceOverlay)
{
return input;
}
else
{
// Needs work :S
Vector2 movePos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(
rt,
input, canvas.GetEventCamera(),
out movePos);
Vector3 output = canvas.transform.TransformPoint(movePos);
return output;
}
}
public static Camera GetEventCamera(this Canvas input)
{
return input.worldCamera == null ? Camera.main : input.worldCamera;
}
} }
} }