ImaageQ_Unity_App/Assets/Rendering/Scripts/Furniture/FurnitureManager.cs
Ignacio Gómez Puga 5847d844a5 Removed TOTU 103
2025-03-04 12:04:52 -06:00

95 lines
3.0 KiB
C#

using Assets.Scripts.Furniture;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class FurnitureManager : MonoBehaviour
{
[SerializeField] private ARRaycastManager arRaycastManager; // Para detectar superficies
[SerializeField] private List<GameObject> furniturePrefabs; // Lista de prefabs de muebles
[SerializeField] private GameObject furniturePreviewPrefab; // Prefab para previsualización
private GameObject _currentPreview;
private GameObject _currentFurniturePrefab;
private Vector3 _detectedPosition = Vector3.zero;
private Quaternion _detectedRotation = Quaternion.identity;
private bool _canAddFurniture = false;
private void Start()
{
// Establece el primer mueble como predeterminado
if (furniturePrefabs.Count > 0)
{
SetSelectedFurniture(furniturePrefabs[0]);
}
}
private void Update()
{
DetectPlaneAndUpdatePreview();
}
/// <summary>
/// Método para detectar planos y actualizar la posición de previsualización.
/// </summary>
private void DetectPlaneAndUpdatePreview()
{
if (_currentPreview == null) return;
var hits = new List<ARRaycastHit>();
var middleScreen = new Vector2(Screen.width / 2, Screen.height / 2);
if (arRaycastManager.Raycast(middleScreen, hits, TrackableType.PlaneWithinPolygon))
{
_detectedPosition = hits[0].pose.position;
_detectedRotation = hits[0].pose.rotation;
_currentPreview.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation);
_currentPreview.SetActive(true);
_canAddFurniture = true;
}
else
{
_currentPreview.SetActive(false);
_canAddFurniture = false;
}
}
/// <summary>
/// Método para colocar un mueble en la posición detectada.
/// </summary>
public void PlaceFurniture()
{
if (!_canAddFurniture || _currentFurniturePrefab == null) return;
var newFurniture = Instantiate(_currentFurniturePrefab);
newFurniture.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation);
// Añadir funcionalidad de manipulación (rotar, mover, etc.)
newFurniture.AddComponent<FurnitureDragger>();
Debug.Log("Mueble colocado en la posición detectada.");
}
/// <summary>
/// Cambiar el prefab del mueble seleccionado.
/// </summary>
/// <param name="furniturePrefab">Prefab del mueble seleccionado.</param>
public void SetSelectedFurniture(GameObject furniturePrefab)
{
_currentFurniturePrefab = furniturePrefab;
// Actualizar el preview
if (_currentPreview != null)
{
Destroy(_currentPreview);
}
_currentPreview = Instantiate(furniturePreviewPrefab ?? furniturePrefab);
_currentPreview.SetActive(false);
}
}