196 lines
8.5 KiB
C#
196 lines
8.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using ZXK.UTility;
|
||
/*******************************************************************************
|
||
*Create By CG
|
||
*Function
|
||
*******************************************************************************/
|
||
namespace ZXK.BYSS
|
||
{
|
||
public class TerminalModel
|
||
{
|
||
public List<string> _LinesID { get; set; } = new List<string>();
|
||
public string _CtrlBoard { get; set; }
|
||
public string _TerminalName { get; set; }
|
||
public GameObject _TerminalGeo { get; set; }
|
||
|
||
public Vector3 _ChaTouPos { get; set; } = Vector3.zero;//插头作为子物体时的局部位置
|
||
public Vector3 _ChaTouRot { get; set; } = Vector3.up*-90;//插头作为子物体时的局部位置
|
||
|
||
public List<GameObject> _ConnectGeos { get; set; } = new List<GameObject>();
|
||
}
|
||
|
||
public class LineConnectModel
|
||
{
|
||
[SerializeField]
|
||
public Dictionary<string, List<TerminalModel[][]>> _terminalDic = new Dictionary<string, List<TerminalModel[][]>>();
|
||
/// <summary>
|
||
/// 最外层List存有多少行
|
||
/// 数组存有多少端子控制板,每个板子有多少个需要串联的端子
|
||
/// </summary>
|
||
|
||
public Dictionary<string, List<TerminalModel[][]>> _TerminalDic { get => _terminalDic;}
|
||
|
||
|
||
///// <summary>
|
||
///// 初始化数据
|
||
///// </summary>
|
||
public IEnumerator ReadLineConnectDataWeb(Transform contain,System.Action completeCall)
|
||
{
|
||
string url = "";
|
||
//考核加载自由接线表
|
||
if (AppManagement.Instance._TrainExam == EnumCtrl.Model.Exam)
|
||
{
|
||
url = System.IO.Path.Combine(Application.streamingAssetsPath, ConstCtrl.DATA_LINECONNECT_PATH);
|
||
WDebug.Log("加载自由接线表" + url);
|
||
}
|
||
//练习教学加载步骤接线表
|
||
else
|
||
{
|
||
url = System.IO.Path.Combine(Application.streamingAssetsPath, ConstCtrl.DATA_TeachAndTrainLINECONNECT_PATH);
|
||
Debug.Log("加载步骤接线表" + url);
|
||
}
|
||
UnityWebRequest request = UnityWebRequest.Get(url);
|
||
yield return request.SendWebRequest();
|
||
string t = request.downloadHandler.text;
|
||
if (request.result != UnityWebRequest.Result.Success)
|
||
{
|
||
Debug.LogError("Error: " + request.error);
|
||
}
|
||
else
|
||
{
|
||
Dictionary<string, GameObject> allChildren = new Dictionary<string, GameObject>();
|
||
AddAllChildren(contain, allChildren);
|
||
|
||
// 处理数据
|
||
string[] lines = t.Split(System.Environment.NewLine);
|
||
|
||
string curTypeNameTemp = "";
|
||
List<TerminalModel[][]> terminalArray = null;
|
||
string[] ctrlBoardName = null;
|
||
|
||
for (int a = 0; a < lines.Length; a++)
|
||
{
|
||
string line = lines[a];
|
||
if (string.IsNullOrEmpty(line)) continue;
|
||
if (lines[a].Contains("*"))
|
||
{
|
||
line = line.TrimStart('*').TrimEnd('*');
|
||
line = line.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\r\n", string.Empty);
|
||
string[] titleStr = line.Split("*");
|
||
curTypeNameTemp = titleStr[0];
|
||
if (EnumCtrl.GetEnumDescription(AppManagement.Instance._CurType).Equals(curTypeNameTemp))
|
||
{
|
||
string[] explain = titleStr[1].Split("|");
|
||
ctrlBoardName = new string[explain.Length - 1];
|
||
for (int index = 0; index < ctrlBoardName.Length; index++)
|
||
{
|
||
ctrlBoardName[index] = explain[index + 1];
|
||
}
|
||
terminalArray = new List<TerminalModel[][]>();
|
||
_terminalDic.Add(curTypeNameTemp, terminalArray);
|
||
}
|
||
else
|
||
{
|
||
terminalArray = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (terminalArray == null) continue;
|
||
line = line.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\r\n", string.Empty);
|
||
string[] ctrlBoards = line.Split("|");
|
||
string lineID = ctrlBoards[0];
|
||
TerminalModel[][] terminalGroupModels = new TerminalModel[ctrlBoards.Length - 1][];
|
||
for (int i = 1; i < ctrlBoards.Length; i++)
|
||
{
|
||
WDebug.Log("add数据1:" + ctrlBoards[i]);
|
||
if (!string.IsNullOrEmpty(ctrlBoards[i])&&!string.IsNullOrWhiteSpace(ctrlBoards[i]))
|
||
{
|
||
WDebug.Log("add数据2:" + ctrlBoards[i]);
|
||
string[] terminals = ctrlBoards[i].Split("_");
|
||
TerminalModel[] terminalModels = new TerminalModel[terminals.Length];
|
||
for (int j = 0; j < terminals.Length; j++)
|
||
{
|
||
TerminalModel temp = new TerminalModel();
|
||
temp._LinesID.Add(lineID);
|
||
temp._CtrlBoard = ctrlBoardName[i - 1];
|
||
temp._TerminalName = terminals[j];
|
||
if (allChildren.ContainsKey(temp._TerminalName))
|
||
{
|
||
temp._TerminalGeo = allChildren[temp._TerminalName];
|
||
TerminalCtrl tempCtrl = temp._TerminalGeo.GetComponent<TerminalCtrl>();
|
||
if (tempCtrl == null)
|
||
{
|
||
tempCtrl = temp._TerminalGeo.AddComponent<TerminalCtrl>();
|
||
}
|
||
tempCtrl.Init(temp);
|
||
}
|
||
terminalModels[j] = temp;
|
||
}
|
||
terminalGroupModels[i - 1] = terminalModels;
|
||
}
|
||
else
|
||
{
|
||
WDebug.Log("NoAdd数据2:" + ctrlBoards[i]);
|
||
}
|
||
}
|
||
terminalArray.Add(terminalGroupModels);
|
||
}
|
||
}
|
||
completeCall?.Invoke();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 递归添加某个物体下所有子孙
|
||
/// </summary>
|
||
/// <param name="parent"></param>
|
||
/// <param name="list"></param>
|
||
private void AddAllChildren(Transform parent, Dictionary<string, GameObject> list)
|
||
{
|
||
for (int i = 0; i < parent.childCount; i++)
|
||
{
|
||
GameObject childGeo = parent.GetChild(i).gameObject;
|
||
if (list.ContainsKey(childGeo.name))
|
||
{
|
||
//Debug.LogWarning($"存在重复键{childGeo.name}");
|
||
}
|
||
else
|
||
{
|
||
list.Add(childGeo.name, childGeo);
|
||
}
|
||
AddAllChildren(parent.GetChild(i), list);
|
||
}
|
||
}
|
||
|
||
|
||
public override string ToString()
|
||
{
|
||
string returnStr = "";
|
||
for (int i = 0; i < _TerminalDic[EnumCtrl.GetEnumDescription(AppManagement.Instance._CurType)].Count; i++)
|
||
{
|
||
TerminalModel[][] terminals = _TerminalDic[EnumCtrl.GetEnumDescription(AppManagement.Instance._CurType)][i];
|
||
string temp = "";
|
||
for (int a = 0; a < terminals.GetLength(0); a++)
|
||
{
|
||
if (terminals[a] != null)
|
||
{
|
||
for (int b = 0; b < terminals[a].Length; b++)
|
||
{
|
||
temp += terminals[a][b]._TerminalName + "_";
|
||
}
|
||
}
|
||
temp= temp.TrimEnd('_');
|
||
temp += "|";
|
||
}
|
||
temp = temp.Substring(0, temp.Length-1);
|
||
Debug.Log(temp);
|
||
returnStr = $"{returnStr}\n{temp}";
|
||
}
|
||
return returnStr;
|
||
}
|
||
}
|
||
} |