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