Project 4: Game Programming Essentials

by Andrew Long on ⌜Dec 7, 2022⌟

Tags: #GPE338, #Unity, #Game Programming

Project 4

Project 4 is one of the easiest yet, that doesn’t mean I didn’t have any issues however. When I first tried to do something akin to property drawers, I immediately disregarded property drawers and went straight to editor scripting and it was the worst mistake I could have made. I did this well before Project 4 was even talked about, so maybe that’s why I find the concepts here easy.

Editor Scripting:

Editor scripting can be easy, only if you make it easy. Editor scripting is also incredibly powerful, but if you would like to keep the rest of the editor intact while modifying on property, use a property drawer instead.

In my own example, I keep the code super simple. I draw a warning, then I draw the regular editor inspector.

Attributes:

From hiding variables in the editor or using custom property drawers, there are many reasons to use attributes in Unity. In the code above I use an attribute and a custom property drawer to hide a property depending on an enum variables value.

The inspector when the attributes are applied:

Property Drawer:

Property drawers in Unity are really useful if you need to change how one property is drawn. The code above looks absolutely insane, but all it does is get the value of an enum variable and disables the gui of the property if its value is passed to the attribute.

This is the product of the code:

Bitmasking:

This certainly isn’t the method of bitmasking you’d be expecting in Unity, but this is bit masking. This function finds the position of duplicate characters in a string. More documentation on the methodologies behind this function can be found here, written in Python3.

With the code:

FindDuplicateChars.PrintList(
    FindDuplicateChars.FindDuplicateCharInString("tmpt")
);

prints out this:

The reason the duplicate is found at 3 and not 4, is because strings are zero indexed.

Scriptable Objects:

To create a scriptable object you need to add a CreateAssetMenu attribute above the ScriptableObject deriving class.

The GameManager above has a references to the scriptable object and prints to the console if the debug setting in the scriptable object is true.

To complete the reference, drag the scriptable object onto the pointer property in the inspector.

Scriptable objects are not typically edited because they are used as psuedo templates, but in my case it is being edited because the scriptable object holds setting data.