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