using System;
using System.Collections.Generic;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.Util;
using UnityEngine.SceneManagement;
namespace UnityEngine.ResourceManagement.ResourceProviders
{
///
/// Basic implementation of IInstanceProvider.
///
public class InstanceProvider : IInstanceProvider
{
Dictionary> m_InstanceObjectToPrefabHandle = new Dictionary>();
///
public GameObject ProvideInstance(ResourceManager resourceManager, AsyncOperationHandle prefabHandle, InstantiationParameters instantiateParameters)
{
GameObject result = instantiateParameters.Instantiate(prefabHandle.Result);
m_InstanceObjectToPrefabHandle.Add(result, prefabHandle);
return result;
}
///
public void ReleaseInstance(ResourceManager resourceManager, GameObject instance)
{
AsyncOperationHandle resource;
if (!m_InstanceObjectToPrefabHandle.TryGetValue(instance, out resource))
{
Debug.LogWarningFormat("Releasing unknown GameObject {0} to InstanceProvider.", instance);
}
else
{
resourceManager.Release(resource);
m_InstanceObjectToPrefabHandle.Remove(instance);
}
if (instance != null)
{
if (Application.isPlaying)
Object.Destroy(instance);
else
Object.DestroyImmediate(instance);
}
}
}
}