Implementations of select Unity Scripting Tutorials, complete with basic Scenes
Unity Scripting Tutorials (Videos)
INTERMEDIATE GAMEPLAY SCRIPTING
How to create, implement, and call an extension method.
- Extension Methods are a way of adding functionality to a type:
- without having to create a derived type.
- without changing the original type.
- Perfect for occasions when you cannot edit the original type's source. (e.g. Components)
- Extension methods must be placed in a non-generic
static
class. - Extension methods are used liked instance methods, but they too are
static
. - Use of the
this
keyword in the parameters makes the static method an extension method.- The variable following
this
denotes which class the method becomes an extension of. - e.g.
public static void ResetTransformation(this Transform trans)
- The variable following
How to create coroutines and use them to achieve complex behaviors.
- Coroutines are functions that execute in intervals.
How to create and use delegates to provide complex and dynamic functionality in your scripts.
- Delegates can be thought of simply as containers for/pointers to functions.
- Delegates can be passed around or used like a variable.
- Delegates can have values assigned to them and be changed at runtime.
- Delegates contain functions, just as variables contain data.
- Delegates also have the ability to be Multicast:
- use a single delegate variable to represent multiple methods at the same time.
Attributes allow you to attach additional behavior to the methods and variables you create.
In this video you will learn the format of attributes as well as how to use the [Range(min, max)]
and [ExecuteInEditMode]
attributes.
How to create a dynamic "broadcast"/"messaging" system using C# Publisher/Subscriber Delegate Events.
- Events are useful for alerting other classes that something has happened.
- Events function very similar to public multicast delegates.
- Events are just specialized delegates.
- Events are used instead of delegates because they have inherent security.
- Events only allow other classes to Subscribe/Unsubscribe.
- Events also prevent overriding.
Custom inspectors allow you to add a lot of power and flexibility to your workflow. In this video you will learn about the benefits of custom inspectors as well as how to build them.
- Unity requires that you place custom editor scripts in an "Editor" folder.
- Scripts must import
using UnityEditor;
namespace. - Class definition must have attribute
[CustomEditor(typeof(YourScript))]
. - Scripts must implement
public override void OnInspectorGUI()
which is called everytime the Inspector is drawn.
The DrawDefaultInspector function allows us to easily recreate the default inspector for a script inside a custom inspector. This is very useful if we only want to add new items to the inspector for a script instead of changing the currently existing items. In this video you will learn how to use the DrawDefaultInspector function.
- Useful for only adding new items to the default Inspector view of a script.
In Unity we can add buttons to editor windows so that we can call our functions from our scripts. Doing so give us the ability to write scripts specifically to improve our workflow. In this video you will learn how to create buttons in a custom inspector and how to tie them to functions that exist in our scripts.
- Useful for improving workflow by automating tedious tasks with a single click of a button.
UnityEvents are a way of allowing user driven callback to be persisted from edit time to run time without the need for additional programming and script configuration.
Unity Manual Entry /
Scripting API Entry for UnityEvent
- UnityEvents are a way to hook up function calls between GameObjects.
- UnityEvents can be edited in the Inspector, serialized, and be given default values.
- The
UnityEvent
class withinUnityEngine.Events
is the way of exposing callbacks on an object in a way that can be modified through the editor. - UnityEvents can also do what C#
delegate events
do:- Be populated with function calls at run-time via
UnityEvent.AddListener()
.
- Be populated with function calls at run-time via
- UnityEvents are useful for a number of things:
- Content driven callbacks
- Decoupling systems
- Persistent callbacks
- Preconfigured call events
- By default a
UnityEvent
in aMonoBehaviour
binds dynamically to avoid
function. - Dynamic invocation of UnityEvents supports binding to functions with up to 4 arguments.
- To do this you need to define a custom
UnityEvent
class that supports multiple arguments. - This is quite easy to do:
[Serializable]
public class StringEvent : UnityEvent <string> {}
- By adding an instance of this to your class instead of the base
UnityEvent
it will allow the callback to bind dynamically to string functions. - This can then be invoked by calling the
Invoke()
function with astring
as argument. - UnityEvents can be defined with up to 4 arguments in their generic definition.