Files
vr-configurator/vr-configurator/Assets/Scripts/UI/PortSelector.cs
2025-07-09 12:07:11 +02:00

81 lines
2.4 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using Util;
public class PortSelector : MonoBehaviour, IResettable
{
public ModelManager modelManager;
public GameObject prefab;
public Vector3 offset = new Vector3(0,48,0);
public List<GameObject> portGOs = new List<GameObject>();
public List<PortSelectorButton> listPSB = new List<PortSelectorButton>();
public Dictionary<int, PortSelectorButton> mapPSB = new Dictionary<int, PortSelectorButton>();
//Gets called from BaseModelSelector
public void GenerateButtons()
{
ResetThis();
int i = 0;
int index = 0;
foreach (Port port in modelManager.BaseModel.Ports)
{
if (!port.Chooseable)
{
Debug.Log($"Skipping port {port.PortID} for Selection...");
index++;
continue;
}
Vector3 off = i * offset;
Debug.Log($"Adding Port {port.PortName} : {port.PortID} to Selection with {off.x}, {off.y}, {off.z}");
var go = Spawn.GO(prefab, transform, off, port.PortID + i);
portGOs.Add(go);
go.SetActive(true);
var text = go.GetComponentInChildren<TMPro.TextMeshProUGUI>();
if (text == null)
{
Debug.LogWarning($"No TextMeshProUGUI found in {go.name}, cannot set text.");
}
text.text = port.PortName;
var psb = go.GetComponent<PortSelectorButton>();
psb.PortID = port.PortID;
psb.PortIndex = index;
psb.marker = go.GetComponentInChildren<Marker>(true).gameObject;
psb.RemoveOtherMarkers = RemoveOtherMarkers;
listPSB.Add(psb);
mapPSB.Add(index, psb);
index++;
i++;
}
}
private void RemoveOtherMarkers()
{
foreach (var psb in listPSB)
{
psb.marker.SetActive(false);
}
}
public void ResetThis()
{
Debug.Log($"Resetting {this.name}");
foreach (var port in portGOs)
{
Debug.Log($"Destroying {port}");
Destroy(port);
}
portGOs.Clear();
foreach (var psb in listPSB)
{
Debug.Log($"Destroying {psb}");
Destroy(psb);
}
listPSB.Clear();
}
}