Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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