2020-07-09 03:38:28 +08:00
|
|
|
|
/// Credit Simon (simonDarksideJ) Jackson
|
2017-06-22 07:54:02 +08:00
|
|
|
|
/// Sourced from - My head
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class UIExtensionMethods
|
|
|
|
|
{
|
|
|
|
|
public static Canvas GetParentCanvas(this RectTransform rt)
|
|
|
|
|
{
|
|
|
|
|
RectTransform parent = rt;
|
|
|
|
|
Canvas parentCanvas = rt.GetComponent<Canvas>();
|
|
|
|
|
|
|
|
|
|
int SearchIndex = 0;
|
|
|
|
|
while (parentCanvas == null || SearchIndex > 50)
|
|
|
|
|
{
|
|
|
|
|
parentCanvas = rt.GetComponentInParent<Canvas>();
|
|
|
|
|
if (parentCanvas == null)
|
|
|
|
|
{
|
2022-04-24 06:25:31 +08:00
|
|
|
|
if (parent.parent == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-06-22 07:54:02 +08:00
|
|
|
|
parent = parent.parent.GetComponent<RectTransform>();
|
|
|
|
|
SearchIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return parentCanvas;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 19:10:34 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
}
|
2017-06-22 07:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|