Instantly share code, notes, and snippets.

@turbohermit /Cue.cs
Last active Jun 29, 2018

Embed
What would you like to do?
An inheritance-based messaging system for executing behaviour based on enumerations. The system is designed to improve upon Unity's component based systems, but foregoes the need for reflection on every behaviour you're trying to send messages to, saving tons of performance on projects with a lot of behaviours that interact with each other..
//Add whatever keywords are relevant to your project here.
public enum Cue
{
None,
Spawn,
Destroy,
Stop,
Pause,
Start,
Refresh
}
public class CueBehaviour : MonoBehaviour {
public bool executeOnStart;
public int onStartIndex;
public Cue[] cues;
private void Start()
{
if (executeOnStart)
{
ExecuteBehaviour(onStartIndex);
}
}
public virtual void ExecuteBehaviour(int index)
{
//Subclasses can override this with the desired actions to execute.
}
}
//A GameObject with the CueProcesser component on it can recieve a message, then tells all CueBehaviours in it and its children to process that message.
public class CueProcessor : MonoBehaviour {
public Cue loadBehavioursCue = Cue.Refresh;
protected CueBehaviour[] behaviours;
public void Awake()
{
LoadBehaviours();
}
public virtual void ProcessCue(Cue cue)
{
if (cue != Cue.None)
{
if(cue == loadBehavioursCue)
{
LoadBehaviours();
}
for (int i = 0; i < behaviours.Length; i++)
{
if (behaviours[i] != null)
{
for (int c = 0; c < behaviours[i].cues.Length; c++)
{
if (behaviours[i].cues[c] == cue)
{
behaviours[i].ExecuteBehaviour(c);
}
}
}
}
}
}
public void LoadBehaviours()
{
behaviours = GetComponentsInChildren<CueBehaviour>();
}
}
//An example of how a simple CueBehaviour would look like.
public class EmissionOnCue : CueBehaviour {
public ParticleSystem targetParticleSystem;
public bool[] emit;
private ParticleSystem.EmissionModule module;
void Start()
{
module = targetParticleSystem.emission;
}
//This method is called only if there's a match in cues, called by the CueProcessor.
public override void ExecuteBehaviour(int index)
{
module.enabled = emit[index];
}
}
//An example of what could trigger CuesBehaviours, in this case a Cue is sent after the GameObject this is attached to collides with another.
//It iterates through the objects tag to see what Cue it should send to which CueProcessor.
//This can be used for functions like playing audio, spawning particles, showing or hiding interface when two GameObjects touch each other.
public class SendCueOnCollision : MonoBehaviour {
public CueProcessor[] targetProcessor;
public string[] tagFilter;
public Cue[] cueToSend;
public float[] delayInSeconds;
public IEnumerator FireCue(int index)
{
yield return new WaitForSeconds(delayInSeconds[index]);
if (targetProcessor[index] != null)
{
targetProcessor[index].ProcessCue(cueToSend[index]);
}
}
private void OnTriggerEnter(Collider other) {
for(int i=0;i<tagFilter.Length;i++){
if(other.tag == tagFilter[i]){
StartCoroutine(FireCue(i));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment