Rotorz Tile System Icon
Rotorz Tile System for Unity
API Reference
Available on the Unity Asset Store
TOC

IObjectFactory Interface

Interface for an object creator which can be implemented and utilised to override the way in which game objects are created and destroyed.
public interface IObjectFactory
public interface IObjectFactory

Methods

DestroyObject(GameObject, IObjectFactoryContext)
Destroy object instance.
InstantiatePrefab(GameObject, IObjectFactoryContext)
Create object instance from prefab.

Remarks

You could implement a custom object factory to take control of the way objects are instantiated and destroyed. This might be useful in situations where you would like to implement some sort of pooling to recycle discarded objects instead of destroying them.

RtsPoolManagerObjectFactory is an open-source script which adds support for the PoolManager asset by Path-o-logical Games.

These interfaces are useful for providing custom implementations at runtime, but it is also possible to customize the way in which objects are created and destroyed in the editor, though generally for different reasons. For example, implementing a custom object factory for the editor is a useful hook into the tile painting process.

Custom object factories can be activated:

Examples

The default runtime object factory is implemented as follows:

using Rotorz.Tile;
using UnityEngine;

public sealed class DefaultRuntimeObjectFactory : IObjectFactory {
    public GameObject InstantiatePrefab(GameObject prefab, IObjectFactoryContext context) {
        return Object.Instantiate(prefab) as GameObject;
    }
    public void DestroyObject(GameObject go, IObjectFactoryContext context) {
        Object.Destroy(go);
    }
}
#pragma strict
import Rotorz.Tile;

class DefaultRuntimeObjectFactory extends MonoBehaviour implements IObjectFactory {
    function InstantiatePrefab(prefab:GameObject, context:IObjectFactoryContext) {
        return Instantiate(prefab);
    }
    function DestroyObject(go:GameObject, context:IObjectFactoryContext) {
        Object.Destroy(go);
    }
}

The default design-time object factory is implemented as follows:

using Rotorz.Tile;
using UnityEngine;

public sealed class DefaultEditorObjectFactory : IObjectFactory {
    public GameObject InstantiatePrefab(GameObject prefab, IObjectFactoryContext context) {
        return PrefabUtility.InstantiatePrefab(prefab) as GameObject;
    }
    public void DestroyObject(GameObject go, IObjectFactoryContext context) {
        Object.DestroyImmediate(go);
    }
}
#pragma strict
import Rotorz.Tile;

class DefaultEditorObjectFactory extends MonoBehaviour implements IObjectFactory {
    function InstantiatePrefab(prefab:GameObject, context:IObjectFactoryContext) {
        return PrefabUtility.InstantiatePrefab(prefab);
    }
    function DestroyObject(go:GameObject, context:IObjectFactoryContext) {
        Object.DestroyImmediate(go);
    }
}

See Also

Reference