Uploaded April 2023 | Updated November 2026, 36 minutes ago
JSON is a common data format that stores data in human-readable strings that can be interpreted by computers. This makes it possible for you and other people to read and modify the raw data and pass that information along to internal systems for use. JSON is commonly used across the web and other places and has native support built in to Unity using JSON.NET. In this video we're going to walk through an introduction into how to serialize and deserialize JSON objects in Unity so you can quickly store configurable information about your games in a standard open format.
* `JsonConvert.SerializeObject()`: Serializing object's converts a C# object's data into a string so you can store and pass that information along to other systems. Passing in a class will return a string of the data serialized into a JSON format. You can read that data back out using `DeserializeObject`.
* `JsonConvert.DeserializeObject()`: Deserializing object's requires two parameters. The class you want to deserialize the data into and the JSON formatted string you want to deserialize. This method will automatically attempt to convert the provided string into a new instance of the target class and return that class back to you to use. After you've modified data you can write the information back to a JSON string with `SerializeObject`.
When working with strings that span multiple lines there are two Attributes you can add.
* [`MultilineAttribute`](docs.unity3d.com/ScriptReference/MultilineAttribute.html): Multiline strings expand the normal textbox a fixed number of vertical lines. This is less flexible than a `TextArea` but can still be useful in some situations.
* [`TextArea`](docs.unity3d.com/ScriptReference/TextAreaAttribute.html): Text Area's span between a minimum and maximum number of lines and - unlike a `Multiline` attribute - they will add scrollbars if the text exceeds the maximum number of lines. Additionally the `TextArea` spans the entire width of the Inspector giving more room to type.
The JSON.NET documentation is available here: newtonsoft.com/json/help/html/Introduction.htm
***
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
JSON is a common data format that stores data in human-readable strings that can be interpreted by computers. This makes it possible for you and other people to read and modify the raw data and pass that information along to internal systems for use. JSON is commonly used across the web and other places and has native support built in to Unity using JSON.NET. In this video we're going to walk through an introduction into how to serialize and deserialize JSON objects in Unity so you can quickly store configurable information about your games in a standard open format.
* `JsonConvert.SerializeObject()`: Serializing object's converts a C# object's data into a string so you can store and pass that information along to other systems. Passing in a class will return a string of the data serialized into a JSON format. You can read that data back out using `DeserializeObject`.
* `JsonConvert.DeserializeObject()`: Deserializing object's requires two parameters. The class you want to deserialize the data into and the JSON formatted string you want to deserialize. This method will automatically attempt to convert the provided string into a new instance of the target class and return that class back to you to use. After you've modified data you can write the information back to a JSON string with `SerializeObject`.
When working with strings that span multiple lines there are two Attributes you can add.
* [`MultilineAttribute`](docs.unity3d.com/ScriptReference/MultilineAttribute.html): Multiline strings expand the normal textbox a fixed number of vertical lines. This is less flexible than a `TextArea` but can still be useful in some situations.
* [`TextArea`](docs.unity3d.com/ScriptReference/TextAreaAttribute.html): Text Area's span between a minimum and maximum number of lines and - unlike a `Multiline` attribute - they will add scrollbars if the text exceeds the maximum number of lines. Additionally the `TextArea` spans the entire width of the Inspector giving more room to type.
The JSON.NET documentation is available here: newtonsoft.com/json/help/html/Introduction.htm
***
Join the World of Zero Discord Server: discord.gg/hU5Kq2u

