Uploaded January 2020 | Updated December 2026, 3 hours ago
Today I wanted to take a closer look at an older video where we built a dice roller that used Dot Products to determine which face on a dice was rolled. The problem with that original solution was that you would need to manually enter each of the dices vertices to get an accurate result. That's easy enough with a 6 sided dice, but becomes extremely annoying and tedious when you add more sides at awkward angles. Fortunately one of the things programming is great at is automating tedious things and that's exactly what we're going to explore here.
The solution I'm hoping to build is fairly straight forward and actually doesn't take much time at all. We can extract normal's from a mesh and then take the distinct set of normal's as the set of possible sides for our dice. This actually can be written in a single line of code with some LINQ.
```csharp
Vector3[] CalculateNormals(Mesh mesh)
{
return mesh.normals.Distinct().ToArray();
}
```
The solution has a few issues though and we'll take a look at how we might solve some of them. When looking at an object like a cylinder for example we'll get a large number of sides calculated because each edge of the cylinder is a unique side of our dice. We probably don't actually want that to be the case. We'll design a way we might be able to solve this before stepping back and taking a bit of a longer look at the problem.
The basic question of getting unique sides for a dice is solved, but if we want to advance this solution for more complicated shapes and allow it to handle not quite aligned vertices we'll need to do some more work. If you have an idea for a design, let me know! Otherwise I'll be here designing something to help us provide another solution.
The original Dice Roller was built in this video: youtube.com/watch?v=hmDF9PvMDVw
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Today I wanted to take a closer look at an older video where we built a dice roller that used Dot Products to determine which face on a dice was rolled. The problem with that original solution was that you would need to manually enter each of the dices vertices to get an accurate result. That's easy enough with a 6 sided dice, but becomes extremely annoying and tedious when you add more sides at awkward angles. Fortunately one of the things programming is great at is automating tedious things and that's exactly what we're going to explore here.
The solution I'm hoping to build is fairly straight forward and actually doesn't take much time at all. We can extract normal's from a mesh and then take the distinct set of normal's as the set of possible sides for our dice. This actually can be written in a single line of code with some LINQ.
```csharp
Vector3[] CalculateNormals(Mesh mesh)
{
return mesh.normals.Distinct().ToArray();
}
```
The solution has a few issues though and we'll take a look at how we might solve some of them. When looking at an object like a cylinder for example we'll get a large number of sides calculated because each edge of the cylinder is a unique side of our dice. We probably don't actually want that to be the case. We'll design a way we might be able to solve this before stepping back and taking a bit of a longer look at the problem.
The basic question of getting unique sides for a dice is solved, but if we want to advance this solution for more complicated shapes and allow it to handle not quite aligned vertices we'll need to do some more work. If you have an idea for a design, let me know! Otherwise I'll be here designing something to help us provide another solution.
The original Dice Roller was built in this video: youtube.com/watch?v=hmDF9PvMDVw
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
 where we created an interactive canvas you could collaboratively draw on with the rest of the world!
1. [Building Real-Time Web Apps with SignalR](https://www.youtube.com/watch?v=k5rdhYj6TB8)
2. [Connecting a Web App to SignalR](https://www.youtube.com/watch?v=c0zmYjQ8KWA)
3. [Creating a Real-Time Web Canvas](https://www.youtube.com/watch?v=hgq1CYEJg_Q)
You can see some of the other content I have planned outside this channel in the Microsoft Reactor (and even come to our in-person events) at the [Microsoft Reactor San Francisco Meetup Page](https://www.meetup.com/Microsoft-Reactor-San-Francisco/).
***
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Adding Unity Support - SignalR in Unity - Part 2](https://i.ytimg.com/vi/0mk-Kjab4as/mqdefault.jpg)




You can use the `CreateAssetMenuAttribute` to [create asset menus and more easily create new `ScriptableObject`s](https://docs.unity3d.com/ScriptReference/CreateAssetMenuAttribute.html).
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using Scriptable Objects in Unity](https://i.ytimg.com/vi/2P4oPtU2Rtk/mqdefault.jpg)


: 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`](https://docs.unity3d.com/ScriptReference/TextAreaAttribute.html): Text Areas 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: https://www.newtonsoft.com/json/help/html/Introduction.htm
***
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u An Intro to Json Serialization and Deserialization in Unity](https://i.ytimg.com/vi/3F4HjM_5hZY/mqdefault.jpg)

![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)
