Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
hObjectPool.cs
1 #region Copyright Notice & License Information
2 //
3 // hObjectPool.cs
4 //
5 // Author:
6 // Matthew Davey <matthew.davey@dotbunny.com>
7 //
8 // Copyright (c) 2013 dotBunny Inc. (http://www.dotbunny.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 #endregion
28 
29 using UnityEngine;
30 
31 /// <summary>
32 /// A drop in implementation of the Hydrogen.Core.ObjectPool. This simply makes the class an accessible singleton
33 /// with some very simple additional functionality.
34 /// </summary>
35 [AddComponentMenu ("Hydrogen/Singletons/Object Pool")]
36 public sealed class hObjectPool : Hydrogen.Core.ObjectPool
37 {
38  /// <summary>
39  /// Should this object pool survive scene switches?
40  /// </summary>
41  public bool Persistent = true;
42  /// <summary>
43  /// Internal fail safe to maintain instance across threads.
44  /// </summary>
45  /// <remarks>
46  /// Multithreaded Safe Singleton Pattern.
47  /// </remarks>
48  /// <description>
49  /// http://msdn.microsoft.com/en-us/library/ms998558.aspx
50  /// </description>
51  static readonly System.Object _syncRoot = new System.Object ();
52  /// <summary>
53  /// Internal reference to the static instance of the object pool.
54  /// </summary>
55  static volatile hObjectPool _staticInstance;
56 
57  /// <summary>
58  /// Gets the object pool instance, creating one if none is found.
59  /// </summary>
60  /// <value>
61  /// The Object Pool.
62  /// </value>
63  public static hObjectPool Instance {
64  get {
65  if (_staticInstance == null) {
66  lock (_syncRoot) {
67  _staticInstance = FindObjectOfType (typeof(hObjectPool)) as hObjectPool;
68 
69  // If we don't have it, lets make it!
70  if (_staticInstance == null) {
71  var go = GameObject.Find (Hydrogen.Components.DefaultSingletonName) ??
72  new GameObject (Hydrogen.Components.DefaultSingletonName);
73 
74  go.AddComponent<hObjectPool> ();
75  _staticInstance = go.GetComponent<hObjectPool> ();
76  }
77  }
78  }
79  return _staticInstance;
80  }
81  }
82 
83  /// <summary>
84  /// Does an Object Pool already exist?
85  /// </summary>
86  public static bool Exists ()
87  {
88  return _staticInstance != null;
89  }
90 
91  /// <summary>
92  /// Unity's Awake Event
93  /// </summary>
94  protected override void Awake ()
95  {
96  // Make sure to do the object pools normal initialization
97  base.Awake ();
98 
99  // Should this gameObject be kept around :) I think so.
100  if (Persistent)
101  DontDestroyOnLoad (gameObject);
102 
103  }
104 }
static bool Exists()
Does an Object Pool already exist?
Definition: hObjectPool.cs:86
static hObjectPool Instance
Gets the object pool instance, creating one if none is found.
Definition: hObjectPool.cs:63
override void Awake()
Unity&#39;s Awake Event
Definition: hObjectPool.cs:94
bool Persistent
Should this object pool survive scene switches?
Definition: hObjectPool.cs:41
A drop in implementation of the Hydrogen.Core.ObjectPool. This simply makes the class an accessible s...
Definition: hObjectPool.cs:36