| | 1 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 2 | | #define WEB_PLATFORM |
| | 3 | | #endif |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using DCL.Configuration; |
| | 10 | | using Newtonsoft.Json; |
| | 11 | | using TMPro; |
| | 12 | | using UnityEngine; |
| | 13 | | using UnityEngine.Assertions; |
| | 14 | | using UnityEngine.EventSystems; |
| | 15 | | using UnityEngine.Networking; |
| | 16 | | using UnityEngine.UI; |
| | 17 | | using Object = UnityEngine.Object; |
| | 18 | |
|
| | 19 | | namespace DCL.Helpers |
| | 20 | | { |
| | 21 | | public static class Utils |
| | 22 | | { |
| | 23 | | public static Dictionary<string, Material> staticMaterials; |
| | 24 | |
|
| | 25 | | public static Material EnsureResourcesMaterial(string path) |
| | 26 | | { |
| 648 | 27 | | if (staticMaterials == null) |
| | 28 | | { |
| 1 | 29 | | staticMaterials = new Dictionary<string, Material>(); |
| | 30 | | } |
| | 31 | |
|
| 648 | 32 | | if (!staticMaterials.ContainsKey(path)) |
| | 33 | | { |
| 4 | 34 | | Material material = Resources.Load(path) as Material; |
| | 35 | |
|
| 4 | 36 | | if (material != null) |
| | 37 | | { |
| 4 | 38 | | staticMaterials.Add(path, material); |
| | 39 | | } |
| | 40 | |
|
| 4 | 41 | | return material; |
| | 42 | | } |
| | 43 | |
|
| 644 | 44 | | return staticMaterials[path]; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public static void CleanMaterials(Renderer r) |
| | 48 | | { |
| 52 | 49 | | if (r != null) |
| | 50 | | { |
| 208 | 51 | | foreach (Material m in r.materials) |
| | 52 | | { |
| 52 | 53 | | if (m != null) |
| | 54 | | { |
| 52 | 55 | | Material.Destroy(m); |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |
| 52 | 59 | | } |
| | 60 | |
|
| | 61 | | public static Vector2[] FloatArrayToV2List(float[] uvs) |
| | 62 | | { |
| 6 | 63 | | Vector2[] uvsResult = new Vector2[uvs.Length / 2]; |
| 6 | 64 | | int uvsResultIndex = 0; |
| | 65 | |
|
| 92 | 66 | | for (int i = 0; i < uvs.Length;) |
| | 67 | | { |
| 80 | 68 | | Vector2 tmpUv = Vector2.zero; |
| 80 | 69 | | tmpUv.x = uvs[i++]; |
| 80 | 70 | | tmpUv.y = uvs[i++]; |
| | 71 | |
|
| 80 | 72 | | uvsResult[uvsResultIndex++] = tmpUv; |
| | 73 | | } |
| | 74 | |
|
| 6 | 75 | | return uvsResult; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | public static void ResetLocalTRS(this Transform t) |
| | 79 | | { |
| 1699 | 80 | | t.localPosition = Vector3.zero; |
| 1699 | 81 | | t.localRotation = Quaternion.identity; |
| 1699 | 82 | | t.localScale = Vector3.one; |
| 1699 | 83 | | } |
| | 84 | |
|
| | 85 | | public static void SetToMaxStretch(this RectTransform t) |
| | 86 | | { |
| 198 | 87 | | t.anchorMin = Vector2.zero; |
| 198 | 88 | | t.offsetMin = Vector2.zero; |
| 198 | 89 | | t.anchorMax = Vector2.one; |
| 198 | 90 | | t.offsetMax = Vector2.one; |
| 198 | 91 | | t.sizeDelta = Vector2.zero; |
| 198 | 92 | | t.anchoredPosition = Vector2.zero; |
| 198 | 93 | | } |
| | 94 | |
|
| | 95 | | public static void SetToCentered(this RectTransform t) |
| | 96 | | { |
| 0 | 97 | | t.anchorMin = Vector2.one * 0.5f; |
| 0 | 98 | | t.offsetMin = Vector2.one * 0.5f; |
| 0 | 99 | | t.anchorMax = Vector2.one * 0.5f; |
| 0 | 100 | | t.offsetMax = Vector2.one * 0.5f; |
| 0 | 101 | | t.sizeDelta = Vector2.one * 100; |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public static void SetToBottomLeft(this RectTransform t) |
| | 105 | | { |
| 0 | 106 | | t.anchorMin = Vector2.zero; |
| 0 | 107 | | t.offsetMin = Vector2.zero; |
| 0 | 108 | | t.anchorMax = Vector2.zero; |
| 0 | 109 | | t.offsetMax = Vector2.zero; |
| 0 | 110 | | t.sizeDelta = Vector2.one * 100; |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public static void ForceUpdateLayout(this RectTransform rt, bool delayed = true) |
| | 114 | | { |
| 252 | 115 | | if (!rt.gameObject.activeInHierarchy) |
| 14 | 116 | | return; |
| | 117 | |
|
| 238 | 118 | | if (delayed) |
| 156 | 119 | | CoroutineStarter.Start(ForceUpdateLayoutRoutine(rt)); |
| | 120 | | else |
| | 121 | | { |
| 82 | 122 | | InverseTransformChildTraversal<RectTransform>( |
| 1884 | 123 | | (x) => { ForceRebuildLayoutImmediate(x); }, |
| | 124 | | rt); |
| | 125 | | } |
| 82 | 126 | | } |
| | 127 | |
|
| | 128 | | /// <summary> |
| | 129 | | /// Reimplementation of the LayoutRebuilder.ForceRebuildLayoutImmediate() function (Unity UI API) for make it mo |
| | 130 | | /// </summary> |
| | 131 | | /// <param name="rectTransformRoot">Root from which to rebuild.</param> |
| | 132 | | public static void ForceRebuildLayoutImmediate(RectTransform rectTransformRoot) |
| | 133 | | { |
| 5369 | 134 | | if (rectTransformRoot == null) |
| 3 | 135 | | return; |
| | 136 | |
|
| | 137 | | // NOTE(Santi): It seems to be very much cheaper to execute the next instructions manually than execute dire |
| | 138 | | // 'LayoutRebuilder.ForceRebuildLayoutImmediate()', that theorically already contains these ins |
| 5366 | 139 | | var layoutElements = rectTransformRoot.GetComponentsInChildren(typeof(ILayoutElement), true).ToList(); |
| 37438 | 140 | | layoutElements.RemoveAll(e => (e is Behaviour && !((Behaviour) e).isActiveAndEnabled) || e is TextMeshProUGU |
| 24238 | 141 | | foreach (var layoutElem in layoutElements) |
| | 142 | | { |
| 6753 | 143 | | (layoutElem as ILayoutElement).CalculateLayoutInputHorizontal(); |
| 6753 | 144 | | (layoutElem as ILayoutElement).CalculateLayoutInputVertical(); |
| | 145 | | } |
| | 146 | |
|
| 5366 | 147 | | var layoutControllers = rectTransformRoot.GetComponentsInChildren(typeof(ILayoutController), true).ToList(); |
| 11802 | 148 | | layoutControllers.RemoveAll(e => e is Behaviour && !((Behaviour) e).isActiveAndEnabled); |
| 16504 | 149 | | foreach (var layoutCtrl in layoutControllers) |
| | 150 | | { |
| 2886 | 151 | | (layoutCtrl as ILayoutController).SetLayoutHorizontal(); |
| 2886 | 152 | | (layoutCtrl as ILayoutController).SetLayoutVertical(); |
| | 153 | | } |
| 5366 | 154 | | } |
| | 155 | |
|
| | 156 | | private static IEnumerator ForceUpdateLayoutRoutine(RectTransform rt) |
| | 157 | | { |
| 156 | 158 | | yield return null; |
| | 159 | |
|
| 156 | 160 | | InverseTransformChildTraversal<RectTransform>( |
| 7148 | 161 | | (x) => { ForceRebuildLayoutImmediate(x); }, |
| | 162 | | rt); |
| 156 | 163 | | } |
| | 164 | |
|
| | 165 | | public static void InverseTransformChildTraversal<TComponent>(Action<TComponent> action, Transform startTransfor |
| | 166 | | where TComponent : Component |
| | 167 | | { |
| 8883 | 168 | | if (startTransform == null) |
| 97 | 169 | | return; |
| | 170 | |
|
| 34230 | 171 | | foreach (Transform t in startTransform) |
| | 172 | | { |
| 8329 | 173 | | InverseTransformChildTraversal(action, t); |
| | 174 | | } |
| | 175 | |
|
| 8786 | 176 | | var component = startTransform.GetComponent<TComponent>(); |
| | 177 | |
|
| 8786 | 178 | | if (component != null) |
| | 179 | | { |
| 5441 | 180 | | action.Invoke(component); |
| | 181 | | } |
| 8786 | 182 | | } |
| | 183 | |
|
| | 184 | | public static void ForwardTransformChildTraversal<TComponent>(Func<TComponent, bool> action, Transform startTran |
| | 185 | | where TComponent : Component |
| | 186 | | { |
| 0 | 187 | | Assert.IsTrue(startTransform != null, "startTransform must not be null"); |
| | 188 | |
|
| 0 | 189 | | var component = startTransform.GetComponent<TComponent>(); |
| | 190 | |
|
| 0 | 191 | | if (component != null) |
| | 192 | | { |
| 0 | 193 | | if (!action.Invoke(component)) |
| 0 | 194 | | return; |
| | 195 | | } |
| | 196 | |
|
| 0 | 197 | | foreach (Transform t in startTransform) |
| | 198 | | { |
| 0 | 199 | | ForwardTransformChildTraversal(action, t); |
| | 200 | | } |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | public static T GetOrCreateComponent<T>(this GameObject gameObject) where T : Component |
| | 204 | | { |
| 88 | 205 | | T component = gameObject.GetComponent<T>(); |
| | 206 | |
|
| 88 | 207 | | if (!component) |
| | 208 | | { |
| 76 | 209 | | return gameObject.AddComponent<T>(); |
| | 210 | | } |
| | 211 | |
|
| 12 | 212 | | return component; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | public static WebRequestAsyncOperation FetchTexture(string textureURL, bool isReadable, Action<Texture2D> OnSucc |
| | 216 | | { |
| | 217 | | //NOTE(Brian): This closure is called when the download is a success. |
| 0 | 218 | | void SuccessInternal(IWebRequestAsyncOperation request) { OnSuccess?.Invoke(DownloadHandlerTexture.GetConten |
| | 219 | |
|
| 0 | 220 | | var asyncOp = Environment.i.platform.webRequest.GetTexture( |
| | 221 | | url: textureURL, |
| | 222 | | OnSuccess: SuccessInternal, |
| | 223 | | OnFail: OnFail, |
| | 224 | | isReadable: isReadable); |
| | 225 | |
|
| 0 | 226 | | return asyncOp; |
| | 227 | | } |
| | 228 | |
|
| | 229 | | public static bool SafeFromJsonOverwrite(string json, object objectToOverwrite) |
| | 230 | | { |
| | 231 | | try |
| | 232 | | { |
| 0 | 233 | | JsonUtility.FromJsonOverwrite(json, objectToOverwrite); |
| 0 | 234 | | } |
| 0 | 235 | | catch (ArgumentException e) |
| | 236 | | { |
| 0 | 237 | | Debug.LogError("ArgumentException Fail!... Json = " + json + " " + e.ToString()); |
| 0 | 238 | | return false; |
| | 239 | | } |
| | 240 | |
|
| 0 | 241 | | return true; |
| 0 | 242 | | } |
| | 243 | |
|
| 45 | 244 | | public static T FromJsonWithNulls<T>(string json) { return JsonConvert.DeserializeObject<T>(json); } |
| | 245 | |
|
| | 246 | | public static T SafeFromJson<T>(string json) |
| | 247 | | { |
| 840 | 248 | | ProfilingEvents.OnMessageDecodeStart?.Invoke("Misc"); |
| | 249 | |
|
| 840 | 250 | | T returningValue = default(T); |
| | 251 | |
|
| 840 | 252 | | if (!string.IsNullOrEmpty(json)) |
| | 253 | | { |
| | 254 | | try |
| | 255 | | { |
| 839 | 256 | | returningValue = JsonUtility.FromJson<T>(json); |
| 839 | 257 | | } |
| 0 | 258 | | catch (ArgumentException e) |
| | 259 | | { |
| 0 | 260 | | Debug.LogError("ArgumentException Fail!... Json = " + json + " " + e.ToString()); |
| 0 | 261 | | } |
| | 262 | | } |
| | 263 | |
|
| 840 | 264 | | ProfilingEvents.OnMessageDecodeEnds?.Invoke("Misc"); |
| | 265 | |
|
| 840 | 266 | | return returningValue; |
| | 267 | | } |
| | 268 | |
|
| | 269 | | public static GameObject AttachPlaceholderRendererGameObject(Transform targetTransform) |
| | 270 | | { |
| 1 | 271 | | var placeholderRenderer = GameObject.CreatePrimitive(PrimitiveType.Cube).GetComponent<MeshRenderer>(); |
| | 272 | |
|
| 1 | 273 | | placeholderRenderer.material = Resources.Load<Material>("Materials/AssetLoading"); |
| 1 | 274 | | placeholderRenderer.transform.SetParent(targetTransform); |
| 1 | 275 | | placeholderRenderer.transform.localPosition = Vector3.zero; |
| 1 | 276 | | placeholderRenderer.name = "PlaceholderRenderer"; |
| | 277 | |
|
| 1 | 278 | | return placeholderRenderer.gameObject; |
| | 279 | | } |
| | 280 | |
|
| | 281 | | public static void SafeDestroy(Object obj) |
| | 282 | | { |
| 180 | 283 | | if (obj is Transform) |
| 0 | 284 | | return; |
| | 285 | |
|
| | 286 | | #if UNITY_EDITOR |
| 180 | 287 | | if (Application.isPlaying) |
| 180 | 288 | | Object.Destroy(obj); |
| | 289 | | else |
| 0 | 290 | | Object.DestroyImmediate(obj, false); |
| | 291 | | #else |
| | 292 | | UnityEngine.Object.Destroy(obj); |
| | 293 | | #endif |
| 0 | 294 | | } |
| | 295 | |
|
| | 296 | | /** |
| | 297 | | * Transforms a grid position into a world-relative 3d position |
| | 298 | | */ |
| | 299 | | public static Vector3 GridToWorldPosition(float xGridPosition, float yGridPosition) |
| | 300 | | { |
| 990 | 301 | | return new Vector3( |
| | 302 | | x: xGridPosition * ParcelSettings.PARCEL_SIZE, |
| | 303 | | y: 0f, |
| | 304 | | z: yGridPosition * ParcelSettings.PARCEL_SIZE |
| | 305 | | ); |
| | 306 | | } |
| | 307 | |
|
| | 308 | | /** |
| | 309 | | * Transforms a world position into a grid position |
| | 310 | | */ |
| | 311 | | public static Vector2Int WorldToGridPosition(Vector3 worldPosition) |
| | 312 | | { |
| 5684 | 313 | | return new Vector2Int( |
| | 314 | | (int) Mathf.Floor(worldPosition.x / ParcelSettings.PARCEL_SIZE), |
| | 315 | | (int) Mathf.Floor(worldPosition.z / ParcelSettings.PARCEL_SIZE) |
| | 316 | | ); |
| | 317 | | } |
| | 318 | |
|
| | 319 | | public static Vector2 WorldToGridPositionUnclamped(Vector3 worldPosition) |
| | 320 | | { |
| 0 | 321 | | return new Vector2( |
| | 322 | | worldPosition.x / ParcelSettings.PARCEL_SIZE, |
| | 323 | | worldPosition.z / ParcelSettings.PARCEL_SIZE |
| | 324 | | ); |
| | 325 | | } |
| | 326 | |
|
| | 327 | | public static bool AproxComparison(this Color color1, Color color2, float tolerance = 0.01f) // tolerance of rou |
| | 328 | | { |
| 3517 | 329 | | if (Mathf.Abs(color1.r - color2.r) < tolerance |
| | 330 | | && Mathf.Abs(color1.g - color2.g) < tolerance |
| | 331 | | && Mathf.Abs(color1.b - color2.b) < tolerance) |
| | 332 | | { |
| 340 | 333 | | return true; |
| | 334 | | } |
| | 335 | |
|
| 3177 | 336 | | return false; |
| | 337 | | } |
| | 338 | |
|
| 5 | 339 | | public static T ParseJsonArray<T>(string jsonArray) where T : IEnumerable => DummyJsonUtilityFromArray<T>.GetFro |
| | 340 | |
|
| | 341 | | [Serializable] |
| | 342 | | private class DummyJsonUtilityFromArray<T> where T : IEnumerable //UnityEngine.JsonUtility is really fast but ca |
| | 343 | | { |
| | 344 | | [SerializeField] |
| | 345 | | private T value; |
| | 346 | |
|
| | 347 | | public static T GetFromJsonArray(string jsonArray) |
| | 348 | | { |
| | 349 | | string newJson = $"{{ \"value\": {jsonArray}}}"; |
| | 350 | | return JsonUtility.FromJson<DummyJsonUtilityFromArray<T>>(newJson).value; |
| | 351 | | } |
| | 352 | | } |
| | 353 | |
|
| 1 | 354 | | private static int lockedInFrame = -1; |
| 0 | 355 | | public static bool LockedThisFrame() => lockedInFrame == Time.frameCount; |
| | 356 | |
|
| | 357 | | private static bool isCursorLocked; |
| | 358 | | //NOTE(Brian): Made as an independent flag because the CI doesn't work well with the Cursor.lockState check. |
| | 359 | | public static bool IsCursorLocked |
| | 360 | | { |
| 2199 | 361 | | get => isCursorLocked; |
| | 362 | | private set |
| | 363 | | { |
| 314 | 364 | | if (isCursorLocked == value) return; |
| 88 | 365 | | isCursorLocked = value; |
| 88 | 366 | | OnCursorLockChanged?.Invoke(isCursorLocked); |
| 81 | 367 | | } |
| | 368 | | } |
| | 369 | |
|
| | 370 | | public static event Action<bool> OnCursorLockChanged; |
| | 371 | |
|
| | 372 | | public static void LockCursor() |
| | 373 | | { |
| | 374 | | #if WEB_PLATFORM |
| | 375 | | //TODO(Brian): Encapsulate all this mechanism to a new MouseLockController and branch |
| | 376 | | // behaviour using strategy pattern instead of this. |
| | 377 | | if (IsCursorLocked) |
| | 378 | | { |
| | 379 | | return; |
| | 380 | | } |
| | 381 | | #endif |
| 44 | 382 | | Cursor.visible = false; |
| 44 | 383 | | IsCursorLocked = true; |
| 44 | 384 | | Cursor.lockState = CursorLockMode.Locked; |
| 44 | 385 | | lockedInFrame = Time.frameCount; |
| | 386 | |
|
| 44 | 387 | | EventSystem.current?.SetSelectedGameObject(null); |
| 34 | 388 | | } |
| | 389 | |
|
| | 390 | | public static void UnlockCursor() |
| | 391 | | { |
| | 392 | | #if WEB_PLATFORM |
| | 393 | | //TODO(Brian): Encapsulate all this mechanism to a new MouseLockController and branch |
| | 394 | | // behaviour using strategy pattern instead of this. |
| | 395 | | if (!IsCursorLocked) |
| | 396 | | { |
| | 397 | | return; |
| | 398 | | } |
| | 399 | | #endif |
| 157 | 400 | | Cursor.visible = true; |
| 157 | 401 | | IsCursorLocked = false; |
| 157 | 402 | | Cursor.lockState = CursorLockMode.None; |
| | 403 | |
|
| 157 | 404 | | EventSystem.current?.SetSelectedGameObject(null); |
| 71 | 405 | | } |
| | 406 | |
|
| | 407 | | #region BROWSER_ONLY |
| | 408 | |
|
| | 409 | | //TODO(Brian): Encapsulate all this mechanism to a new MouseLockController and branch |
| | 410 | | // behaviour using strategy pattern instead of this. |
| | 411 | | // NOTE: This should come from browser's pointerlockchange callback |
| | 412 | | public static void BrowserSetCursorState(bool locked) |
| | 413 | | { |
| 0 | 414 | | Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; |
| | 415 | |
|
| 0 | 416 | | IsCursorLocked = locked; |
| 0 | 417 | | Cursor.visible = !locked; |
| 0 | 418 | | } |
| | 419 | |
|
| | 420 | | #endregion |
| | 421 | |
|
| | 422 | | public static void DestroyAllChild(this Transform transform) |
| | 423 | | { |
| 82 | 424 | | foreach (Transform child in transform) |
| | 425 | | { |
| 3 | 426 | | Object.Destroy(child.gameObject); |
| | 427 | | } |
| 38 | 428 | | } |
| | 429 | |
|
| | 430 | | public static List<Vector2Int> GetBottomLeftZoneArray(Vector2Int bottomLeftAnchor, Vector2Int size) |
| | 431 | | { |
| 0 | 432 | | List<Vector2Int> coords = new List<Vector2Int>(); |
| | 433 | |
|
| 0 | 434 | | for (int x = bottomLeftAnchor.x; x < bottomLeftAnchor.x + size.x; x++) |
| | 435 | | { |
| 0 | 436 | | for (int y = bottomLeftAnchor.y; y < bottomLeftAnchor.y + size.y; y++) |
| | 437 | | { |
| 0 | 438 | | coords.Add(new Vector2Int(x, y)); |
| | 439 | | } |
| | 440 | | } |
| | 441 | |
|
| 0 | 442 | | return coords; |
| | 443 | | } |
| | 444 | |
|
| | 445 | | public static List<Vector2Int> GetCenteredZoneArray(Vector2Int center, Vector2Int size) |
| | 446 | | { |
| 0 | 447 | | List<Vector2Int> coords = new List<Vector2Int>(); |
| | 448 | |
|
| 0 | 449 | | for (int x = center.x - size.x; x < center.x + size.x; x++) |
| | 450 | | { |
| 0 | 451 | | for (int y = center.y - size.y; y < center.y + size.y; y++) |
| | 452 | | { |
| 0 | 453 | | coords.Add(new Vector2Int(x, y)); |
| | 454 | | } |
| | 455 | | } |
| | 456 | |
|
| 0 | 457 | | return coords; |
| | 458 | | } |
| | 459 | |
|
| | 460 | | public static void DrawRectGizmo(Rect rect, Color color, float duration) |
| | 461 | | { |
| 0 | 462 | | Vector3 tl2 = new Vector3(rect.xMin, rect.yMax, 0); |
| 0 | 463 | | Vector3 bl2 = new Vector3(rect.xMin, rect.yMin, 0); |
| 0 | 464 | | Vector3 tr2 = new Vector3(rect.xMax, rect.yMax, 0); |
| 0 | 465 | | Vector3 br2 = new Vector3(rect.xMax, rect.yMin, 0); |
| | 466 | |
|
| 0 | 467 | | Debug.DrawLine(tl2, bl2, color, duration); |
| 0 | 468 | | Debug.DrawLine(tl2, tr2, color, duration); |
| 0 | 469 | | Debug.DrawLine(bl2, br2, color, duration); |
| 0 | 470 | | Debug.DrawLine(tr2, br2, color, duration); |
| 0 | 471 | | } |
| | 472 | |
|
| | 473 | | public static string ToUpperFirst(this string value) |
| | 474 | | { |
| 0 | 475 | | if (!string.IsNullOrEmpty(value)) |
| | 476 | | { |
| 0 | 477 | | var capital = char.ToUpper(value[0]); |
| 0 | 478 | | value = capital + value.Substring(1); |
| | 479 | | } |
| | 480 | |
|
| 0 | 481 | | return value; |
| | 482 | | } |
| | 483 | |
|
| | 484 | | public static Vector3 Sanitize(Vector3 value) |
| | 485 | | { |
| 266 | 486 | | float x = float.IsInfinity(value.x) ? 0 : value.x; |
| 266 | 487 | | float y = float.IsInfinity(value.y) ? 0 : value.y; |
| 266 | 488 | | float z = float.IsInfinity(value.z) ? 0 : value.z; |
| | 489 | |
|
| 266 | 490 | | return new Vector3(x, y, z); |
| | 491 | | } |
| | 492 | |
|
| 0 | 493 | | public static bool CompareFloats( float a, float b, float precision = 0.1f ) { return Mathf.Abs(a - b) < precisi |
| | 494 | |
|
| | 495 | | public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value) |
| | 496 | | { |
| 93 | 497 | | key = tuple.Key; |
| 93 | 498 | | value = tuple.Value; |
| 93 | 499 | | } |
| | 500 | |
|
| | 501 | | /// <summary> |
| | 502 | | /// Set a layer to the given transform and its child |
| | 503 | | /// </summary> |
| | 504 | | /// <param name="transform"></param> |
| | 505 | | public static void SetLayerRecursively(Transform transform, int layer) |
| | 506 | | { |
| 621 | 507 | | transform.gameObject.layer = layer; |
| 2302 | 508 | | foreach (Transform child in transform) |
| | 509 | | { |
| 530 | 510 | | SetLayerRecursively(child, layer); |
| | 511 | | } |
| 621 | 512 | | } |
| | 513 | |
|
| | 514 | | /// <summary> |
| | 515 | | /// Converts a linear float (between 0 and 1) into an exponential curve fitting for audio volume. |
| | 516 | | /// </summary> |
| | 517 | | /// <param name="volume">Linear volume float</param> |
| | 518 | | /// <returns>Exponential volume curve float</returns> |
| 0 | 519 | | public static float ToVolumeCurve(float volume) { return volume * (2f - volume); } |
| | 520 | |
|
| | 521 | | /// <summary> |
| | 522 | | /// Takes a linear volume value between 0 and 1, converts to exponential curve and maps to a value fitting for a |
| | 523 | | /// </summary> |
| | 524 | | /// <param name="volume">Linear volume (0 to 1)</param> |
| | 525 | | /// <returns>Value for audio mixer group volume</returns> |
| 0 | 526 | | public static float ToAudioMixerGroupVolume(float volume) { return (ToVolumeCurve(volume) * 80f) - 80f; } |
| | 527 | |
|
| | 528 | | public static IEnumerator Wait(float delay, Action onFinishCallback) |
| | 529 | | { |
| 4 | 530 | | yield return new WaitForSeconds(delay); |
| 0 | 531 | | onFinishCallback.Invoke(); |
| 0 | 532 | | } |
| | 533 | |
|
| | 534 | | public static string GetHierarchyPath(this Transform transform) |
| | 535 | | { |
| 0 | 536 | | if (transform.parent == null) |
| 0 | 537 | | return transform.name; |
| 0 | 538 | | return $"{transform.parent.GetHierarchyPath()}/{transform.name}"; |
| | 539 | | } |
| | 540 | |
|
| | 541 | | public static bool TryFindChildRecursively(this Transform transform, string name, out Transform foundChild) |
| | 542 | | { |
| 0 | 543 | | foundChild = transform.Find(name); |
| 0 | 544 | | if (foundChild != null) |
| 0 | 545 | | return true; |
| | 546 | |
|
| 0 | 547 | | foreach (Transform child in transform) |
| | 548 | | { |
| 0 | 549 | | if (TryFindChildRecursively(child, name, out foundChild)) |
| 0 | 550 | | return true; |
| | 551 | | } |
| 0 | 552 | | return false; |
| 0 | 553 | | } |
| | 554 | |
|
| | 555 | | public static bool IsPointerOverUIElement(Vector3 mousePosition) |
| | 556 | | { |
| 0 | 557 | | var eventData = new PointerEventData(EventSystem.current); |
| 0 | 558 | | eventData.position = mousePosition; |
| 0 | 559 | | var results = new List<RaycastResult>(); |
| 0 | 560 | | EventSystem.current.RaycastAll(eventData, results); |
| 0 | 561 | | return results.Count > 1; |
| | 562 | | } |
| | 563 | |
|
| 0 | 564 | | public static bool IsPointerOverUIElement() { return IsPointerOverUIElement(Input.mousePosition); } |
| | 565 | | } |
| | 566 | | } |