浏览代码

updated GameObjectCache with random prefab selection api

/generic-asset-sources
sleal-unity 3 年前
当前提交
6b8aa759
共有 2 个文件被更改,包括 56 次插入14 次删除
  1. 32
      com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs
  2. 38
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs

32
com.unity.perception/Runtime/Randomization/Randomizers/AssetSources/AssetSource.cs


}
/// <summary>
/// Returns the asset loaded from the provided index
/// Returns the unprocessed asset loaded from the provided index
public T LoadAsset(int index)
public T LoadRawAsset(int index)
{
CheckIfInitialized();
return assetSourceLocation.LoadAsset<T>(index);

/// Returns all assets that can be loaded from this AssetSource
/// Returns all unprocessed assets that can be loaded from this AssetSource
public T[] LoadAllAssets()
public T[] LoadAllRawAssets()
array[i] = LoadAsset(i);
{
array[i] = LoadRawAsset(i);
archetype.Preprocess(array[i]);
}
/// Creates an instance of the asset loaded from the provided index
/// Creates an instance of the asset loaded from the provided index and preprocesses it using the archetype
/// assigned to this asset source
public T CreateInstance(int index)
public T CreateProcessedInstance(int index)
return CreateInstance(LoadAsset(index));
return CreateProcessedInstance(LoadRawAsset(index));
/// Instantiates and returns all assets that can be loaded from this asset source
/// Instantiates, preprocesses, and returns all assets that can be loaded from this asset source
public T[] CreateInstances()
public T[] CreateProcessedInstances()
array[i] = CreateInstance(i);
array[i] = CreateProcessedInstance(i);
return array;
}

}
/// <summary>
/// Instantiates a uniformly random sampled asset from this AssetSource
/// Instantiates and preprocesses a uniformly random sampled asset from this AssetSource
return CreateInstance(SampleAsset());
return CreateProcessedInstance(SampleAsset());
}
/// <summary>

Initialize();
}
T CreateInstance(T asset)
T CreateProcessedInstance(T asset)
{
var instance = Object.Instantiate(asset);
if (archetype != null)

38
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs


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

正在加载...
取消
保存