Editor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Assets.Editor;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. public class Editor : EditorWindow {
  8. //[MenuItem("Tool/【*】生成Json")]
  9. public static void Read() {
  10. Transform[] transArr = Selection.transforms;
  11. if (transArr.Length > 0) {
  12. Transform trans = transArr[0];
  13. switch (trans.name) {
  14. case "PlayerSpots":
  15. SavePlayerSpots(trans);
  16. break;
  17. case "PersonSpots":
  18. SaveNpcSpots(trans);
  19. break;
  20. }
  21. }
  22. }
  23. public static void SavePlayerSpots(Transform trans) {
  24. PlayerSpotList spotList = new PlayerSpotList();
  25. for (int i = 0; i < trans.childCount; i++) {
  26. Spot dir = null;
  27. Spot prev = null;
  28. Transform spot = trans.GetChild(i);
  29. for (int j = 0; j < spot.childCount; j++) {
  30. Transform child = spot.GetChild(j);
  31. if (child.name.Contains("Direction")) {
  32. dir = new Spot(child.position, child.rotation, new Vector3(1, 1, 1));
  33. }
  34. else if (child.name.Contains("Preview")) {
  35. prev = new Spot(child.position, child.rotation, new Vector3(1, 1, 1));
  36. }
  37. }
  38. spotList.spots.Add(new PlayerSpot(dir, prev));
  39. }
  40. string json = JsonUtility.ToJson(spotList);
  41. using (FileStream fs = new FileStream("Assets/Json/PlayerSpots.json", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
  42. using (StreamWriter sw = new StreamWriter(fs)) {
  43. sw.Write(json);
  44. }
  45. }
  46. }
  47. public static void SaveNpcSpots(Transform trans) {
  48. NpcSpotList spotList = new NpcSpotList();
  49. for (int i = 0; i < trans.childCount; i++) {
  50. Spot dir = null;
  51. Transform spot = trans.GetChild(i);
  52. for (int j = 0; j < spot.childCount; j++) {
  53. Transform child = spot.GetChild(j);
  54. if (child.name.Contains("Direction")) {
  55. dir = new Spot(child.position, child.rotation, new Vector3(1, 1, 1));
  56. }
  57. }
  58. spotList.spots.Add(new NpcSpot(dir));
  59. }
  60. string json = JsonUtility.ToJson(spotList);
  61. using (FileStream fs = new FileStream("Assets/Json/NpcSpots.json", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
  62. using (StreamWriter sw = new StreamWriter(fs)) {
  63. sw.Write(json);
  64. }
  65. }
  66. }
  67. [MenuItem("Tool/相机看向目标")]
  68. public static void LookAt() {
  69. Transform[] transArr = Selection.transforms;
  70. if (transArr.Length > 0) {
  71. Transform trans = transArr[0];
  72. Camera.main.transform.LookAt(trans);
  73. }
  74. }
  75. [MenuItem("Tool/打印坐标")]
  76. public static void GetPosStr() {
  77. var transList = Selection.transforms.ToList();
  78. transList.Sort((a, b) => a.GetSiblingIndex() - b.GetSiblingIndex());
  79. if (transList.Count == 1) {
  80. string data = "Position:\n" + transList[0].position.x.ToString("F5") + "," + transList[0].position.y.ToString("F5") + "," + transList[0].position.z.ToString("F5");
  81. data += "\nRotation:\n" + transList[0].eulerAngles.x.ToString("F5") + "," + transList[0].eulerAngles.y.ToString("F5") + "," + transList[0].eulerAngles.z.ToString("F5");
  82. Debug.Log(data);
  83. return;
  84. }
  85. List<string> list1 = new List<string>();
  86. List<string> list2 = new List<string>();
  87. foreach (Transform trans in transList) {
  88. string data1 = trans.position.x.ToString("F5") + "," + trans.position.y.ToString("F5") + "," + trans.position.z.ToString("F5");
  89. string data2 = trans.eulerAngles.x.ToString("F5") + "," + trans.eulerAngles.y.ToString("F5") + "," + trans.eulerAngles.z.ToString("F5");
  90. list1.Add(data1);
  91. list2.Add(data2);
  92. }
  93. Debug.Log("Position:\n" + string.Join(";", list1.ToArray()) + "\nRotation:\n" + string.Join(";", list2.ToArray()));
  94. }
  95. [MenuItem("DevUtil/修复碰撞器")]
  96. public static void FixCollider() {
  97. Transform[] transArr = Selection.transforms;
  98. foreach (var trans in transArr) {
  99. _fixCollider(trans);
  100. }
  101. }
  102. private static void _fixCollider(Transform trans) {
  103. var colliders = trans.GetComponentsInChildren<Collider>();
  104. foreach (var collider in colliders) {
  105. if (collider.transform.localScale.x < 0.2) {
  106. collider.transform.localScale = new Vector3(1, 1, 1);
  107. if (collider is BoxCollider) {
  108. var boxCollider = collider as BoxCollider;
  109. boxCollider.size /= 10;
  110. boxCollider.center /= 10;
  111. }
  112. else if (collider is CapsuleCollider) {
  113. var capsuleCollider = collider as CapsuleCollider;
  114. capsuleCollider.center /= 10;
  115. capsuleCollider.height /= 10;
  116. capsuleCollider.radius /= 10;
  117. }
  118. }
  119. }
  120. }
  121. }