Files
vr-configurator/vr-configurator/Assets/Scripts/UI/BaseModelSelector.cs
2025-06-03 18:44:42 +02:00

95 lines
2.5 KiB
C#

using TMPro;
using Unity.XR.CoreUtils;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class BaseModelSelector : MonoBehaviour
{
public Canvas canvas;
public GameObject buttonPrefab;
public PortSelector portSelector;
public ModelManager modelManager;
public GameObject unexplodeButton;
public GameObject ContentUi;
public GameObject _canvasButtons;
void Start()
{
buttonPrefab.SetActive(false);
_canvasButtons.SetActive(false);
CreateModelButtons();
}
void CreateModelButtons()
{
float startY = 0f; // Start position on Y axis
float buttonHeight = 0.3f; // Distance between Buttons
foreach (var model in ModelList.BaseModels)
{
var button = Spawn.GO(buttonPrefab, canvas.transform, new Vector3(0, startY, 0), "ModelButton:" + model.NameId);
button.GetNamedChild("Icon").GetComponent<Image>().sprite = Resources.Load<Sprite>($"sprites/" + model.NameId);
button.GetNamedChild("ModelName").GetComponent<TextMeshProUGUI>().text = model.NameHuman;
button.GetComponent<Button>().onClick.AddListener(() => OnModelButtonClicked(model));
button.SetActive(true);
startY -= buttonHeight; // Next button below
}
}
void OnModelButtonClicked(BaseModel model)
{
Debug.Log($"Selected model: {model.NameHuman}");
if (modelManager == null)
{
Debug.LogError("ModelManager is not assigned!");
return;
}
modelManager.LoadSelectedModel(model);
if (portSelector == null)
{
Debug.LogError("PortSelector is null!");
return;
}
portSelector.GenerateButtons();
if (_canvasButtons == null)
{
Debug.LogError("CanvasButtonis null!");
return;
}
_canvasButtons.SetActive(true);
if (canvas == null)
{
Debug.LogError("Canvas doesn't exist");
return;
}
gameObject.SetActive(false);
if (ContentUi == null)
{
Debug.LogError("ContentUi is null!");
return;
}
ContentUi.SetActive(true);
if (unexplodeButton == null)
{
Debug.LogError("UnexplodeButton is null!");
}
unexplodeButton.SetActive(false);
}
public void Activate()
{
gameObject.SetActive(true);
}
}