Skip to content

Instantly share code, notes, and snippets.

@alvivar
Last active August 29, 2015 14:15
Show Gist options
  • Save alvivar/cd5a0e48d2f23985a0c8 to your computer and use it in GitHub Desktop.
Save alvivar/cd5a0e48d2f23985a0c8 to your computer and use it in GitHub Desktop.
Liteprint: Extension set for a quick & semiautomatic data pool for transform objects.
// Liteprint v0.1 alpha
// Extension set for a quick & semiautomatic data pool for transform objects.
// Just put '.lit' after any transform to access his powers.
// - .litCreate( Prepares & fill a pool for the current transform (optional).
// - .litSpawn( Returns a clone from the pool based on the current transform (Instantiate-like).
// - .litRecycle() Put back the current clone to his pool for reuse.
// - .litFlush() Cleans & destroy all pool elements for the current transform.
// Created by Andrés Villalobos [[email protected]] [twitter.com/matnesis]
// 14/02/2015 4:21 pm
// Copyright (c) 14/02/2015 [email protected]
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Extension set for a quick & semiautomatic data pool for transform objects.
/// </summary>
public static class Liteprint
{
private static Dictionary<Transform, List<Transform>> readyPool;
private static Dictionary<Transform, Transform> outPool;
private static void PrepareInternalDictionaries(Transform instance)
{
// Dictionaries
if (readyPool == null)
readyPool = new Dictionary<Transform, List<Transform>>();
if (outPool == null)
outPool = new Dictionary<Transform, Transform>();
// Pool lists
if (readyPool.ContainsKey(instance) == false)
readyPool[instance] = new List<Transform>();
if (readyPool[instance] == null)
readyPool[instance] = new List<Transform>();
}
private static Transform GetParent(Transform from)
{
string parentName = "[Liteprint::" + from.name + "." + from.GetInstanceID() + "]";
GameObject parent = GameObject.Find(parentName);
if (parent == null)
parent = new GameObject(parentName);
return parent.transform;
}
/// <summary>
/// Prepares & fill a pool for the current transform.
/// </summary>
public static void litCreatePool(this Transform instance, int quantity)
{
PrepareInternalDictionaries(instance);
Vector3 pos = instance.position;
while (quantity-- > 0)
{
Transform newClone = MonoBehaviour.Instantiate(instance, pos + new Vector3(-9999, -9999, -9999), Quaternion.identity) as Transform;
newClone.parent = GetParent(instance);
readyPool[instance].Add(newClone);
}
}
/// <summary>
/// Returns a clone from the pool based on the current transform (Instantiate-like).
/// </summary>
public static Transform litSpawn(this Transform instance, Vector3 position, Quaternion rotation)
{
PrepareInternalDictionaries(instance);
// If not enough, create more
if (readyPool[instance].Count < 1)
instance.litCreatePool(2);
// First on the pool
Transform spawn = readyPool[instance][0];
// Skip nulls & retry
if (spawn == null)
{
readyPool[instance].RemoveAt(0);
return instance.litSpawn(position, rotation);
}
// Allocation
spawn.parent = GetParent(instance);
spawn.position = position;
spawn.rotation = rotation;
// Pool swap
readyPool[instance].RemoveAt(0);
outPool[spawn] = instance;
return spawn;
}
/// <summary>
/// Put back the current clone to his pool for reuse.
/// </summary>
public static bool litRecycle(this Transform instance)
{
PrepareInternalDictionaries(instance);
if (outPool.ContainsKey(instance) == true)
{
readyPool[outPool[instance]].Add(instance);
return true;
}
return false;
}
/// <summary>
/// Cleans & destroy all pool elements for the current transform.
/// </summary>
public static void litFlush(this Transform instance)
{
if (readyPool.ContainsKey(instance))
{
foreach (Transform t in readyPool[instance])
{
if (outPool.ContainsKey(t))
outPool.Remove(t);
MonoBehaviour.Destroy(t.gameObject);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment