Utilities.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UniRx;
  6. namespace KairoEngine.Utility
  7. {
  8. public class Utilities
  9. {
  10. public const int sortingOrderDefault = 5000;
  11. // Create Text in the World
  12. 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) {
  13. if (color == null) color = Color.white;
  14. return CreateWorldText(parent, text, localPosition, fontSize, (Color)color, textAnchor, textAlignment, sortingOrder);
  15. }
  16. // Create Text in the World
  17. public static TextMesh CreateWorldText(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, TextAnchor textAnchor, TextAlignment textAlignment, int sortingOrder) {
  18. GameObject gameObject = new GameObject("World_Text", typeof(TextMesh));
  19. Transform transform = gameObject.transform;
  20. transform.SetParent(parent, false);
  21. transform.localPosition = localPosition;
  22. TextMesh textMesh = gameObject.GetComponent<TextMesh>();
  23. textMesh.anchor = textAnchor;
  24. textMesh.alignment = textAlignment;
  25. textMesh.text = text;
  26. textMesh.fontSize = fontSize;
  27. textMesh.color = color;
  28. textMesh.GetComponent<MeshRenderer>().sortingOrder = sortingOrder;
  29. return textMesh;
  30. }
  31. // Get Mouse Position in World with Z = 0f
  32. public static Vector3 GetMouseWorldPosition() {
  33. Vector3 vec = GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main);
  34. vec.z = 0f;
  35. return vec;
  36. }
  37. public static Vector3 GetMouseWorldPositionWithZ() {
  38. return GetMouseWorldPositionWithZ(Input.mousePosition, Camera.main);
  39. }
  40. public static Vector3 GetMouseWorldPositionWithZ(Camera worldCamera) {
  41. return GetMouseWorldPositionWithZ(Input.mousePosition, worldCamera);
  42. }
  43. public static Vector3 GetMouseWorldPositionWithZ(Vector3 screenPosition, Camera worldCamera) {
  44. Vector3 worldPosition = worldCamera.ScreenToWorldPoint(screenPosition);
  45. return worldPosition;
  46. }
  47. public static Vector3 GetMouseWorldPosition(int mouseColliderLayerMask)
  48. {
  49. if(Camera.main == null) return new Vector3();
  50. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  51. if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask))
  52. {
  53. return raycastHit.point;
  54. }
  55. else return Vector3.zero;
  56. }
  57. public static Vector3 GetMouseWorldPosition(int mouseColliderLayerMask, ref bool pointerHit)
  58. {
  59. if(Camera.main == null) return new Vector3();
  60. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  61. if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask))
  62. {
  63. pointerHit = true;
  64. return raycastHit.point;
  65. }
  66. else
  67. {
  68. pointerHit = false;
  69. return Vector3.zero;
  70. }
  71. }
  72. // Create a Text Popup in the World, no parent
  73. public static void CreateWorldTextPopup(string text, Vector3 localPosition) {
  74. CreateWorldTextPopup(null, text, localPosition, 40, Color.white, localPosition + new Vector3(0, 20), 1f, true);
  75. }
  76. // Create a Text Popup in the World
  77. public static void CreateWorldTextPopup(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, Vector3 finalPopupPosition, float popupTime, bool centered) {
  78. TextMesh textMesh = CreateWorldText(parent, text, localPosition, fontSize, color, TextAnchor.LowerLeft, TextAlignment.Left, sortingOrderDefault);
  79. if(centered) textMesh.anchor = TextAnchor.MiddleCenter;
  80. Transform transform = textMesh.transform;
  81. Vector3 moveAmount = (finalPopupPosition - localPosition) / popupTime;
  82. CompositeDisposable disposables = new CompositeDisposable();
  83. Observable.EveryUpdate().Subscribe(_ => {
  84. transform.position += moveAmount * Time.deltaTime;
  85. popupTime -= Time.deltaTime;
  86. if (popupTime <= 0f) {
  87. UnityEngine.Object.Destroy(transform.gameObject);
  88. disposables.Clear();
  89. }
  90. }).AddTo(disposables);
  91. }
  92. public static float GetAngleFromVectorFloat(Vector3 dir) {
  93. dir = dir.normalized;
  94. float n = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  95. if (n < 0) n += 360;
  96. return n;
  97. }
  98. public static Vector3 ApplyRotationToVector(Vector3 vec, Vector3 vecRotation) {
  99. return ApplyRotationToVector(vec, GetAngleFromVectorFloat(vecRotation));
  100. }
  101. public static Vector3 ApplyRotationToVector(Vector3 vec, float angle) {
  102. return Quaternion.Euler(0,0,angle) * vec;
  103. }
  104. }
  105. }