![Live Coding Handles and Custom Editors in Unity
There are a few fun ways to make fantastic Editor tools in Unity for your code. Two of those are `Handles` and `GenericMenu`. They enable you to create interactive components inside the Unity Scene View that allow you to more easily interact with your objects. They work as fantastic solutions for building custom editors. The thing is... Ive never really used them. Its time that changed.
Lets explore how these features work and how we might use them for our own projects. How do we draw a custom Handle using Unitys Handle features? How about creating a custom editor with a context menu we can consume? Well build both to improve the visualization of our custom Graph code from a previous video.
To draw a basic `Handle` you may use `OnSceneGUI`. A sample of what this kind of editor might look like is below. It will draw a yellow line from the center of the scene to the `GameObject`s position.
```csharp
using UnityEditor;
[CustomEditor(typeof(MyCustomClass))]
public class MyCustomClassEditor: Editor
{
protected virtual void OnSceneGUI()
{
MyCustomClass customClass = (MyCustomClass)target;
// Safety against selecting a null GameObject
if (customClass null)
{
return;
}
Handles.color = Color.yellow;
Handles.DrawLine(Vector3.one, customClass.transform.position);
}
}
```
You may draw a custom `GenericMenu` using something like this. It will create a two option menu that prints out the selected option to the Debug Log.
```csharp
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent(The name of a menu option), false, () ⇒ { Debug.Log(Picked First Option); });
menu.AddItem(new GUIContent(The name of a second menu option), false, () ⇒ { Debug.Log(Picked Second Option); });
// once you are done, show your menu
menu.ShowAsContext();
```
The documentation for `Handles` is available on Unitys website: https://docs.unity3d.com/ScriptReference/Handles.html
The documentation for `GenericMenu` is also on Unitys site. More info here: https://docs.unity3d.com/ScriptReference/GenericMenu.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Live Coding Handles and Custom Editors in Unity](https://i.ytimg.com/vi/3efiobxsurY/mqdefault.jpg)

![Using C#s nameof Expression - C# Quick Tip
The `nameof` expression allows you to convert a variable, property, type or function into a string that represents its name. This can be an alternative to typing out strings directly in your code when you need to reference the name of an object in your code. This is really common when implementing `INotifyPropertyChanged` events and has a number of other uses as well.
The advantage of using `nameof` compared to writing out the object in a string is that the dotnet compile will fail if the reference in your nameof expression does not exist. This can help catch issues during refactoring when elements are renamed. Typically, when using strings directly those references can become runtime errors and potentially be very difficult to identify (ex. a UI element not updating because it no longer receives its Property Update notification). `nameof` can help avoid that issue in your code.
Some examples of how to use `nameof`:
```csharp
class Program
{
public float aVariableNamedFoo = 3.14f;
public float Foo =˃ aVariableNamedFoo;
static void Main(string[] args)
{
Console.WriteLine($Class: {nameof(Program)}); //Class: Program
Console.WriteLine($Variable: {nameof(aVariableNamedFoo)}); //Variable: aVariableNamedFoo
Console.WriteLine($Property: {nameof(Foo)}); //Property: Foo
Console.WriteLine($Function: {nameof(Main)}); //Function: Main
}
}
```
`nameof` can also be used against members of other objects. For example the `nameof` a Lists Count can be retrieved using:
```csharp
var contents = new List˂Page˃();
Console.WriteLine($List Count: {nameof(contents.Count)}); //List Count: Count
```
You can learn more about using `nameof` in the C# documentation here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof
***
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using C#s nameof Expression - C# Quick Tip](https://i.ytimg.com/vi/48iBTUYjf8k/mqdefault.jpg)

. These are very Unity specific and include a lot of custom code to integrate cleanly into Unitys lighting systems. Because of this, a `Surface Shader` doesnt require you to create any custom lighting and will automatically function and look natural inside of most environments. It will receive lights, cast shadows and more.
That isnt the case for the `Unlit Shader` which removes the Unity specific `surface` function for more traditional `vertex` and `fragment` ones which align very closely with how a shader might work in OpenGL or other engines (though not necessarily identically!). Like the name suggests an `Unlit Shader` has no lighting effects, the default shader will just draw a texture onto a mesh and be as true to color as possible for that texture.
Just because the shader doesnt include things like lighting doesnt mean we cant add our own though. First though, we should take a look at vertex and fragment shader functions.
- `#pragma vertex vert`: will declare a function for a vertex shader named `vert`. Vertex shaders jobs are to transform a mesh into a camera based position so that it can be drawn onto the screen. This is done using a series of matrix multiplications. Luckily that work is already done inside of Unitys templates with the code `o.vertex = UnityObjectToClipPos(v.vertex)`. That code accepts an input vertex which will be a point relative to the original position of the object (in our example we use 3 boxes, the *input* of the 3 boxes is the same vertices but the *output* is modified because while the cubes are identical theyre in different positions). Vertex shaders can store other information than the position of a vertex such as the color or normal direction of the mesh and we can introduce extra information we may need here as well.
- `#pragma fragment frag` will declare a fragment shader with the name `frag`. These are passed the output information from the vertex shader and are responsible for actually putting color on the screen. Theyre called `fragments` and not `pixels` here because its actually possible for multiple `fragments` to be drawn in a single pixel for a variety of reasons (meshes overlapping one another for example). Inside the `fragment shader` the values from the vertex shader are not necessarily received unmodified. Your graphics cards will do some fancy interpolation for you between these two steps. If you think of the vertices in our mesh like numbers on a number line you might end up with a line that has 0 and 1 on it. The fragment shader represents the entire line including 0 and 1, but also all the numbers in-between. In the cases of those intermediate numbers we dont usually want duplicates of 0 and 1 but the unique value at that position in the number line. Fragment shaders will do this calculation for us and interpolate colors, vectors and more for us and pass those interpolated values into the input of the fragment shader.
Using this information we can introduce a custom lighting effect by passing the normal value of each vertex into our shader. This will give us a direction facing away from the face of the mesh (the normal of the floor - as an example - is straight up). Using a `dot product` we can compare the result of that normal value to a global light source to determine how strongly the light is impacting that point on the mesh. A very quick sample of what the code to do this might look like is this:
```shader
fixed3 lightDirection = fixed3(0, -1, 0); // Create a light source from directly overhead
// A max function is used here to prevent negative values during our math.
fixed lightIntensity = max(-1 * dot(lightDirection, input.worldNormal), 0);
```
Code for the full shader will be released in the next few days.
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Writing Our Own Diffuse Lighting Shader](https://i.ytimg.com/vi/4XfXOEDzBx4/mqdefault.jpg)



![Solving FizzBuzz with C#s Pattern Matching Switch Statements
Pattern Matching was introduced into C# a few years ago and Ive not really been able to integrate it into my projects. I wanted to explore the feature a bit more by solving Fizz Buzz. I figured we could learn about it together. 😃
FizzBuzz is a common interview question that covers order of operations, conditionals and loops in the language of your choice. In this video, well be replacing the more commonly used `if` statements with a pattern matching switch expression.
The rules of FizzBuzz are:
Iterate over a series of integers.
* If the number is divisible by 3 print Fizz
* If the number is divisible by 5 print Buzz
* If the number is divisible by 3 **and** 5 print FizzBuzz
* Otherwise print the number to the console
For example if we provide 9 we should print Fizz because 9 can be evenly divided by 3 with no remainder. If we provide 10 we should print Buzz because 10 is divisible by 5. If we provide 15 we should print FizzBuzz because it is divisible by both 3 and 5.
This is the code from the video (The greater-than and less-than symbols may need to be replaced. YouTube does not support them natively.)
```
class Program
{
static void Main(string[] args)
{
for(int i = 0; i ˂ 20; ++i) {
Console.WriteLine(GetOutputString(i));
}
}
static string GetOutputString(int i) {
return (i%3, i%5) switch
{
(0, 0) =˃ FizzBuzz,
(0, _) =˃ Fizz,
(_, 0) =˃ Buzz,
_ =˃ i.ToString()
};
}
}
```
Here are some additional examples and tutorials on using pattern-matching in C#: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/pattern-matching
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Solving FizzBuzz with C#s Pattern Matching Switch Statements](https://i.ytimg.com/vi/68ko-chyM2E/mqdefault.jpg)
. Some of these may not be helpful if your doing local development with an existing remote repo somewhere like GitHub or GitLab.
Files you change in the `.git` directory will not be checked into your git repository and therefore will not be visible to the rest of your team. If youd like to share your pre-commit and other hooks with your team you can place the scripts somewhere inside your project and create a symlink to them in the `.git/hooks` directory.
Note: make sure that you make your Git Hooks executable or git will not be able to access and run them!
If youd like to be able to edit `.git` files more easily inside of Visual Studio Code youll need to disable the automatic file exclusion. You can do this by updating your `settings.json` file by adding the following setting:
```json
files.exclude: {
**/.git: false
},
```
This setting prevents all `.git` directories in your project - regardless of where they are located - from being hidden.
You can also bypass Git Hooks using the ` no-verify` (shorthand `-n`) argument when running git commands in your CLI. This will completely bypass these hooks and prevent them from running. If you have a Git Hook that requires tests to pass before pushing running `git push no-verify` would allow you to push your changes for example.
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using Git Hooks](https://i.ytimg.com/vi/6MzXs35hLOI/mqdefault.jpg)