MonoEvent Primer

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "Coming soon...")
 
Line 1: Line 1:
Coming soon...
+
= 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:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
using UnityEngine;
 +
using System.Collections;
 +
using BSGTools.Events;
 +
 
 +
public class EditQuadEvent : MonoEvent {
 +
 
 +
internal override IEnumerator[] InitEvent() {
 +
throw new System.NotImplementedException();
 +
}
 +
 
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
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:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
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);
 +
}
 +
}
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
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:
 +
 
 +
<syntaxhighlight lang="csharp">
 +
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);
 +
}
 +
}
 +
}
 +
</syntaxhighlight>

Revision as of 17:08, 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);
		}
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox