112 lines
3.2 KiB
C#
112 lines
3.2 KiB
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using TMPro;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class ActiveScoppedElements : MonoBehaviour
|
||
|
{
|
||
|
|
||
|
[Serializable]
|
||
|
public struct ObjectToInstantiate {
|
||
|
public List<String> elements;
|
||
|
public GameObject obj;
|
||
|
}
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
public HashSet<String> actives = new HashSet<String>();
|
||
|
public HashSet<GameObject> InstantiatesObject = new HashSet<GameObject>();
|
||
|
public TextMeshProUGUI textToUpdate;
|
||
|
public TextMeshProUGUI objectInstantiateText;
|
||
|
public ObjectToInstantiate[] objectToInstantiates;
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
if (this.textToUpdate != null)
|
||
|
{
|
||
|
textToUpdate.text = "Scanner des Markers...";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void AddScoppedElement(String name)
|
||
|
{
|
||
|
Debug.Log("Added " + name);
|
||
|
if (!actives.Contains(name))
|
||
|
{
|
||
|
actives.Add(name);
|
||
|
RefreshText();
|
||
|
RefreshAtom();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void RefreshText()
|
||
|
{
|
||
|
if (this.textToUpdate != null)
|
||
|
{
|
||
|
this.textToUpdate.text = String.Join(", ", this.actives);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void RefreshAtom()
|
||
|
{
|
||
|
foreach (var objt in objectToInstantiates)
|
||
|
{
|
||
|
if (objt.elements.TrueForAll((s => actives.Contains(s))))
|
||
|
{
|
||
|
// Obtenez la position de la caméra
|
||
|
Vector3 cameraPosition = Camera.main.transform.position;
|
||
|
|
||
|
// Obtenez la rotation de la caméra sans inclure la rotation autour de l'axe Y
|
||
|
Quaternion cameraRotation = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0);
|
||
|
|
||
|
// Calculez la position finale en ajoutant la direction vers l'avant multipliée par la distance désirée
|
||
|
Vector3 position = cameraPosition + cameraRotation * Vector3.forward * 0.5f;
|
||
|
|
||
|
var instantiatedObj = Instantiate(objt.obj, position, cameraRotation);
|
||
|
instantiatedObj.name = "[N] " + objt.obj.name;
|
||
|
InstantiatesObject.Add(instantiatedObj);
|
||
|
Debug.Log("Instantiate " + instantiatedObj.name);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var a = InstantiatesObject.FirstOrDefault(s => s.name == ("[N] " + objt.obj.name));
|
||
|
if (a != null)
|
||
|
{
|
||
|
Debug.Log("Destroy " + a.name);
|
||
|
InstantiatesObject.Remove(a);
|
||
|
Destroy(a);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (InstantiatesObject.Count > 0)
|
||
|
{
|
||
|
objectInstantiateText.text = String.Join(", ", InstantiatesObject.Select((s) => s.name));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
objectInstantiateText.text = "En attente...";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void RemoveUnscoppedElement(String name)
|
||
|
{
|
||
|
Debug.Log("Removed " + name);
|
||
|
actives.Clear();
|
||
|
RefreshText();
|
||
|
RefreshAtom();
|
||
|
////if (actives.Contains(name))
|
||
|
//{
|
||
|
// actives.Remove(name);
|
||
|
// RefreshText();
|
||
|
// RefreshAtom();
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|