MonoEvent Primer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Delays And Additional Tasks)
(Delays And Additional Tasks)
Line 115: Line 115:
 
MonoEvent comes with 2 prebuilt coroutines for delaying, one of which is timescale independent.
 
MonoEvent comes with 2 prebuilt coroutines for delaying, one of which is timescale independent.
  
Create two more coroutines, '''MoveLeft()''' and '''MoveUp()'''. Configure them so that they move the quad to the left several units and up several units, respectively. Finally, using '''Delay''' and '''ScaledDelay''', change the return array of '''InitEvent()''' so that the coroutines are executed in the proper order. Note that since we changed the time scale, and that WaitForSeconds is time scale dependent, we need to adjust for this in the '''ChangeColorRandom''' coroutine:
+
Create two more coroutines, '''MoveLeft()''' and '''MoveUp()'''. Configure them so that they move the quad to the left several units and up several units, respectively. Finally, using '''Delay''' and '''ScaledDelay''', change the return array of '''InitEvent()''' so that the coroutines are executed in the proper order. Note that since we changed the time scale, and because WaitForSeconds is time scale dependent, we need to adjust for this in the '''ChangeColorRandom''' coroutine:
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">

Revision as of 17:56, 4 June 2014

Contents

MonoEvent Primer

Initial Notes

  • This tutorial is NOT a Unity tutorial nor a primer to C# or programming in general. This tutorial assumes that you already have a good grasp of how Unity works and that you have intimate knowledge of programming concepts and the C# language.
  • BSGTools is C# ONLY. Unityscript and Boo are NOT supported.
  • MonoEvent was designed for programmers, by a programmer. It has limited configurable options in the Inspector, and the base script should not be manually added to a GameObject for any reason. Nearly all configuration is done in code.


Setting Up The Scene

The content of our scene is really simple:

  • A quad primitive, at (0,0,0)
  • An orthographic camera, at (0,0,-10)
  • A directional light, at (0,0,-10)


Creating The MonoEvent

Create a new script called EditQuadEvent and add it to the camera. Delete the default MonoBehaviour methods and make the base type MonoEvent. You're required to fully implement the base abstract class, and this involves overriding the InitEvent() method:

using UnityEngine;
using System.Collections;
using BSGTools.Events;
 
public class EditQuadEvent : MonoEvent {
 
	internal override IEnumerator[] InitEvent() {
		throw new System.NotImplementedException();
	}
 
}


We'll implement the actual functionality of this in a little bit.


Creating The Initial Tasks

Our first goal is to change the quad's renderer's color to a random color multiple times with delays in-between. Add serialized private renderer field so that we can assign this in the inspector. Also, create a new coroutine method called ChangeColorRandom(), with a return type of IEnumerator:

using UnityEngine;
using System.Collections;
using BSGTools.Events;
 
public class EditQuadEvent : MonoEvent {
	[SerializeField]
	private Renderer quadRenderer;
	[SerializeField]
	private int changeCount = 5;
	[SerializeField]
	private float delayBetweenChanges = 1f;
 
	internal override IEnumerator[] InitEvent() {
		throw new System.NotImplementedException();
	}
 
 
	private IEnumerator ChangeColorRandom(int changeCount, float delayBetweenChanges) {
		for(int i = 0; i < changeCount; i++) {
			quadRenderer.material.color = new Color(Random.value, Random.value, Random.value);
			yield return new WaitForSeconds(delayBetweenChanges);
		}
	}
}


We need to provide this coroutine to the base class for execution. To do this, change InitEvent() so that it returns an IEnumerator array with ChangeColorRandom as an array element:

using UnityEngine;
using System.Collections;
using BSGTools.Events;
 
public class EditQuadEvent : MonoEvent {
	[SerializeField]
	private Renderer quadRenderer;
	[SerializeField]
	private int changeCount = 5;
	[SerializeField]
	private float delayBetweenChanges = 1f;
 
	internal override IEnumerator[] InitEvent() {
		return new IEnumerator[] { ChangeColorRandom(changeCount, delayBetweenChanges) };
	}
 
	private IEnumerator ChangeColorRandom(int changeCount, float delayBetweenChanges) {
		for(int i = 0; i < changeCount; i++) {
			quadRenderer.material.color = new Color(Random.value, Random.value, Random.value);
			yield return new WaitForSeconds(delayBetweenChanges);
		}
	}
}


Return to Unity, and enable the option titled "Execute On Start". Now run your scene to see the quad change colors.


Delays And Additional Tasks

Set your projects time scale to a value of (0.5).

Our next goal is to have our event do the following:

  • Change the color of the quad multiple times with delays in-between.
  • Wait for 0.5 second, independent from time scale.
  • Move our quad over several units to the left (-x).
  • Wait for 0.5 second, affected by time scale.
  • Move our quad several units up (+y).
  • Execute one more half-second, time scale independent delay and then loop.


MonoEvent comes with 2 prebuilt coroutines for delaying, one of which is timescale independent.

Create two more coroutines, MoveLeft() and MoveUp(). Configure them so that they move the quad to the left several units and up several units, respectively. Finally, using Delay and ScaledDelay, change the return array of InitEvent() so that the coroutines are executed in the proper order. Note that since we changed the time scale, and because WaitForSeconds is time scale dependent, we need to adjust for this in the ChangeColorRandom coroutine:

using UnityEngine;
using System.Collections;
using BSGTools.Events;
 
public class EditQuadEvent : MonoEvent {
	[SerializeField]
	private Renderer quadRenderer;
	[SerializeField]
	private int changeCount = 5;
	[SerializeField]
	private float delayBetweenChanges = 1f;
 
	//Delay and ScaledDelay are declared in the MonoEvent
	//base class, there is no need to create these methods yourself.
	internal override IEnumerator[] InitEvent() {
		return new IEnumerator[] { ChangeColorRandom(changeCount, delayBetweenChanges), Delay(0.5f), MoveLeft(), ScaledDelay(0.5f), MoveUp(), Delay(0.5f) };
	}
 
	private IEnumerator MoveUp() {
		var newPosition = quadRenderer.transform.position;
		newPosition.y += 3f;
		quadRenderer.transform.position = newPosition;
		yield return null;
	}
 
	private IEnumerator MoveLeft() {
		var newPosition = quadRenderer.transform.position;
		newPosition.x -= 3f;
		quadRenderer.transform.position = newPosition;
		yield return null;
	}
 
	private IEnumerator ChangeColorRandom(int changeCount, float delayBetweenChanges) {
		for(int i = 0; i < changeCount; i++) {
			quadRenderer.material.color = new Color(Random.value, Random.value, Random.value);
			yield return new WaitForSeconds(delayBetweenChanges * Time.timeScale);
		}
	}
}


Return to Unity, and enable the option titled "Loop". Now run your scene to see the quad... do exactly what we told it to do.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox