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