diff --git a/Assets/RecyclerKit/TrashMan.cs b/Assets/RecyclerKit/TrashMan.cs
index 79c4ae6..378e375 100755
--- a/Assets/RecyclerKit/TrashMan.cs
+++ b/Assets/RecyclerKit/TrashMan.cs
@@ -1,10 +1,11 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
+using UnityEngine.SceneManagement;
-public partial class TrashMan : MonoBehaviour
+public class TrashMan : MonoBehaviour
{
///
/// access to the singleton
@@ -32,12 +33,12 @@ public partial class TrashMan : MonoBehaviour
///
/// uses the GameObject instanceId as its key for fast look-ups
///
- private Dictionary _instanceIdToRecycleBin = new Dictionary();
+ Dictionary _instanceIdToRecycleBin = new Dictionary();
///
/// uses the pool name to find the GameObject instanceId
///
- private Dictionary _poolNameToInstanceId = new Dictionary();
+ Dictionary _poolNameToInstanceId = new Dictionary();
[HideInInspector]
public new Transform transform;
@@ -45,7 +46,7 @@ public partial class TrashMan : MonoBehaviour
#region MonoBehaviour
- private void Awake()
+ void Awake()
{
if( instance != null )
{
@@ -64,11 +65,16 @@ private void Awake()
// only cull if we have an interval greater than 0
if( cullExcessObjectsInterval > 0 )
StartCoroutine( cullExcessObjects() );
+
+ SceneManager.activeSceneChanged += activeSceneChanged;
}
- private void OnLevelWasLoaded()
+ void activeSceneChanged( Scene oldScene, Scene newScene )
{
+ if( oldScene.name == null )
+ return;
+
for( var i = recycleBinCollection.Count - 1; i >= 0; i-- )
{
if( !recycleBinCollection[i].persistBetweenScenes )
@@ -77,7 +83,7 @@ private void OnLevelWasLoaded()
}
- private void OnApplicationQuit()
+ void OnApplicationQuit()
{
instance = null;
}
@@ -91,7 +97,7 @@ private void OnApplicationQuit()
/// coroutine that runs every couple seconds and removes any objects created over the recycle bins limit
///
/// The excess objects.
- private IEnumerator cullExcessObjects()
+ IEnumerator cullExcessObjects()
{
var waiter = new WaitForSeconds( cullExcessObjectsInterval );
@@ -108,7 +114,7 @@ private IEnumerator cullExcessObjects()
///
/// populats the lookup dictionaries
///
- private void initializePrefabPools()
+ void initializePrefabPools()
{
if( recycleBinCollection == null )
return;
@@ -129,7 +135,7 @@ private void initializePrefabPools()
/// internal method that actually does the work of grabbing the item from the bin and returning it
///
/// Game object instance identifier.
- private static GameObject spawn( int gameObjectInstanceId, Vector3 position, Quaternion rotation )
+ static GameObject spawn( int gameObjectInstanceId, Vector3 position, Quaternion rotation )
{
if( instance._instanceIdToRecycleBin.ContainsKey( gameObjectInstanceId ) )
{
@@ -139,11 +145,9 @@ private static GameObject spawn( int gameObjectInstanceId, Vector3 position, Qua
{
var newTransform = newGo.transform;
-#if UNITY_4_6 || UNITY_5_0
- if (newTransform as RectTransform)
- newTransform.SetParent(null, false);
+ if( newTransform as RectTransform )
+ newTransform.SetParent( null, false );
else
-#endif
newTransform.parent = null;
newTransform.position = position;
@@ -165,7 +169,7 @@ private static GameObject spawn( int gameObjectInstanceId, Vector3 position, Qua
/// The despawn after delay.
/// Go.
/// Delay in seconds.
- private IEnumerator internalDespawnAfterDelay( GameObject go, float delayInSeconds )
+ IEnumerator internalDespawnAfterDelay( GameObject go, float delayInSeconds )
{
yield return new WaitForSeconds( delayInSeconds );
despawn( go );
@@ -231,11 +235,9 @@ public static GameObject spawn( GameObject go, Vector3 position = default( Vecto
Debug.LogWarning( "attempted to spawn go (" + go.name + ") but there is no recycle bin setup for it. Falling back to Instantiate" );
var newGo = GameObject.Instantiate( go, position, rotation ) as GameObject;
-#if UNITY_4_6 || UNITY_5_0
- if (newGo.transform as RectTransform != null)
- newGo.transform.SetParent(null, false);
+ if( newGo.transform as RectTransform != null )
+ newGo.transform.SetParent( null, false );
else
-#endif
newGo.transform.parent = null;
return newGo;
@@ -279,11 +281,9 @@ public static void despawn( GameObject go )
{
instance._instanceIdToRecycleBin[instance._poolNameToInstanceId[goName]].despawn( go );
-#if UNITY_4_6 || UNITY_5_0
- if (go.transform as RectTransform != null)
- go.transform.SetParent(instance.transform, false);
+ if( go.transform as RectTransform != null )
+ go.transform.SetParent( instance.transform, false );
else
-#endif
go.transform.parent = instance.transform;
}
}
@@ -323,8 +323,9 @@ public static TrashManRecycleBin recycleBinForGameObjectName( string gameObjectN
/// Go.
public static TrashManRecycleBin recycleBinForGameObject( GameObject go )
{
- if( instance._instanceIdToRecycleBin.ContainsKey( go.GetInstanceID() ) )
- return instance._instanceIdToRecycleBin[go.GetInstanceID()];
+ TrashManRecycleBin recycleBin;
+ if( instance._instanceIdToRecycleBin.TryGetValue( go.GetInstanceID(), out recycleBin ) )
+ return recycleBin;
return null;
}
diff --git a/Assets/RecyclerKit/TrashMan.cs.meta b/Assets/RecyclerKit/TrashMan.cs.meta
index f91b1db..ee8c83d 100644
--- a/Assets/RecyclerKit/TrashMan.cs.meta
+++ b/Assets/RecyclerKit/TrashMan.cs.meta
@@ -1,8 +1,12 @@
fileFormatVersion: 2
guid: 9ddf7a5150a474960a78be74651f1a64
+timeCreated: 1478211320
+licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
- executionOrder: 0
+ executionOrder: -100
icon: {instanceID: 0}
userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/RecyclerKit/TrashManRecycleBin.cs b/Assets/RecyclerKit/TrashManRecycleBin.cs
index 6802214..b357720 100755
--- a/Assets/RecyclerKit/TrashManRecycleBin.cs
+++ b/Assets/RecyclerKit/TrashManRecycleBin.cs
@@ -72,17 +72,17 @@ public sealed class TrashManRecycleBin
///
/// stores all of our GameObjects
///
- private Stack _gameObjectPool;
+ Stack _gameObjectPool;
///
/// last time culling happened
///
- private float _timeOfLastCull = float.MinValue;
+ float _timeOfLastCull = float.MinValue;
///
/// keeps track of the total number of instances spawned
///
- private int _spawnedInstanceCount = 0;
+ int _spawnedInstanceCount = 0;
#region Private
@@ -91,7 +91,7 @@ public sealed class TrashManRecycleBin
/// allocates
///
/// Count.
- private void allocateGameObjects( int count )
+ void allocateGameObjects( int count )
{
if( imposeHardLimit && _gameObjectPool.Count + count > hardLimit )
count = hardLimit - _gameObjectPool.Count;
@@ -100,15 +100,12 @@ private void allocateGameObjects( int count )
{
GameObject go = GameObject.Instantiate( prefab.gameObject ) as GameObject;
go.name = prefab.name;
-#if UNITY_4_6 || UNITY_5_0
- if(go.transform as RectTransform)
- {
- go.transform.SetParent(TrashMan.instance.transform, false);
- }
+ if( go.transform as RectTransform )
+ go.transform.SetParent( TrashMan.instance.transform, false );
else
-#endif
- go.transform.parent = TrashMan.instance.transform;
+ go.transform.parent = TrashMan.instance.transform;
+
go.SetActive( false );
_gameObjectPool.Push( go );
}
@@ -118,7 +115,7 @@ private void allocateGameObjects( int count )
///
/// pops an object off the stack. Returns null if we hit the hardLimit.
///
- private GameObject pop()
+ GameObject pop()
{
if( imposeHardLimit && _spawnedInstanceCount >= hardLimit )
return null;
diff --git a/Assets/RecyclerKitDemo/DemoScene.unity b/Assets/RecyclerKitDemo/DemoScene.unity
index 6cd1b9a..cd0bbbf 100644
Binary files a/Assets/RecyclerKitDemo/DemoScene.unity and b/Assets/RecyclerKitDemo/DemoScene.unity differ
diff --git a/Assets/RecyclerKitDemo/DemoUI.cs b/Assets/RecyclerKitDemo/DemoUI.cs
index 3acb166..8986c80 100644
--- a/Assets/RecyclerKitDemo/DemoUI.cs
+++ b/Assets/RecyclerKitDemo/DemoUI.cs
@@ -1,8 +1,6 @@
using UnityEngine;
using System.Collections;
-#if UNITY_4_6 || UNITY_5_0
using UnityEngine.UI;
-#endif
public class DemoUI : MonoBehaviour
@@ -12,12 +10,11 @@ public class DemoUI : MonoBehaviour
public GameObject spherePrefab;
public GameObject capsulePrefab;
- private bool _didCreateCapsuleRecycleBin;
-#if UNITY_4_6 || UNITY_5_0
- private bool _didCreateUiStuff;
+ bool _didCreateCapsuleRecycleBin;
+ bool _didCreateUiStuff;
GameObject canvasRoot;
GameObject uiPrefab;
-#endif
+
void Start()
{
@@ -66,7 +63,7 @@ void OnGUI()
TrashMan.spawn( "Particles", Random.onUnitSphere * 3f );
}
-#if UNITY_4_6 || UNITY_5_0
+
if( GUILayout.Button( "Spawn UI element" ) )
{
CreateCanvas();
@@ -76,7 +73,8 @@ void OnGUI()
rt.anchoredPosition = new Vector2(Random.Range (-380,380), Random.Range (-280,280));
TrashMan.despawnAfterDelay( go, Random.Range( 1f, 5f ) );
}
-#endif
+
+
if( GUILayout.Button( "Create Recycle Bin at Runtime" ) )
{
_didCreateCapsuleRecycleBin = true;
@@ -95,14 +93,15 @@ void OnGUI()
}
}
-#if UNITY_4_6 || UNITY_5_0
+
void CreateCanvas()
{
- if(!_didCreateUiStuff)
+ if( !_didCreateUiStuff )
{
_didCreateUiStuff = true;
+
//Create the UI canvas game object
- canvasRoot = new GameObject("Canvas");
+ canvasRoot = new GameObject( "Canvas" );
var canvas = canvasRoot.AddComponent