GQ_Communicate/GQ_TongXin/Assets/script/TOOL.cs

213 lines
5.8 KiB
C#

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<GameObject> gameObjects;
public List<Transform> Uwei;
public string port;
public string deviceId;
public string conDevice;
public string conPort;
public Transform UI_follow;
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<DeviceQuery>();
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<DeviceQuery>();
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<GameObject>("机柜/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<Transform> ts = new List<Transform>();
//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);
}
}
/// <summary>
/// 根据数字编号排序
/// </summary>
[ContextMenu("根据数字编号排序")]
void SortChildrenByName(List<Transform> children)
{
//List<Transform> children = new List<Transform>();
//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<Renderer>();
}
catch (Exception)
{
renderer = gameObjects[i].GetComponent<Renderer>();
}
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;
}
}
}
}