using System;
using System.Collections.Generic;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
namespace UnityEngine.ResourceManagement.ResourceProviders
{
///
/// Class that contains properties to apply to instantiated objects.
///
public struct InstantiationParameters
{
Vector3 m_Position;
Quaternion m_Rotation;
Transform m_Parent;
bool m_InstantiateInWorldPosition;
bool m_SetPositionRotation;
///
/// Position in world space to instantiate object.
///
public Vector3 Position { get { return m_Position; } }
///
/// Rotation in world space to instantiate object.
///
public Quaternion Rotation { get { return m_Rotation; } }
///
/// Transform to set as the parent of the instantiated object.
///
public Transform Parent { get { return m_Parent; } }
///
/// When setting the parent Transform, this sets whether to preserve instance transform relative to world space or relative to the parent.
///
public bool InstantiateInWorldPosition { get { return m_InstantiateInWorldPosition; } }
///
/// Flag to tell the IInstanceProvider whether to set the position and rotation on new instances.
///
public bool SetPositionRotation { get { return m_SetPositionRotation; } }
///
/// Create a new InstantationParameters class that will set the parent transform and use the prefab transform.
///
/// Transform to set as the parent of the instantiated object.
/// Flag to tell the IInstanceProvider whether to set the position and rotation on new instances.
public InstantiationParameters(Transform parent, bool instantiateInWorldSpace)
{
m_Position = Vector3.zero;
m_Rotation = Quaternion.identity;
m_Parent = parent;
m_InstantiateInWorldPosition = instantiateInWorldSpace;
m_SetPositionRotation = false;
}
///
/// Create a new InstantationParameters class that will set the position, rotation, and Transform parent of the instance.
///
/// Position relative to the parent to set on the instance.
/// Rotation relative to the parent to set on the instance.
/// Transform to set as the parent of the instantiated object.
public InstantiationParameters(Vector3 position, Quaternion rotation, Transform parent)
{
m_Position = position;
m_Rotation = rotation;
m_Parent = parent;
m_InstantiateInWorldPosition = false;
m_SetPositionRotation = true;
}
///
/// Instantiate an object with the parameters of this object.
///
/// Object type. This type must be of type UnityEngine.Object.
/// Object to instantiate.
/// Returns the instantiated object.
public TObject Instantiate(TObject source) where TObject : Object
{
TObject result;
if (m_Parent == null)
{
if (m_SetPositionRotation)
result = Object.Instantiate(source, m_Position, m_Rotation);
else
result = Object.Instantiate(source);
}
else
{
if (m_SetPositionRotation)
result = Object.Instantiate(source, m_Position, m_Rotation, m_Parent);
else
result = Object.Instantiate(source, m_Parent, m_InstantiateInWorldPosition);
}
return result;
}
}
///
/// Interface that provides instances of objects. This is used in ResourceManager.Instantiate* calls.
///
public interface IInstanceProvider
{
///
/// Provide an instance of the gameobject contained in the prefabHandle.
///
/// The object that contains all the resource locations.
/// The operation handle for the prefab to instantiate.
/// The parameters to use for instantation.
/// The instantiated object.
GameObject ProvideInstance(ResourceManager resourceManager, AsyncOperationHandle prefabHandle, InstantiationParameters instantiateParameters);
///
/// Release an instance.
///
/// The object that contains all the resource locations.
/// The instance to release.
void ReleaseInstance(ResourceManager resourceManager, GameObject instance);
}
}