// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Utilities.GameObjectManagement { /// /// An abstract class used by the GameObjectPool for creating and recycling game objects. /// public abstract class GameObjectCreator { /// /// Creates a GameObject for the GameObjectPool. The position and rotation of the GameObject /// is set by the GameObjectPool when GetGameObject is called. /// /// An instantiated GameObject. public abstract GameObject Instantiate(); /// /// Called when the GameObject is about to be recycled by the GameObjectPool. This allows you to potentially free /// up any resources before it is deactivated by the GameObjectPool. If the GameObject has a component that implements /// the IGameObjectCreatorHandler interface, it will call its PrepareForRecycle function. /// /// The GameObject that is about to be recycled. public virtual void PrepareForRecycle(GameObject obj) { var components = obj.GetComponents(); foreach (var component in components) { if (component is IGameObjectCreatorListener) { (component as IGameObjectCreatorListener).PrepareForRecycle(); } } } /// /// Called before the GameObject's position and rotation are set (as well as its active state) by the GameObjectPool /// when GetGameObject is called. If the GameObject has a component that implements /// the IGameObjectCreatorHandler interface, it will call its PrepareForUse function. /// /// The GameObject that is about to be used. public virtual void PrepareForUse(GameObject obj) { var components = obj.GetComponents(); foreach (var component in components) { if (component is IGameObjectCreatorListener) { (component as IGameObjectCreatorListener).PrepareForUse(); } } } } }