Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Last active July 4, 2017 10:31
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 AngryAnt/65b1289fe0af88c6e5fce7f3001bd0c8 to your computer and use it in GitHub Desktop.
Save AngryAnt/65b1289fe0af88c6e5fce7f3001bd0c8 to your computer and use it in GitHub Desktop.
Example source for post "CoreObject" at http://angryant.com/2017/07/04/CoreObject/
using UnityEngine;
public class CoreCharacterInstance : MonoBehaviour
{
[SerializeField] CoreCharacter m_CoreCharacter;
[SerializeField] Character m_Character;
void Reset ()
{
m_Character = GetComponent<Character> ();
}
void Awake ()
{
m_CoreCharacter.Register (m_Character);
}
}
using UnityEngine;
[CreateAssetMenu(fileName = "New core GameObject.asset", menuName = "New core GameObject")]
public class CoreGameObject : CoreObject<GameObject>
{}
using UnityEngine;
public class CoreGameObjectInstance : MonoBehaviour
{
[SerializeField] CoreGameObject m_CoreGameObject;
void Awake ()
{
m_CoreGameObject.Register (gameObject);
}
}
using UnityEngine;
using System;
public abstract class CoreObject<T> : ScriptableObject where T : class
{
WeakReference m_Instance;
public T Instance
{
get
{
if (m_Instance == null)
{
return null;
}
if (m_Instance.Target == null)
{
return (T)(m_Instance.Target = null);
}
return (T)m_Instance.Target;
}
}
public void Register (T instance)
{
if (m_Instance == null)
{
m_Instance = new WeakReference (instance);
}
else
{
m_Instance.Target = instance;
}
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField] CoreGameObject m_TheObject;
void Start ()
{
m_TheObject.Instance.SetActive (false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment