using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UniRx; namespace KairoEngine.Utility { public class Utilities { public const int sortingOrderDefault = 5000; // Create Text in the World public static TextMesh CreateWorldText(string text, Transform parent = null, Vector3 localPosition = default(Vector3), int fontSize = 40, Color? color = null, TextAnchor textAnchor = TextAnchor.UpperLeft, TextAlignment textAlignment = TextAlignment.Left, int sortingOrder = sortingOrderDefault) { if (color == null) color = Color.white; return CreateWorldText(parent, text, localPosition, fontSize, (Color)color, textAnchor, textAlignment, sortingOrder); } // Create Text in the World public static TextMesh CreateWorldText(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, TextAnchor textAnchor, TextAlignment textAlignment, int sortingOrder) { GameObject gameObject = new GameObject("World_Text", typeof(TextMesh)); Transform transform = gameObject.transform; transform.SetParent(parent, false); transform.localPosition = localPosition; TextMesh textMesh = gameObject.GetComponent(); textMesh.anchor = textAnchor; textMesh.alignment = textAlignment; textMesh.text = text; textMesh.fontSize = fontSize; textMesh.color = color; textMesh.GetComponent().sortingOrder = sortingOrder; return textMesh; } // Get Mouse Position in World with Z = 0f public static Vector3 GetMouseWorldPosition() { Vector3 vec = GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main); vec.z = 0f; return vec; } public static Vector3 GetMouseWorldPositionWithZ() { return GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main); } public static Vector3 GetMouseWorldPositionWithZ(Camera worldCamera) { return GetMouseWorldPositionWithZ(Input.mousePosition, worldCamera); } public static Vector3 GetMouseWorldPositionWithZ(Vector3 screenPosition, Camera worldCamera) { Vector3 worldPosition = worldCamera.ScreenToWorldPoint(screenPosition); return worldPosition; } public static Vector3 GetMouseWorldPosition(int mouseColliderLayerMask) { if(Camera.main == null) return new Vector3(); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask)) { return raycastHit.point; } else return Vector3.zero; } public static Vector3 GetMouseWorldPosition(int mouseColliderLayerMask, ref bool pointerHit) { if(Camera.main == null) return new Vector3(); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask)) { pointerHit = true; return raycastHit.point; } else { pointerHit = false; return Vector3.zero; } } // Create a Text Popup in the World, no parent public static void CreateWorldTextPopup(string text, Vector3 localPosition) { CreateWorldTextPopup(null, text, localPosition, 40, Color.white, localPosition + new Vector3(0, 20), 1f, true); } // Create a Text Popup in the World public static void CreateWorldTextPopup(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, Vector3 finalPopupPosition, float popupTime, bool centered) { TextMesh textMesh = CreateWorldText(parent, text, localPosition, fontSize, color, TextAnchor.LowerLeft, TextAlignment.Left, sortingOrderDefault); if(centered) textMesh.anchor = TextAnchor.MiddleCenter; Transform transform = textMesh.transform; Vector3 moveAmount = (finalPopupPosition - localPosition) / popupTime; CompositeDisposable disposables = new CompositeDisposable(); Observable.EveryUpdate().Subscribe(_ => { transform.position += moveAmount * Time.deltaTime; popupTime -= Time.deltaTime; if (popupTime <= 0f) { UnityEngine.Object.Destroy(transform.gameObject); disposables.Clear(); } }).AddTo(disposables); } public static float GetAngleFromVectorFloat(Vector3 dir) { dir = dir.normalized; float n = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; if (n < 0) n += 360; return n; } public static Vector3 ApplyRotationToVector(Vector3 vec, Vector3 vecRotation) { return ApplyRotationToVector(vec, GetAngleFromVectorFloat(vecRotation)); } public static Vector3 ApplyRotationToVector(Vector3 vec, float angle) { return Quaternion.Euler(0,0,angle) * vec; } } }