Files
beyond/Assets/Scripts/Grid/SerializedStreamedItem.cs
2024-11-20 15:21:28 +01:00

78 lines
2.0 KiB
C#

using System;
using Lean.Pool;
using UnityEngine;
namespace Beyond
{
[Serializable]
public class SerializedStreamedItem : ISpawnable, IPositionable, IGuidable
{
[SerializeField] public GameObject m_prefabToSpawn;
[SerializeField] public GuidObject guidObject;
[SerializeField] public Vector3 position;
[SerializeField] public Quaternion rotation;
//true value indicated that his item can be spawned by resource spawner, otherwise it is placeholder.
[SerializeField] public bool isAvailable = true;
private GameObject m_spawnedObject;
public SerializedStreamedItem(GameObject prefab, Transform trans)
{
m_prefabToSpawn = prefab;
position = trans.position;
rotation = trans.rotation;
}
public bool IsSpawned()
{
return m_spawnedObject != null;
}
public bool Spawn()
{
if (!isAvailable)
return false;
if (m_prefabToSpawn != null)
{
m_spawnedObject = LeanPool.Spawn(m_prefabToSpawn,position, rotation);
m_spawnedObject.SetActive(true);
var sr = m_spawnedObject.GetComponent<StreamedResource>();
if (sr != null)
{
sr.StreamedItem = this;
}
return m_spawnedObject != null;
}
return false;
}
public bool Despawn()
{
if (m_spawnedObject != null)
{
m_spawnedObject.SetActive(false);
LeanPool.Despawn(m_spawnedObject);
m_spawnedObject = null;
return true;
}
return false;
}
public Guid GetGuid()
{
return guidObject.guid;
}
public Vector3 GetPosition()
{
return position;
}
public Quaternion GetRotation()
{
return rotation;
}
}
}