123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using Assets.Editor;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- public class Editor : EditorWindow {
- //[MenuItem("Tool/【*】生成Json")]
- public static void Read() {
- Transform[] transArr = Selection.transforms;
- if (transArr.Length > 0) {
- Transform trans = transArr[0];
- switch (trans.name) {
- case "PlayerSpots":
- SavePlayerSpots(trans);
- break;
- case "PersonSpots":
- SaveNpcSpots(trans);
- break;
- }
- }
- }
- public static void SavePlayerSpots(Transform trans) {
- PlayerSpotList spotList = new PlayerSpotList();
- for (int i = 0; i < trans.childCount; i++) {
- Spot dir = null;
- Spot prev = null;
- Transform spot = trans.GetChild(i);
- for (int j = 0; j < spot.childCount; j++) {
- Transform child = spot.GetChild(j);
- if (child.name.Contains("Direction")) {
- dir = new Spot(child.position, child.rotation, new Vector3(1, 1, 1));
- }
- else if (child.name.Contains("Preview")) {
- prev = new Spot(child.position, child.rotation, new Vector3(1, 1, 1));
- }
- }
- spotList.spots.Add(new PlayerSpot(dir, prev));
- }
- string json = JsonUtility.ToJson(spotList);
- using (FileStream fs = new FileStream("Assets/Json/PlayerSpots.json", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
- using (StreamWriter sw = new StreamWriter(fs)) {
- sw.Write(json);
- }
- }
- }
- public static void SaveNpcSpots(Transform trans) {
- NpcSpotList spotList = new NpcSpotList();
- for (int i = 0; i < trans.childCount; i++) {
- Spot dir = null;
- Transform spot = trans.GetChild(i);
- for (int j = 0; j < spot.childCount; j++) {
- Transform child = spot.GetChild(j);
- if (child.name.Contains("Direction")) {
- dir = new Spot(child.position, child.rotation, new Vector3(1, 1, 1));
- }
- }
- spotList.spots.Add(new NpcSpot(dir));
- }
- string json = JsonUtility.ToJson(spotList);
- using (FileStream fs = new FileStream("Assets/Json/NpcSpots.json", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
- using (StreamWriter sw = new StreamWriter(fs)) {
- sw.Write(json);
- }
- }
- }
- [MenuItem("Tool/相机看向目标")]
- public static void LookAt() {
- Transform[] transArr = Selection.transforms;
- if (transArr.Length > 0) {
- Transform trans = transArr[0];
- Camera.main.transform.LookAt(trans);
- }
- }
- [MenuItem("Tool/打印坐标")]
- public static void GetPosStr() {
- var transList = Selection.transforms.ToList();
- transList.Sort((a, b) => a.GetSiblingIndex() - b.GetSiblingIndex());
- if (transList.Count == 1) {
- string data = "Position:\n" + transList[0].position.x.ToString("F5") + "," + transList[0].position.y.ToString("F5") + "," + transList[0].position.z.ToString("F5");
- data += "\nRotation:\n" + transList[0].eulerAngles.x.ToString("F5") + "," + transList[0].eulerAngles.y.ToString("F5") + "," + transList[0].eulerAngles.z.ToString("F5");
- Debug.Log(data);
- return;
- }
- List<string> list1 = new List<string>();
- List<string> list2 = new List<string>();
- foreach (Transform trans in transList) {
- string data1 = trans.position.x.ToString("F5") + "," + trans.position.y.ToString("F5") + "," + trans.position.z.ToString("F5");
- string data2 = trans.eulerAngles.x.ToString("F5") + "," + trans.eulerAngles.y.ToString("F5") + "," + trans.eulerAngles.z.ToString("F5");
- list1.Add(data1);
- list2.Add(data2);
- }
- Debug.Log("Position:\n" + string.Join(";", list1.ToArray()) + "\nRotation:\n" + string.Join(";", list2.ToArray()));
- }
- [MenuItem("DevUtil/修复碰撞器")]
- public static void FixCollider() {
- Transform[] transArr = Selection.transforms;
- foreach (var trans in transArr) {
- _fixCollider(trans);
- }
- }
- private static void _fixCollider(Transform trans) {
- var colliders = trans.GetComponentsInChildren<Collider>();
- foreach (var collider in colliders) {
- if (collider.transform.localScale.x < 0.2) {
- collider.transform.localScale = new Vector3(1, 1, 1);
- if (collider is BoxCollider) {
- var boxCollider = collider as BoxCollider;
- boxCollider.size /= 10;
- boxCollider.center /= 10;
- }
- else if (collider is CapsuleCollider) {
- var capsuleCollider = collider as CapsuleCollider;
- capsuleCollider.center /= 10;
- capsuleCollider.height /= 10;
- capsuleCollider.radius /= 10;
- }
- }
- }
- }
- }
|