load deviceANDIcon by codes automatically

This commit is contained in:
XuGaoFeng
2026-07-10 15:01:12 +08:00
parent 9579356ed1
commit d5aeb63276
17 changed files with 4670 additions and 4172 deletions

View File

@@ -1,13 +1,33 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
struct RequiredInformation
{
public string SNWithdeviceName;
public Vector3 localPosition;
public string customName;//这个是浮标上面的文本,自己命的名
public string pictureSerialNumber;//MT开头的图片
public RequiredInformation(string sn, Vector3 pos, string name, string picSn)
{
SNWithdeviceName = sn;
localPosition = pos;
customName = name;
pictureSerialNumber = picSn;
}
}
public class AskForWEBInfomation : MonoBehaviour
{
public Camera cam;
public GameObject buoy;//把BUoy预制体拖进来使用代码自动生成ICON的子物体
public List<Sprite> pictures = new List<Sprite>();//把图片预制体都拖过来
[Serializable]
public class IdentifierTextBinding
{
@@ -25,9 +45,70 @@ public class AskForWEBInfomation : MonoBehaviour
[Header("设备SN列表")]
public List<string> deviceSnList = new List<string>
{
"ys_2026020420","ys_2026020419","ys_2026020418","ys_2026020417","ys_2026020416","ys_2026020415"
};
List<RequiredInformation> info = new List<RequiredInformation>()
{
new RequiredInformation("ys_2026020415_sb_gnss001",new Vector3(10.9530001f,-10.7779989f,-1.75300026f),"GNSS","MT014") ,
new RequiredInformation("ys_2026020416_sb_wuxianqingjiaoji001",new Vector3(-0.25177002f,-0.00340000005f,3.51672745f),"无线倾角计1","倾角图标") ,
new RequiredInformation("ys_2026020416_sb_wuxianqingjiaoji002",new Vector3(-3.22239995f,-0.0055999998f,1.67970002f),"无线倾角计2","倾角图标") ,
new RequiredInformation("ys_2026020416_sb_wuxianqingjiaoji003",new Vector3(-0.254400015f,-0.00449999981f,-0.108400002f),"无线倾角计3","倾角图标") ,
new RequiredInformation("ys_2026020417_sb_fengjizhendong001",new Vector3(1.14961243f,55.1254196f,2.07600784f),"振动加速度1","MT019") ,
new RequiredInformation("ys_2026020417_sb_fengjizhendong002",new Vector3(1.14961243f,55.1785011f,1.38459396f),"振动加速度2","MT019") ,
new RequiredInformation("ys_2026020418_sb_yingbianji001",new Vector3(-1.90900004f,10.3079996f,3.55800009f),"应变计1","MT013") ,
new RequiredInformation("ys_2026020418_sb_yingbianji002",new Vector3(-3.17600012f,10.2980003f,1.66199994f),"应变计2","MT013") ,
new RequiredInformation("ys_2026020418_sb_yingbianji003",new Vector3(-1.87399995f,10.2880001f,-0.150199994f),"应变计3","MT013") ,
new RequiredInformation("ys_2026020418_sb_yingbianji004",new Vector3(0.349900007f,10.2938004f,0.450199991f),"应变计4","MT013") ,
new RequiredInformation("ys_2026020418_sb_yingbianji005",new Vector3(0.400999993f,10.2967997f,2.88899994f),"应变计5","MT013") ,
new RequiredInformation("ys_2026020419_sb_wenshiya001",new Vector3(-69.6037674f,-33.9762917f,141.763977f),"温湿压1","温湿图标") ,
new RequiredInformation("ys_2026020420_sb_yachashi001",new Vector3(10.7563248f,-12.5099993f,-1.32729721f),"静力水准仪1","MT014") ,
new RequiredInformation("ys_2026020420_sb_yachashi002",new Vector3(-1.17034912f,-12.5128984f,4.73325729f),"静力水准仪2","MT014") ,
new RequiredInformation("ys_2026020420_sb_yachashi003",new Vector3(-1.17050004f,-12.5162926f,-1.3204f),"静力水准仪3","MT014") ,
new RequiredInformation("ys_2026020420_sb_yachashi004",new Vector3(1.83947754f,-12.5169001f,1.70901871f),"静力水准仪4","MT014") ,
new RequiredInformation("ys_2026020420_sb_yachashi005",new Vector3(-4.20899963f,-12.5176001f,1.70901871f),"静力水准仪5","MT014") ,
};
/* Dictionary<string, Vector3> deviceName = new Dictionary<string, Vector3>//这个是ICONS下面所有的设备用系统自动实例化
{
{ "ys_2026020415_sb_gnss001",new Vector3(10.9530001f,-10.7779989f,-1.75300026f)} ,
{"ys_2026020416_sb_wuxianqingjiaoji001", new Vector3(-0.25177002f,-0.00340000005f,3.51672745f)},
{"ys_2026020416_sb_wuxianqingjiaoji002",new Vector3(-3.22239995f,-0.0055999998f,1.67970002f)},
{"ys_2026020416_sb_wuxianqingjiaoji003",new Vector3(-0.254400015f,-0.00449999981f,-0.108400002f) },
{"ys_2026020417_sb_fengjizhendong001",new Vector3(1.14961243f,55.1254196f,2.07600784f)},
{"ys_2026020417_sb_fengjizhendong002",new Vector3(1.14961243f,55.1785011f,1.38459396f) },
{"ys_2026020418_sb_yingbianji001" ,new Vector3(-1.90900004f,10.3079996f,3.55800009f)},
{"ys_2026020418_sb_yingbianji002" ,new Vector3(-3.17600012f,10.2980003f,1.66199994f)},
{"ys_2026020418_sb_yingbianji003" ,new Vector3(-1.87399995f,10.2880001f,-0.150199994f)},
{"ys_2026020418_sb_yingbianji004" ,new Vector3(0.349900007f,10.2938004f,0.450199991f)},
{"ys_2026020418_sb_yingbianji005" ,new Vector3(0.400999993f,10.2967997f,2.88899994f)},
{"ys_2026020419_sb_wenshiya001",new Vector3(-69.6037674f,-33.9762917f,141.763977f)},
{"ys_2026020420_sb_yachashi002",new Vector3(-1.17034912f,-12.5128984f,4.73325729f)},
{"ys_2026020420_sb_yachashi003",new Vector3(-1.17050004f,-12.5162926f,-1.3204f)},
{"ys_2026020420_sb_yachashi004",new Vector3(1.83947754f,-12.5169001f,1.70901871f)},
{"ys_2026020420_sb_yachashi005",new Vector3(-4.20899963f,-12.5176001f,1.70901871f)},
};
Dictionary<string, string> customName = new Dictionary<string, string>//这个是浮标上面的文本,自己命的名
{
{ "ys_2026020415_sb_gnss001","GNSS"} ,
{"ys_2026020416_sb_wuxianqingjiaoji001", "无线倾角计1"},
{"ys_2026020416_sb_wuxianqingjiaoji002","无线倾角计2"},
{"ys_2026020416_sb_wuxianqingjiaoji003","无线倾角计3" },
{"ys_2026020417_sb_fengjizhendong001","振动加速度1"},
{"ys_2026020417_sb_fengjizhendong002","振动加速度2" },
{"ys_2026020418_sb_yingbianji001" ,"应变计1"},
{"ys_2026020418_sb_yingbianji002" ,"应变计2"},
{"ys_2026020418_sb_yingbianji003" ,"应变计3"},
{"ys_2026020418_sb_yingbianji004" ,"应变计4"},
{"ys_2026020418_sb_yingbianji005" ,"应变计5"},
{"ys_2026020419_sb_wenshiya001","温湿压1"},
{"ys_2026020420_sb_yachashi002","温湿压2"},
{"ys_2026020420_sb_yachashi003","温湿压3"},
{"ys_2026020420_sb_yachashi004","温湿压4"},
{"ys_2026020420_sb_yachashi005","温湿压5"},
};*/
[Header("手动拖拽绑定identifier -> TMP_Text")]
public List<IdentifierTextBinding> bindings = new List<IdentifierTextBinding>();
@@ -42,8 +123,53 @@ public class AskForWEBInfomation : MonoBehaviour
public bool _isRunning = false;//这个东西只是用来防止手动连点
private void Awake()
{
cam = Camera.main;
InitiateICONS();
BuildBindingMap();
}
private void InitiateICONS()
{
foreach (RequiredInformation info in info)
{
//生成一个预制体
GameObject obj = Instantiate(buoy);
obj.name = info.SNWithdeviceName;
obj.transform.SetParent(transform);
obj.transform.localPosition = info.localPosition;
//开始修理预制体下面的响应函数和text
int first = obj.name.IndexOf("_");
int second = obj.name.IndexOf('_', first + 1);//second 是第二个下划线的索引索引是从0开始的
string deviceSn = obj.name.Substring(0, second);//前半部分比如ys_2026030415,末尾没有下划线,但是这部分不重要,重要的是后半部分。
//这里second表示长度第二个下划线索引是9也就是第10位但是索引做长度正好把第十位丢掉了
string deviceName = obj.name.Substring(second + 1); //设备名,也就是后半部分,从指定索引开始到结尾
//第一个子物体主要负责浮标的响应函数
obj.transform.GetChild(0).name = deviceName + "ICON";
obj.transform.GetChild(0).GetComponent<Button>().onClick.RemoveAllListeners();
obj.transform.GetChild(0).GetComponent<Button>().onClick.AddListener(
() => {
cam.GetComponent<DoubleClickToFocus>().FlyToIconAndConvertCore(obj.transform.GetChild(0).name);
}
);
obj.transform.GetChild(0).GetComponent<Button>().onClick.AddListener(
() => {
ManuallyLoadOneDevice(obj.name);
}
);
foreach (Sprite img in pictures)//遍历图片数组,如果找到就结束
{
if (img.name == info.pictureSerialNumber)
{
obj.transform.GetChild(0).GetComponent<Image>().sprite = img;
break;
}
}
//第二个子物体负责浮标上面的文字描述
obj.transform.GetChild(1).name = deviceName + "Text";
obj.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = info.customName;
}
}
private void BuildBindingMap()
{
textMap.Clear();
@@ -64,12 +190,12 @@ public class AskForWEBInfomation : MonoBehaviour
//这个是和前端交互的时候按下的按钮
public void ManuallyLoadOneDevice(string SnWithDeviceName)
{
if (_isRunning)
return;
StartCoroutine(LoadOneDeviceManually(SnWithDeviceName));
}
IEnumerator DelayPart()
{
@@ -116,7 +242,7 @@ public class AskForWEBInfomation : MonoBehaviour
}
string json = req.downloadHandler.text;
DeviceDetailResponse resp = null;
try
{
@@ -134,7 +260,7 @@ public class AskForWEBInfomation : MonoBehaviour
yield break;
}
if (resp.status.code != 0)
{
@@ -154,7 +280,7 @@ public class AskForWEBInfomation : MonoBehaviour
if (string.IsNullOrWhiteSpace(point.identifier)) continue;
UpdateText(point.identifier, point.value, point.unitName, point.name);
}
}
}
@@ -165,7 +291,7 @@ public class AskForWEBInfomation : MonoBehaviour
_isRunning = true;
yield return null;
float transitionDuration = Camera.main.transform.GetComponent<DoubleClickToFocus>().transitionDuration;
yield return new WaitForSeconds(transitionDuration+0.01f); // 等待2秒
yield return new WaitForSeconds(transitionDuration + 0.01f); // 等待2秒
// 这里写延迟执行的代码
Debug.Log("2秒后执行");
@@ -219,7 +345,7 @@ public class AskForWEBInfomation : MonoBehaviour
SendData sendData = new SendData();
sendData.result = resp.result;
sendData.iconName = iconName;
string CoutJsonData = JsonUtility.ToJson(sendData,true);
string CoutJsonData = JsonUtility.ToJson(sendData, true);
Application.ExternalCall("sendDataToVue", CoutJsonData);
//Debug.Log(CoutJsonData);
@@ -242,7 +368,7 @@ public class AskForWEBInfomation : MonoBehaviour
UpdateText(point.identifier, point.value, point.unitName, point.name);
}
}
_isRunning = false;//协程运行完成自动解锁。
@@ -270,7 +396,7 @@ public class AskForWEBInfomation : MonoBehaviour
finalText = itemName + ": " + finalText;
}
target.text = finalText;
target.text = finalText;
}
private string GetRequestUrl()
@@ -321,7 +447,7 @@ public class AskForWEBInfomation : MonoBehaviour
{
public string iconName;
public DeviceResult result;
}
[Serializable]
public class DataPoints