Wednesday, 7 March 2018

Using Linq to count how many times an object occurs in a list

You have a list full of objects. Some of the objects are duplicates, and you want know know how many of each distinct item you have in the list.

I searched around for some answers, and I did find some StackOverflow answers that almost got me to the result, but it wasn’t quite right.

In my example, the list I want to sort is a list of custom type Run ie List<Run>(). In my code, Run is a struct that looks like:

public struct Run
     public string RunID;
     public string Config;

The StackOverflow responses were mostly long and hard-to-read Linq statements. I like Linq a lot, but it’s not always easy on the eyes, especially once you start adding extensions.

Instead, I came up with the following:

void ProcessList (List<Run> runConfigs)
     var grouping = runConfigs.GroupBy(o => o.Config);
     foreach (var configGroup in grouping)
          string configName = configGroup.Key.ToString();   
          string count = configGroup.Count<Run>().ToString();

Doing the above code allows me to use configName and count however I please (which in my case was outputting them to a csv).

Here’s the same example but for a List<string>():

var grouping = listOfStrings.GroupBy(x => x);
foreach (var stringGroup in grouping)
    string name = stringGroup.Key.ToString();
    string count = stringGroup.Count().ToString();

If you step through this code with the debugger, it will show that 'name' is one of the distinct values in the list, and 'count' is how many times that object appears in the list. These values can now be used however you like.

Thursday, 1 March 2018

What is dynamic batching, and why is it off by default?

While reading this tutorial, I came to a section where it said something like “as you can see, dynamic batching has stopped working”. I did not see that. In the image in the tutorial “Saved by batching” on the stats panel had dropped to 0, but my stats panel still showed a number in the 1000s.

When I checked the player settings, I noticed dynamic batching was turned off. After a bit of investigation, I learned that dynamic batching is now off by default for 3d projects as of Unity 2018.1; but why is this useful?

A mesh is the part of a 3D object that describes its shape. When certain criteria are met, Unity is able to combine meshes from different GameObjects into one big mesh before they are drawn. This is called dynamic batching. The "dynamic" part means it happens while the game is running.

Sometimes (but not always!), it takes less time to draw one big mesh than it would to draw lots of smaller meshes. Dynamic batching occurs on the CPU, and drawing meshes occurs on the GPU. You can think of dynamic batching as spending CPU time to save GPU time. This means that it becomes more likely that the game becomes CPU-bound (meaning that the frame rate is lower than you want because the CPU is taking the most time), but you should ideally want as much as possible to happen on the GPU; that’s what it’s there for.

If your game is already CPU-bound, then dynamic batching will not help. But if your game is GPU-bound (meaning that the frame rate is lower than you want because the GPU is taking the most time), then it may help.

(FYI: The GPU equivalent of dynamic batching is GPU instancing.)

For these reasons, dynamic batching is desirable for old mobile devices that don’t have a GPU (or have a bad GPU), but it is not efficient for games intended for more modern devices.

Here is a helpful article that goes into a bit more detail: Why Are My Batches (Draw Calls) So High?

Friday, 23 February 2018

Accessing Shader Graph properties via code

You can create and name properties in shader graph using the Blackboard

These names really just serve as labels, so if you want to access these properties via code (to use material.SetColor, for example) then you need to peek at the shader code.

Right-click the master node and select 'Copy shader'

Paste that into text editor of choice and look at the Properties code block at the top 

My property was named _ColorTest and the actual name of it is Color_A2.. etc

Now I can do
material.SetColor("Color_A208B217", Color.magenta);


Thursday, 15 February 2018


I'm working through a tutorial on and wanted to take a moment to make some notes about coroutines.

I have used coroutines in almost every game I've made, but not stopped to think about how they really work.

Source for the below text:

My own notes and additions are in bold/blue

What does yield do?

The yield statement is used by iterators to make life easy for them (this part could potentially be explained better. Iteration is the act of repeating a process. Usually, when people talk about working iteratively, they are talking about working, making a small change or fix, then continuing to work and repeating that process, as opposed to doing it all in one go and fixing everything at the end. In this case, I think the sentence is talking about iterative functions and loops.).

To make enumeration possible, you'd need to keep track of your progress. This involves some boilerplate code (boilerplate code refers to code that has to be included in lots of different sections without being changed eg declarations or the basic empty template in HTML) that is essentially always the same. What you'd really want is to just write something like return firstItem; return secondItem; until you are done. The yield statement allows you to do exactly that.

So whenever you're using yield, an enumerator object is created behind the scenes to take care of the tedious bits.

Alternate definition: 'The yield statement is a special kind of return, that ensures that the function will continue from the line after the yield statement next time it is called.'

How do coroutines work?

When you're creating a coroutine in Unity, what you're really doing is creating an iterator. When you pass it to the StartCoroutine method, it will get stored and gets asked for its next item every frame, until it is finished.

The yield statements produce (return) the items (actions?). The statements in between – the stuff that you want to happen – are side-effects of the iterator doing its job.

You can yield special things like WaitForSeconds to have more control over when your own code continues, but the overall approach is simply that of an iterator.

