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