|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using Unity.Profiling; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.Perception.Randomization.Samplers; |
|
|
|
|
|
|
|
namespace UnityEngine.Perception.Randomization.Randomizers.Utilities |
|
|
|
{ |
|
|
|
|
|
|
{ |
|
|
|
static ProfilerMarker s_ResetAllObjectsMarker = new ProfilerMarker("ResetAllObjects"); |
|
|
|
|
|
|
|
GameObject[] m_Prefabs; |
|
|
|
UniformSampler m_Sampler = new UniformSampler(); |
|
|
|
Transform m_CacheParent; |
|
|
|
Dictionary<int, int> m_InstanceIdToIndex; |
|
|
|
List<CachedObjectData>[] m_InstantiatedObjects; |
|
|
|
|
|
|
/// <param name="prefabs">The prefabs to cache</param>
|
|
|
|
public GameObjectOneWayCache(Transform parent, GameObject[] prefabs) |
|
|
|
{ |
|
|
|
m_Prefabs = prefabs; |
|
|
|
m_CacheParent = parent; |
|
|
|
m_InstanceIdToIndex = new Dictionary<int, int>(); |
|
|
|
m_InstantiatedObjects = new List<CachedObjectData>[prefabs.Length]; |
|
|
|
|
|
|
foreach (var prefab in prefabs) |
|
|
|
{ |
|
|
|
if (!IsPrefab(prefab)) |
|
|
|
{ |
|
|
|
prefab.transform.parent = parent; |
|
|
|
prefab.SetActive(false); |
|
|
|
} |
|
|
|
var instanceId = prefab.GetInstanceID(); |
|
|
|
m_InstanceIdToIndex.Add(instanceId, index); |
|
|
|
m_InstantiatedObjects[index] = new List<CachedObjectData>(); |
|
|
|
|
|
|
|
|
|
|
++NumObjectsInCache; |
|
|
|
var newObject = Object.Instantiate(prefab, m_CacheParent); |
|
|
|
newObject.SetActive(true); |
|
|
|
++m_NumObjectsActive[index]; |
|
|
|
m_InstantiatedObjects[index].Add(new CachedObjectData(newObject)); |
|
|
|
return newObject; |
|
|
|
|
|
|
/// Retrieves an existing instance of the given prefab from the cache if available.
|
|
|
|
/// Otherwise, instantiate a new instance of the given prefab.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The index of the prefab to instantiate</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
|
public GameObject GetOrInstantiate(int index) |
|
|
|
{ |
|
|
|
var prefab = m_Prefabs[index]; |
|
|
|
return GetOrInstantiate(prefab); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieves an existing instance of a random prefab from the cache if available.
|
|
|
|
/// Otherwise, instantiate a new instance of the random prefab.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public GameObject GetOrInstantiateRandomPrefab() |
|
|
|
{ |
|
|
|
return GetOrInstantiate(m_Prefabs[(int)(m_Sampler.Sample() * m_Prefabs.Length)]); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Return all active cache objects back to an inactive state
|
|
|
|
/// </summary>
|
|
|
|
public void ResetAllObjects() |
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static bool IsPrefab(GameObject obj) |
|
|
|
{ |
|
|
|
return obj.scene.rootCount == 0; |
|
|
|
} |
|
|
|
|
|
|
|
struct CachedObjectData |
|
|
|