Alternate definition: 'A coroutine is like a function that has the ability to pause execution and return control to Unity but then to continue where it left off on the following frame.' (source)

Post frequency increasing

If you notice that I'm posting more regular blogs, it's because I want to also use this blog as a sort of personal notebook where I store things that I find useful, but other people might also like (inspired my friend Ming Wai's blog)

I will still post bigger blogs every now and then, but I also want to write about smaller things to remind myself about them or understand them better.

Saturday, 3 February 2018

Shaders aren't as scary as I thought: My first shader with Unity Shader Graph

Disclaimer: I have worked at Unity since September 2016, but this is not a marketing blog or anything like that. This is my personal blog with my genuine experiences and reactions. Also, my boyfriend works on the Shader Graph team, but he wasn’t actually even in the country when I was making these.


If you’ve ever played any of my games, you will know that I am definitely not an artist. Art is fun and I appreciate it, but I’m not good at it.

For those new here; hello, I’m Sophia. I’ve been working as a programmer for a few years (in various roles/companies). I studied Physics at university, and I’m pretty good at maths, so I have been asked to help when my friends have had issues writing their shaders before. Small stuff like figuring out relationships between variables etc.

While I’ve always wanted to make shaders, I never really knew where to start. It felt very counterintuitive to write code relating to visuals but with no visual feedback.

During Scenario Test Week at work, my team has been working on a golf game. I was working on pickups/modifiers, and I wanted some cool effects to happen when certain modifiers turn on. My initial thought was ‘I need fire’. Since 2018.1 is the release that includes Shader Graph, it felt like the time had come for me to finally make my first shader.

I was going in pretty much completely blind. I just started adding nodes.

I’m not sure why I thought the Hue node would be relevant at all, but it turned out pretty fun. It's not included in the gif, but the Time node is definitely involved here.

I came back to this “Disco Fire” later. I didn’t want to get distracted before I even started.

Time and noise were definitely relevant, but I just didn’t really know how. Most of these connections are definitely irrelevant, but I wanted to show what I actually made.

So this sort of resembles fire, but it’s definitely not great.

At this point, I went back to the Disco Fire, because it was way more fun. This ended up being used on the golf ball when the speed boost is applied, so I was pretty happy with that one and moved on.

I don’t know why I didn’t include all the nodes for this one :( sorry. It's meant to be lava-ish.

I guess with this one I was going for like a… moss… thing? Idk. shaders, man. The checkerboard node was because I wanted to input more than one colour, and the Voronoi noise was because I wanted it to be sort of blobby (like lava).

This one is definitely my favourite, and I actually started to kind of understand what I was doing. As you can see, there are a lot fewer pointless noodles, and the final look is a lot closer to the lava effect I was trying to achieve. I ended up using this one for the slow ball pickup because it made me think of stone.

Here’s each of them before I added shadows. I was backporting these shaders to a Unity project that wasn’t using the Lightweight Render Pipeline, so I had to make a few changes to the shader file (I don’t want to go into this in this non-tutorial, but happy to make a separate post showing what I did, if anyone wants.)

Aaaand here's my lil test scene showing the fast ball in action (with shadows!)

So that was my experience with using Shader Graph to make my first ever shader! Hopefully this will encourage more people to try using it because it's really not as scary as it looks.

Sunday, 17 September 2017

I made a Unity editor window because of a weird dream I had

Yep, that's right. I've been thinking about custom Unity editor windows so much that I had a dream about them.

I originally wanted to make editor windows to create some in-editor tools, and automate certain processes. I had been experimenting and playing, but I hadn't really made anything functional. Then I had the dream.

Making an editor window itself is pretty simple. I was able to follow the Unity docs to make one when I first started learning. If you follow that documentation, you will be able to create an editor window. It won't do much yet, though.

Unity editor window scripts derive from UnityEditor.EditorWindow as opposed to UnityEngine.MonoBehaviour, and I needed to figure out how to get clicks and events from the custom window to affect game objects and other UnityEngine objects. After a lot of searching, I found a tutorial on The Knights Of Unity, and I was able to use the examples they provided to help me.

Since I already knew how to create editor windows, I had gotten as far as:

Apologies for the weird Gist formatting.

This gave me an editor window with a toggle (defaulted to false/unchecked), but it didn't actually do anything.

Obviously, I needed a way to find a way to make this toggle's event actually trigger an action in the scene. For this, I needed to make use of EditorPrefs. Making use of what I had learnt from The Knights Of Unity's blog, I created the following script:

The script has a public bool (with some editor-only properties), as well as a public reference to a GameObject which gets turned on and off in Update(). This is great, but it means I need to change the window script from

mangoEnabled = EditorGUILayout.Toggle("Mango Toggle", mangoEnabled);


ClickToMango.mangoEnabled = EditorGUILayout.Toggle("Mango Toggle", ClickToMango.mangoEnabled);

which references the bool in the ClickToMango class/script.

My Click To Mango dream became a reality.

Could I have done this just using a regular script in the inspector? Yes, probably. Did I learn more about how custom editor windows by doing this? Yes, definitely.

You can look at this on GitHub if you want.

PS I got this mango from the Unity Asset Store <3