Skip to content

Commit

Permalink
removed all Unity 4.x code
Browse files Browse the repository at this point in the history
switched over to SceneManager
  • Loading branch information
prime31 committed Nov 3, 2016
1 parent 029d14b commit b54a7f4
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 54 deletions.
49 changes: 25 additions & 24 deletions 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
{
/// <summary>
/// access to the singleton
Expand Down Expand Up @@ -32,20 +33,20 @@ public partial class TrashMan : MonoBehaviour
/// <summary>
/// uses the GameObject instanceId as its key for fast look-ups
/// </summary>
private Dictionary<int,TrashManRecycleBin> _instanceIdToRecycleBin = new Dictionary<int,TrashManRecycleBin>();
Dictionary<int,TrashManRecycleBin> _instanceIdToRecycleBin = new Dictionary<int,TrashManRecycleBin>();

/// <summary>
/// uses the pool name to find the GameObject instanceId
/// </summary>
private Dictionary<string,int> _poolNameToInstanceId = new Dictionary<string,int>();
Dictionary<string,int> _poolNameToInstanceId = new Dictionary<string,int>();

[HideInInspector]
public new Transform transform;


#region MonoBehaviour

private void Awake()
void Awake()
{
if( instance != null )
{
Expand All @@ -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 )
Expand All @@ -77,7 +83,7 @@ private void OnLevelWasLoaded()
}


private void OnApplicationQuit()
void OnApplicationQuit()
{
instance = null;
}
Expand All @@ -91,7 +97,7 @@ private void OnApplicationQuit()
/// coroutine that runs every couple seconds and removes any objects created over the recycle bins limit
/// </summary>
/// <returns>The excess objects.</returns>
private IEnumerator cullExcessObjects()
IEnumerator cullExcessObjects()
{
var waiter = new WaitForSeconds( cullExcessObjectsInterval );

Expand All @@ -108,7 +114,7 @@ private IEnumerator cullExcessObjects()
/// <summary>
/// populats the lookup dictionaries
/// </summary>
private void initializePrefabPools()
void initializePrefabPools()
{
if( recycleBinCollection == null )
return;
Expand All @@ -129,7 +135,7 @@ private void initializePrefabPools()
/// internal method that actually does the work of grabbing the item from the bin and returning it
/// </summary>
/// <param name="gameObjectInstanceId">Game object instance identifier.</param>
private static GameObject spawn( int gameObjectInstanceId, Vector3 position, Quaternion rotation )
static GameObject spawn( int gameObjectInstanceId, Vector3 position, Quaternion rotation )
{
if( instance._instanceIdToRecycleBin.ContainsKey( gameObjectInstanceId ) )
{
Expand All @@ -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;
Expand All @@ -165,7 +169,7 @@ private static GameObject spawn( int gameObjectInstanceId, Vector3 position, Qua
/// <returns>The despawn after delay.</returns>
/// <param name="go">Go.</param>
/// <param name="delayInSeconds">Delay in seconds.</param>
private IEnumerator internalDespawnAfterDelay( GameObject go, float delayInSeconds )
IEnumerator internalDespawnAfterDelay( GameObject go, float delayInSeconds )
{
yield return new WaitForSeconds( delayInSeconds );
despawn( go );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -323,8 +323,9 @@ public static TrashManRecycleBin recycleBinForGameObjectName( string gameObjectN
/// <param name="go">Go.</param>
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;
}

Expand Down
6 changes: 5 additions & 1 deletion Assets/RecyclerKit/TrashMan.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 9 additions & 12 deletions Assets/RecyclerKit/TrashManRecycleBin.cs
Expand Up @@ -72,17 +72,17 @@ public sealed class TrashManRecycleBin
/// <summary>
/// stores all of our GameObjects
/// </summary>
private Stack<GameObject> _gameObjectPool;
Stack<GameObject> _gameObjectPool;

/// <summary>
/// last time culling happened
/// </summary>
private float _timeOfLastCull = float.MinValue;
float _timeOfLastCull = float.MinValue;

/// <summary>
/// keeps track of the total number of instances spawned
/// </summary>
private int _spawnedInstanceCount = 0;
int _spawnedInstanceCount = 0;


#region Private
Expand All @@ -91,7 +91,7 @@ public sealed class TrashManRecycleBin
/// allocates
/// </summary>
/// <param name="count">Count.</param>
private void allocateGameObjects( int count )
void allocateGameObjects( int count )
{
if( imposeHardLimit && _gameObjectPool.Count + count > hardLimit )
count = hardLimit - _gameObjectPool.Count;
Expand All @@ -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 );
}
Expand All @@ -118,7 +115,7 @@ private void allocateGameObjects( int count )
/// <summary>
/// pops an object off the stack. Returns null if we hit the hardLimit.
/// </summary>
private GameObject pop()
GameObject pop()
{
if( imposeHardLimit && _spawnedInstanceCount >= hardLimit )
return null;
Expand Down
Binary file modified Assets/RecyclerKitDemo/DemoScene.unity
Binary file not shown.
31 changes: 15 additions & 16 deletions 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
Expand All @@ -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()
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
var cs = canvasRoot.AddComponent<CanvasScaler>();
Expand All @@ -111,22 +110,22 @@ void CreateCanvas()
cs.referenceResolution = new Vector2(800,600);

//create our ui prefab
uiPrefab = new GameObject("UItxt");
uiPrefab.transform.position = new Vector3(1000,10000);
uiPrefab = new GameObject( "UItext" );
uiPrefab.transform.position = new Vector3( 1000, 10000 );
var txt = uiPrefab.AddComponent<Text>();
txt.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
txt.font = Resources.GetBuiltinResource( typeof( Font ), "Arial.ttf" ) as Font;
txt.text = "Some text";
txt.horizontalOverflow = HorizontalWrapMode.Overflow;
txt.color = Color.white;
txt.resizeTextForBestFit = true;

//Make a recycle bin for it
// Make a recycle bin for it
var recycleBin = new TrashManRecycleBin()
{
prefab = uiPrefab
};
TrashMan.manageRecycleBin( recycleBin );
}
}
#endif

}
Binary file added ProjectSettings/ClusterInputManager.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectVersion.txt
@@ -1,2 +1,2 @@
m_EditorVersion: 5.0.2f1
m_EditorVersion: 5.4.2f2
m_StandardAssetsVersion: 0
Binary file added ProjectSettings/UnityAdsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/UnityConnectSettings.asset
Binary file not shown.

0 comments on commit b54a7f4

Please sign in to comment.