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