Skip to content

antfarmar/Unity-Scripting-Tutorials

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Scripting Tutorials

Implementations of select Unity Scripting Tutorials, complete with basic Scenes

Unity Scripting Tutorials (Videos)

Contents

INTERMEDIATE GAMEPLAY SCRIPTING

EDITOR SCRIPTING

OTHER SCRIPTING

INTERMEDIATE GAMEPLAY SCRIPTING

11. Extension Methods

How to create, implement, and call an extension method.

Unity / Youtube

Notes:
  • 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)

14. Coroutines

How to create coroutines and use them to achieve complex behaviors.

Unity / Youtube

Notes:
  • Coroutines are functions that execute in intervals.

16. Delegates

How to create and use delegates to provide complex and dynamic functionality in your scripts.

Unity / Youtube

Notes:
  • 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.

17. Attributes

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.

Unity / Youtube

Notes:

18. Events

How to create a dynamic "broadcast"/"messaging" system using C# Publisher/Subscriber Delegate Events.

Unity / Youtube

Notes:
  • 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.

EDITOR SCRIPTING

01. Building A Custom Inspector

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 / Youtube

Notes:
  • 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.

02. The DrawDefaultInspector Function

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.

Unity / Youtube

Notes:
  • Useful for only adding new items to the default Inspector view of a script.

03. Adding Buttons To A Custom Inspector

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.

Unity / Youtube

Notes:
  • Useful for improving workflow by automating tedious tasks with a single click of a button.

OTHER SCRIPTING

01. UnityEvent

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

Notes:
  • 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 within UnityEngine.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().
  • UnityEvents are useful for a number of things:
    • Content driven callbacks
    • Decoupling systems
    • Persistent callbacks
    • Preconfigured call events
Generic UnityEvents
  • By default a UnityEvent in a MonoBehaviour binds dynamically to a void 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 a string as argument.
  • UnityEvents can be defined with up to 4 arguments in their generic definition.

About

Implementations of select Unity Scripting Tutorials [https://unity3d.com/learn/tutorials/topics/scripting]

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages