Skip to content

Instantly share code, notes, and snippets.

@mattak
Created May 9, 2016 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattak/b49bb8066f285123f963703c9f33a060 to your computer and use it in GitHub Desktop.
Save mattak/b49bb8066f285123f963703c9f33a060 to your computer and use it in GitHub Desktop.
using UnityEngine;
namespace App
{
public class CameraFinder : MonoBehaviour
{
public Camera DisposeCamera;
private Canvas _canvas;
// Use this for initialization
void Start()
{
this._canvas = this.GetComponent<Canvas>();
if (Camera.main != null
&& this.DisposeCamera != null
&& !System.Object.ReferenceEquals(this.DisposeCamera, Camera.main))
{
this._canvas.worldCamera = Camera.main;
Destroy(this.DisposeCamera.gameObject);
}
}
}
}
using UnityEngine.SceneManagement;
namespace App
{
public class SceneManager : SingletonMonoBehaviour<SceneManager>
{
public void Replace(string name)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(name, LoadSceneMode.Single);
}
public void Hover(string name)
{
if (!UnityEngine.SceneManagement.SceneManager.GetSceneByName(name).isLoaded)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(name, LoadSceneMode.Additive);
}
}
public void Dispose(string name)
{
if (UnityEngine.SceneManagement.SceneManager.GetSceneByName(name).isLoaded)
{
UnityEngine.SceneManagement.SceneManager.UnloadScene(name);
}
}
}
}
using UnityEngine;
namespace App
{
public class SingletonMonoBehaviour<T> : MonoBehaviour where T : SingletonMonoBehaviour<T>
{
protected static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = (T) FindObjectOfType(typeof(T));
if (instance == null)
{
Debug.LogWarning(typeof(T) + "is nothing");
}
}
return instance;
}
}
protected void Awake()
{
CheckInstance();
}
protected bool CheckInstance()
{
if (instance == null)
{
instance = (T) this;
return true;
}
else if (Instance == this)
{
return true;
}
Destroy(this);
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment