-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneralHelpers.cs
67 lines (53 loc) · 1.99 KB
/
GeneralHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public static class GeneralHelpers
{
// Camera Reference:
// Store reference to camera so its only called once
private static Camera _camera;
public static Camera Camera
{
get
{
if (_camera == null) _camera = Camera.main;
return _camera;
}
}
// Non-Allocating WaitForSeconds
// Reduce garbage collection by reusing WaitForSeconds if same wait time exists
private static readonly Dictionary<float, WaitForSeconds> WaitDictionary = new Dictionary<float, WaitForSeconds>();
public static WaitForSeconds GetWait(float time)
{
if (WaitDictionary.TryGetValue(time, out var wait)) return wait;
WaitDictionary[time] = new WaitForSeconds(time);
return WaitDictionary[time];
}
// Is Pointer Over UI?
// Detects if cursor or touch is over any UI element
// example: _text.text = Helpers.IsOverUI() ? "Over UI" : "Not Over UI";
private static PointerEventData _eventDataCurrentPosition;
private static List<RaycastResult> _results;
public static bool IsOverUI()
{
_eventDataCurrentPosition = new PointerEventData(EventSystem.current)
{
position = Input.mousePosition
};
_results = new List<RaycastResult>();
EventSystem.current.RaycastAll(_eventDataCurrentPosition, _results);
return _results.Count > 0;
}
// Find World Point Of Canvas Element
// example: transform.position = Helpers.GetWorldPositionOfCanvasElement(target)
public static Vector2 GetWorldPosOfCanvasElement(RectTransform element)
{
RectTransformUtility.ScreenPointToWorldPointInRectangle(element, element.position, Camera, out var result);
return result;
}
// Destroy All Child Objects
public static void DeleteChildren(this Transform t)
{
foreach (Transform child in t) Object.Destroy(child.gameObject);
}
}