//using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using TMPro; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; public class TOOL : MonoBehaviour { public List gameObjects; public List Uwei; public GameObject prefab; public string port; public string deviceId; public string conDevice; public string conPort; public Transform UI_follow; public RedactDevice redactDevice; public TMP_FontAsset f_old; public TMP_FontAsset f_new; public Font font_old; public Font font_new; public Image Image; //public Collider collider; void Start() { //collider=GetComponent(); //var t = CombineJSON.Base64StringToTexture("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAADa0lEQVRYw72WQWtdVRSFv73PfS99fUlVagWxpZqOFFpnFqE6rgT/QQYO/AvOHPk/xEHjxIkOFFsKjlQUUVEngpRgox1IsCI2Me+eu5eDc1/SlMg7t2IOnMG73Hf2umuts9eG411268sP13a2vsnvvPGygHFzXIW/unHtmafPnX3fiUvWjEjmAFM/huLpznc33ruwev7W7vZPl7Y+fZvIGTMDGC1ioHn3zasfTMa+5mZYZcWQmM1mrF68wvnnXqTd+Z1ff/iMvHMXJEJBSuXbFwGYLDW29tZHu4wbwyoRRIh7f8/4+rUr3P7+E/7a/hnzBrxB0aIuY14H4IQZTJYaTjQMAiCN+OXzDXb3ArMGcFCggOgyqT9sEQCzADfDHbwSgGGklJA7YRACJFCRp+vauQcWAkAITz4IAIB7KRaoAIBef4jc4pUeIATJBwIwSAkUQhK6D4B6BmolQArMRpip2gNG8UsXIroHGAgRA0wIKnS6Wz0DKu9LRzCA6NoWr2YgwFMieT0DcwChKD7QvL7KLciZVMtAIMwcBkrgLrooHgqBMCIMhZO7jHklA0gkTySPgQyomDCKBBHG6pObnDl1h618dZ4FLMyCIoGV7fU7JaOLIkEn6AKeeHSbazefhejqGZCEuWNOdRaYwJx9A6qXAYPpZES0M3zILUjm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cm9vdC9uZXdzaXRlL3d3dy5lYXN5aWNvbi5uZXQvY2RuLWltZy5lYXN5aWNvbi5jbi9maWxlcy8xLzEyNjc0LnBuZ5hinOAAAAAASUVORK5CYII="); //Image.sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), Vector2.zero); } // Update is called once per frame void Update() { Debug.Log(Input.mousePosition); } float Map(float value, float inputMin, float inputMax, float outputMin, float outputMax) { return (value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin; } [ContextMenu("设置线缆层级高度")] public void SetxianLanHight() { for (int i = 0; i < gameObjects.Count; i++) { gameObjects[i].transform.localPosition += (new Vector3(0, 0.135f, 0) * i); } } [ContextMenu("按顺序设置端口位置")] public void setPortPos() { for (int i = 0; i < gameObjects.Count; i++) { gameObjects[i].GetComponent().portList.portPosition = (i + 1).ToString(); } } [ContextMenu("重命名")] public void reName() { if (gameObjects.Count == 0 && gameObjects == null) return; for (int i = 0; i < gameObjects.Count; i++) { var deviceQuery = gameObjects[i].GetComponent(); int a = int.Parse(gameObjects[i].name.Split('R')[1]); string b = ""; if (a <= 8) { b = "I-"; } else if (a <= 16) { b = "H-"; } else if (a <= 24) { b = "G-"; } else if (a <= 32) { b = "F-"; } else if (a <= 40) { b = "E-"; } else if (a <= 48) { b = "D-"; } else if (a <= 56) { b = "C-"; } else if (a <= 64) { b = "B-"; } else if (a <= 72) { b = "A-"; } var nub = (a % 8).ToString().PadLeft(2, '0'); if (nub == "00") nub = "08"; var devicePosition = b + nub; if (deviceQuery == null) { var D = gameObjects[i].AddComponent(); D.deviceList.deviceCode = ""; D.deviceList.devicePosition = devicePosition; D.deviceList.type = "1"; } else { deviceQuery.deviceList.deviceCode = ""; deviceQuery.deviceList.devicePosition = devicePosition; deviceQuery.deviceList.type = "1"; } continue; var U = gameObjects[i].transform.Find("U位"); if (U == null) { //var go = new GameObject("U位"); //go.transform.SetParent(gameObjects[i].transform); //for (int j = 0; j < 42; j++) //{ // var g = new GameObject((j + 1).ToString()); // g.transform.SetParent(go.transform); //} var go = GameObject.Instantiate(Resources.Load("机柜/U位")).transform; go.position = gameObjects[i].transform.position; go.SetParent(gameObjects[i].transform); go.name = "U位"; } else { DestroyImmediate(U.gameObject); //if (U.transform.childCount != 42) //{ // for (int j = 0; j < 42; j++) // { // var g = new GameObject((j + 1).ToString()); // g.transform.SetParent(U.transform); // } //} } } } [ContextMenu("寻找物体组")] public void das() { GameManager.Inst.FindPortPos(); } [ContextMenu("U位修改")] public void us() { List ts = new List(); //for (int i = 0; i < transform.childCount; i++) //{ // ts.Add(transform.GetChild(i).transform); //} //ts.Reverse(); //for (int i = 0; i < ts.Count; i++) //{ // ts[i].name = (i + 1).ToString(); //} //ts.Reverse(); for (int i = 0; i < Uwei.Count; i++) { ts.Clear(); for (int j = 0; j < Uwei[i].childCount; j++) { ts.Add(Uwei[i].GetChild(j).transform); } ts.Reverse(); for (int k = 0; k < ts.Count; k++) { ts[k].name = (k + 1).ToString(); } SortChildrenByName(ts); } } /// /// 根据数字编号排序 /// [ContextMenu("根据数字编号排序")] void SortChildrenByName(List children) { //List children = new List(); //foreach (Transform child in transform) //{ // children.Add(child); //} children.Sort((a, b) => { int numberA = int.Parse(a.name); int numberB = int.Parse(b.name); return numberA.CompareTo(numberB); }); for (int i = 0; i < children.Count; i++) { children[i].SetSiblingIndex(i); } } [ContextMenu("匹配U位")] public void dsad() { foreach (var item in Uwei) { for (int i = 0; i < gameObjects.Count; i++) { if (item.name == gameObjects[i].name) { item.SetParent(gameObjects[i].transform); item.name = "U位"; } } } } [ContextMenu("UI跟随")] public void dasda() { for (int i = 0; i < gameObjects.Count; i++) { Renderer renderer = null; try { renderer = gameObjects[i].transform.Find(gameObjects[i].name).GetComponent(); } catch (Exception) { renderer = gameObjects[i].GetComponent(); } if (renderer != null) { Bounds bounds = renderer.bounds; Vector3 topPosition = gameObjects[i].transform.position + new Vector3(0, bounds.size.y, 0); UI_follow.GetChild(i).position = topPosition; } } } [ContextMenu("修改机柜")] public void dsadsa() { //var objA = gameObjects[0].GetComponent().deviceList; //MapperConfiguration config = new MapperConfiguration(cfg => //{ // cfg.CreateMap(); // cfg.CreateMap(); //}); //IMapper mapper = config.CreateMapper(); //// 转换 ClassA 到 ClassB //redactDevice.mybody = mapper.Map(objA); } [ContextMenu("xg端口碰撞器")] public void ssadsa() { if (transform.Find("端口碰撞器")) { for (int i = 0; i < transform.Find("端口碰撞器").childCount; i++) { transform.Find("端口碰撞器").GetChild(i).SetParent(transform); } DestroyImmediate(transform.Find("端口碰撞器").gameObject); } } [ContextMenu("add告警")] public void sadsadsasssssss() { var a = GetComponentsInChildren(); Array.ForEach(a, item => { if (!item.gameObject.GetComponent()) item.gameObject.AddComponent(); }); } [ContextMenu("add温湿度")] public void dasd() { Array.ForEach(gameObjects.ToArray(), item => { if (!item.gameObject.GetComponent()) item.gameObject.AddComponent(); }); } public GameObject originalObject; // 模型的原始游戏对象 [ContextMenu("根据mesh生成立方体")] public void GenerateCubeFromMesh() { //获取原始对象的Mesh Mesh originalMesh = originalObject.GetComponent().sharedMesh; //创建一个新的游戏对象和网格 GameObject cubeObject = new GameObject("Cube"); Mesh cubeMesh = new Mesh(); //设置立方体的顶点数据 Vector3[] vertices = new Vector3[originalMesh.vertices.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = originalMesh.vertices[i]; } //创建立方体的三角形面 int[] triangles = new int[originalMesh.triangles.Length]; for (int i = 0; i < triangles.Length; i++) { triangles[i] = originalMesh.triangles[i]; } //将顶点和三角形面设置到网格中 cubeMesh.vertices = vertices; cubeMesh.triangles = triangles; //设置网格的UV坐标和法线 cubeMesh.uv = originalMesh.uv; cubeMesh.normals = originalMesh.normals; //将网格赋值给立方体对象的MeshFilter组件 MeshFilter cubeMeshFilter = cubeObject.AddComponent(); cubeMeshFilter.mesh = cubeMesh; //添加立方体对象的MeshRenderer组件并共享材质 MeshRenderer cubeMeshRenderer = cubeObject.AddComponent(); cubeMeshRenderer.sharedMaterial = originalObject.GetComponent().sharedMaterial; cubeObject.transform.parent = transform; } [ContextMenu("添加高亮立方体")] public void addcube() { for (int i = 0; i < gameObjects.Count; i++) { // GameObject cubeObject = GameObject.Instantiate(Resources.Load("高亮"), gameObjects[i].transform); //// cubeObject.transform.parent = gameObjects[i].transform; // cubeObject.transform.transform.localPosition = Vector3.zero; // cubeObject.transform.transform.localEulerAngles = Vector3.zero; // //cubeObject.transform.transform.localScale = Vector3.one; var a = gameObjects[i].transform.GetChild(gameObjects[i].transform.childCount - 1); a.name = "高亮"; } } [ContextMenu("修改tmp字体")] public void srtTMP() { Array.ForEach(GameObject.FindObjectsOfType(true), (item) => { if (item.font == null) item.font = f_new; }); } [ContextMenu("修改字体")] public void setFont() { Array.ForEach(GameObject.FindObjectsOfType(true), (item) => { if (item.font == null) item.font = font_new; }); } [ContextMenu("端口修改层级")] public void setport() { Array.ForEach(transform.GetComponentsInChildren(), (item) => { item.gameObject.layer = 11; }); } [ContextMenu("add端口高亮")] public void addporthight() { Array.ForEach(transform.GetComponentsInChildren(), (item) => { if (!item.transform.Find("端口高亮")) { //GameObject go = Instantiate(prefab); prefab.transform.SetParent(item.transform); prefab.name = "端口高亮"; } }); } [ContextMenu("加门脚本")] public void adddoor() { var s = Array.FindAll(GameObject.FindObjectsOfType(), (itme) => { return itme.name == "门把手"; }); for (int i = 0; i < s.Length; i++) { if (!s[i].Find("Canvas/Image").GetComponent()) s[i].Find("Canvas/Image").gameObject.AddComponent(); } } [ContextMenu("预制体加透明脚本")] public void tm() { Array.ForEach(transform.GetComponentsInChildren(), (item) => { if (!item.gameObject.GetComponent()) item.gameObject.AddComponent(); }); } [ContextMenu("板卡预制体加检测器")] public void DSA() { var a = Array.FindAll(transform.GetComponentsInChildren(), (item) => { return true; }).ToList(); foreach (var item in a) { Renderer renderer = item.transform.parent.GetComponent(); if (renderer != null) { var initrot = item.transform.parent.transform.rotation; item.transform.parent.transform.rotation = Quaternion.Euler(Vector3.zero); item.transform.parent.transform.rotation = Quaternion.identity; var bounds = renderer.bounds; if (item.transform.parent.gameObject.GetComponent()) { if (!item.transform.parent.GetComponent()) { item.transform.parent.gameObject.AddComponent(); item.transform.parent.gameObject.layer = 13; } continue; } var aa = item.transform.parent.gameObject.AddComponent(); aa.isTrigger = false; aa.center = bounds.center - item.transform.parent.transform.position; aa.size = bounds.size; item.transform.parent.transform.rotation = initrot; } if (!item.transform.parent.GetComponent()) { item.transform.parent.gameObject.AddComponent(); item.transform.parent.gameObject.layer = 13; } } } [ContextMenu("同步容积")] public void tbrj() { GetComponent().volume = GetComponent().volume; } [ContextMenu("修改机柜结构")] public void setCabinets() { for (int i = gameObjects.Count - 1; i >= 0; i--) { if (gameObjects[i].transform.Find(gameObjects[i].name)) { var go = gameObjects[i].transform.Find(gameObjects[i].name); go.SetParent(transform); var c = gameObjects[i].transform; for (int j = c.childCount - 1; j >= 0; j--) { c.GetChild(j).SetParent(go); } var d_old = gameObjects[i].gameObject.GetComponent(); var d_new = go.gameObject.AddComponent(); d_new.deviceList.type = d_old.deviceList.type; d_new.deviceList.devicePosition = d_old.deviceList.devicePosition; DestroyImmediate(gameObjects[i].gameObject); ReverseChildren(go); } else { gameObjects[i].transform.SetParent(transform); } } SortChildrenByName(); } void SortChildrenByName() { List children = new List(); // 获取物体的所有子物体 for (int i = 0; i < transform.childCount; i++) { children.Add(transform.GetChild(i)); } // 按照名字进行排序 children.Sort((a, b) => { int aIndex = int.Parse(a.name.Replace("R", "")); int bIndex = int.Parse(b.name.Replace("R", "")); return aIndex.CompareTo(bIndex); }); // 重新设置子物体的顺序 for (int i = 0; i < children.Count; i++) { children[i].SetSiblingIndex(i); } } void ReverseChildren(Transform go) { int childCount = go.childCount; for (int i = 0; i < childCount / 2; i++) { int opposite = childCount - 1 - i; Transform child = go.GetChild(i); Transform oppositeChild = go.GetChild(opposite); // 交换子物体的位置 child.SetSiblingIndex(opposite); oppositeChild.SetSiblingIndex(i); } } [ContextMenu("调整锚点位置")] public void set_MaoDian_pos() { var v = transform.Find("锚点"); if (v) { v.SetParent(null); v.SetParent(transform); } } [ContextMenu("寻找设置板卡网格")] public void findSetMesh() { MeshCollider[] meshColliders = transform.GetComponentsInChildren(); for (int i = 0; i < meshColliders.Length; i++) { if (meshColliders[i].gameObject.GetComponent()) meshColliders[i].sharedMesh = meshColliders[i].gameObject.GetComponent().mesh; } } }