Change Unity’s C# Template Code Style

The first thing I used to do every time I’d edit a new C# script in Unity is delete the comments and unused usings which are all included as part of the template. Going through that activity for every new script was bad enough, but on top of that the code style used in the template is pretty far off from what is considered standard for C#. Eventually, I decided it was time to stop my pre-scripting ritual and just make the default template into what I want. So to begin:

Navigate to the ScriptTemplates folder in your Unity install folder.

Route with a typical Windows install: C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates

Open 81-C# Script-NewBehaviourScript.cs.txt in your text editor of choice.

From here you can edit the file’s layout however you choose. Mine looks like this:

using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour 
{
    private void Start() 
    {
        #NOTRIM#
    }
    
    private void Update()
    {
        #NOTRIM#
    }
}

Note: You may be unable to save/overwrite the template file if you did not open your text editor As Administrator. Don’t worry, you can just Save As to your Desktop and drag/drop the new version from the Desktop to the ScriptTemplates directory.

Event Delegation in Unity

What is Event Delegation?

“…a helper object, known as a delegate, is given the responsibility to execute a task for the delegator
– Delegation Pattern Wikipedia

Why use Event Delegation?

A good practice when working with Unity (or any framework) is to decouple your UI logic from your game logic. It’s fine to have the UI code reach down to the game logic layer and call methods directly on it but having the game logic layer directly call methods on the UI logic’s layer is, in general, a bad practice because doing so “marries” you to whatever UI you are currently using. So to get around this we can use C#’s Delegates and Events to fire off methods throughout our UI and even on the game logic layer.

How to use Event Delegation with Unity

All we have to do is:

  • create a static Event Manager class
    • Add delegates and events to this Event Manager
  • Listeners will add functions to the events
  • Delegators will fire off the events

The Manager: Defining events

public class EventManager {
    public delegate void EnemyHovered(int laneIndex, int targetRange);
    public static event EnemyHovered EnemyTargetedRequest;
    public static void EnemyTargeted(int laneIndex, int targetRange) {
        if(EnemyTargetedRequest != null) EnemyTargetedRequest(laneIndex, targetRange);
    }
}

In this example we create a delegate with the type EnemyHovered which takes the arguments laneIndex and targetRange. Then we make an event EnemyTargetRequest which is the event that the listeners will subscribe functions to. Finally, there is a static method that can be called by delegators when they want to fire off this event, triggering all the functions that are subscribed to it.

The Listener: Subscribing to the event

In another class we can add the following:

void Awake() {
    EventManager.EnemyTargetedRequest += DoSomething;
}

public void DoSomething(int targetedIndex, int targetRange){
    // do something
}

In this class we define some function called DoSomething that takes the same arguments as our delegate. In the Awake() method we add the function to the static class’s EnemyTargetedRequest event.

The Delegator: Firing off events

In yet another class we can do the following:

public void OnPointerEnter(PointerEventData eventData) {      
    EventManager.EnemyTargeted(_index, _targetCount);
}

Here we’re just using Unity’s OnPointerEnter event as the UI event that will trigger our EnemyTargeted event. Because our Listener class subscribed it’s DoSomething function to the EnemyTargetRequest event when SignalManager.EnemyTargeted is called it will fire off the DoSomething function!

So with this, you can keep all your events off in the static Event Manager, subscribe to them when you need an object to react to an event and fire off the events whenever you need to which allows you to keep UI and game logic separated. You could even have GameObjects subscribe to events and unsubscribe from them as they are created and destroyed which is very useful when these objects are created and destroyed dynamically.

 

Naming Things is Hard: Brainstorming Names

I’ve been working on my game for about 3 months now and I have yet to come up with a suitable name for it. I’ve been referring to it mainly as “My Game” since it’s really my first game project to make it this far. But I have given it a code name which I will use to refer to it when posting here, and that code name is Nano. This comes from the setting of the game which I’ll discuss more in a future post but suffice to say, nano technology plays an important roll in the setting.

How to Brainstorm Names

I came up with Robot Monkey Brain during a few short brainstorming sessions with a pal of mine. It’s real important to have someone to bounce ideas off of, and when brainstorming it’s best if there aren’t any negative judgments about ideas. Just get a couple of people to start spouting out names and phrases and follow the good ideas, building on them but feel free to jump tracks if one path is getting stale. We were hovering around the words Robot and Monkey since I’m a big fan of robotics and my online handle has included a misspelling of the word monkey since I was a freshman in high school. Once you find a good one make sure it isn’t taken:

  1. Search the name, see if the name already has some pop culture weight around it.
  2. See if the domain is taken. You want the .com extension unless you’re outside the US and don’t plan on making your game available internationally.
  3. Check if there is already a Facebook page and twitter account using that name.
  4. If all those things are clear, snatch up the domain, Facebook page and twitter name.

I kinda screwed up the twitter part in that RobotMonkeyBrain is 1 letter too long to be a twitter handle, so I had to go with @RoboMonkeyBrain. It’s not the end of the world but the OCD part of me would prefer all the names match and I do have the Robot Monkey Brain Facebook Page all set.

I’ll go through the same process when I’m ready to give an official name to Nano, but I still need to develop more of the core systems and setting details before I set that in stone.