unity-atoms/Assets/UnityAtoms/GameFunctions/GetUnusedGameObject/GetUnusedGameObject.cs
2018-10-30 20:05:06 +01:00

44 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms
{
/* Gets an unused GameObject from the GameObjectList. If an GameObject is used or not is determined by IsUsed GameFunction.
* If no unused GameObject is found a new one is instantiated and added to the GameObjectList.
*/
[CreateAssetMenu(menuName = "UnityAtoms/Game Functions/Get Unused GameObject")]
public class GetUnusedGameObject : GameObjectVector3QuaternionFunction
{
[SerializeField]
private GameObjectList List;
[SerializeField]
private GameObject Prefab;
[SerializeField]
private BoolGameObjectFunction IsUsed;
public override GameObject Call(Vector3 position, Quaternion quaternion)
{
if (IsUsed == null)
{
Debug.LogWarning("IsUsed must be defined!");
}
for (int i = 0; i < List.Count; ++i)
{
if (!IsUsed.Call(List[i]))
{
List[i].transform.position = position;
List[i].transform.rotation = quaternion;
return List[i];
}
}
var newGameObject = Instantiate(Prefab, position, quaternion);
List.Add(newGameObject);
return newGameObject;
}
}
}