using System; using System.Collections.Generic; using System.Linq; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; using static UnityEditor.Progress; public class TOOL : MonoBehaviour { public List gameObjects; public string port; public string deviceId; public string conDevice; public string conPort; void Start() { } private void OnDropdownValueChanged_port(int arg0) { Debug.Log("索引改变为" + arg0); } // Update is called once per frame void Update() { } [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 = "01"; 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"; } 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(); } } /// /// 根据数字编号排序 /// [ContextMenu("根据数字编号排序")] public void ordering_rule() { // 获取父物体下的所有子物体 Transform parent = transform; int childCount = parent.childCount; Transform[] children = new Transform[childCount]; for (int i = 0; i < childCount; i++) { children[i] = parent.GetChild(i); } // 按照子物体名字排序 children = children.OrderBy(child => child.name).ToArray(); // 重新调整子物体在父物体下的顺序 for (int i = 0; i < childCount; i++) { children[i].SetSiblingIndex(i); } } }