Utilities.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // Create a Text Popup in the World, no parent
  58. public static void CreateWorldTextPopup(string text, Vector3 localPosition) {
  59. CreateWorldTextPopup(null, text, localPosition, 40, Color.white, localPosition + new Vector3(0, 20), 1f, true);
  60. }
  61. // Create a Text Popup in the World
  62. public static void CreateWorldTextPopup(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, Vector3 finalPopupPosition, float popupTime, bool centered) {
  63. TextMesh textMesh = CreateWorldText(parent, text, localPosition, fontSize, color, TextAnchor.LowerLeft, TextAlignment.Left, sortingOrderDefault);
  64. if(centered) textMesh.anchor = TextAnchor.MiddleCenter;
  65. Transform transform = textMesh.transform;
  66. Vector3 moveAmount = (finalPopupPosition - localPosition) / popupTime;
  67. CompositeDisposable disposables = new CompositeDisposable();
  68. Observable.EveryUpdate().Subscribe(_ => {
  69. transform.position += moveAmount * Time.deltaTime;
  70. popupTime -= Time.deltaTime;
  71. if (popupTime <= 0f) {
  72. UnityEngine.Object.Destroy(transform.gameObject);
  73. disposables.Clear();
  74. }
  75. }).AddTo(disposables);
  76. }
  77. public static float GetAngleFromVectorFloat(Vector3 dir) {
  78. dir = dir.normalized;
  79. float n = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  80. if (n < 0) n += 360;
  81. return n;
  82. }
  83. public static Vector3 ApplyRotationToVector(Vector3 vec, Vector3 vecRotation) {
  84. return ApplyRotationToVector(vec, GetAngleFromVectorFloat(vecRotation));
  85. }
  86. public static Vector3 ApplyRotationToVector(Vector3 vec, float angle) {
  87. return Quaternion.Euler(0,0,angle) * vec;
  88. }
  89. }
  90. }