Last active
July 4, 2017 10:31
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[CreateAssetMenu(fileName = "New core GameObject.asset", menuName = "New core GameObject")] | |
public class CoreGameObject : CoreObject<GameObject> | |
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class CoreGameObjectInstance : MonoBehaviour | |
{ | |
[SerializeField] CoreGameObject m_CoreGameObject; | |
void Awake () | |
{ | |
m_CoreGameObject.Register (gameObject); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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