Files
2026-06-15 13:08:09 +02:00

145 lines
4.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
public class ObjectVisualizer : MonoBehaviour
{
public Transform familiarObjects;
public Transform unfamiliarObjects;
public List<int> objectDisplayOrder;
[Header("Input Setup")]
public InputActionReference changeObjectTypeAction; // Switches between familiar and unfamiliar objects
public InputActionReference showNextObjectAction; // Shows next object in the circular list
public InputActionReference showPreviousObjectAction; // Shows previous object in the circular list
private bool displayFamiliarObjects = true;
private int displayObjectSize;
private int currentShowedIndex = 0;
void OnEnable()
{
// Activate input callbacks
changeObjectTypeAction.action.performed += ChangeObjectType;
showNextObjectAction.action.performed += ShowNextObject;
showPreviousObjectAction.action.performed += ShowPreviousObject;
}
void OnDisable()
{
// Deactivate input callbacks
changeObjectTypeAction.action.performed -= ChangeObjectType;
showNextObjectAction.action.performed -= ShowNextObject;
showPreviousObjectAction.action.performed -= ShowPreviousObject;
}
/// INPUT CALLBACKS
void ChangeObjectType(InputAction.CallbackContext ctx)
{
displayFamiliarObjects = displayFamiliarObjects ? false : true;
ChangeObjectTypeToFamiliar(displayFamiliarObjects);
}
void ShowNextObject(InputAction.CallbackContext ctx)
{
currentShowedIndex++;
UpdateDisplayedObject();
}
void ShowPreviousObject(InputAction.CallbackContext ctx)
{
currentShowedIndex--;
UpdateDisplayedObject();
}
public void ChangeObjectTypeToFamiliar(bool familiar)
{
displayFamiliarObjects = familiar;
Debug.Log($"Changing object type | Displaying familiar objects?: {displayFamiliarObjects}");
UpdateDisplayObjectType();
// Restart displayed object index
currentShowedIndex = 0;
UpdateDisplayedObject();
}
public void ShowObjectInIndex(int idx)
{
currentShowedIndex = idx;
UpdateDisplayedObject();
}
public void ShowObjectInTypeAndIndex(bool familiar, int idx)
{
ChangeObjectTypeToFamiliar(familiar);
ShowObjectInIndex(idx);
}
public void HideAllObjects()
{
foreach (Transform child in familiarObjects)
{
child.gameObject.SetActive(false);
}
foreach (Transform child in unfamiliarObjects)
{
child.gameObject.SetActive(false);
}
}
/// MAIN METHODS
private int GetObjectsListSize()
{
if (displayFamiliarObjects)
return familiarObjects.childCount;
else
return unfamiliarObjects.childCount;
}
void UpdateDisplayObjectType()
{
familiarObjects.gameObject.SetActive(displayFamiliarObjects);
unfamiliarObjects.gameObject.SetActive(!displayFamiliarObjects);
displayObjectSize = GetObjectsListSize();
}
void UpdateDisplayedObject()
{
// Keep objects within limits
if (currentShowedIndex == displayObjectSize)
currentShowedIndex = 0;
if (currentShowedIndex == -1)
currentShowedIndex = displayObjectSize - 1;
Debug.Log($"Update displayed object | Current index: {currentShowedIndex}");
// Display corresponding object
Transform parentObject = displayFamiliarObjects ? familiarObjects : unfamiliarObjects;
for (int i = 0; i < displayObjectSize; i++)
{
// Show only the corresponding child indicated in the position of the list
parentObject.GetChild(i).gameObject.SetActive(i == objectDisplayOrder[currentShowedIndex]);
}
}
void Start()
{
// Initialize which type of objects to show
UpdateDisplayObjectType();
// Start the list with sequential numbers
objectDisplayOrder = Enumerable.Range(0, displayObjectSize).ToList();
// Initialize displayed object
UpdateDisplayedObject();
HideAllObjects();
}
}