July 4, 2023

The /r/roguelikedev challenge for Unity: Parts 0 and 1

By bluesoul

Part 0 – Setup

Video

You should’ve already clicked the link in the preface to get Unity Hub, and I’ll assume you have figured out an IDE and have installed Git.

Start out by opening Unity Hub. Create a 2D Core Project, give it a name, and choose where to save it to. I left the editor version on the most recent Long-Term Stable build.

Once your project has spun up, go to Edit -> Preferences. Under External Tools, set the External Script Editor to your IDE. If this is Rider, Rider should open up automatically. Unless you’ve messed with the default plugins or are running a thoroughly ancient build, Unity support should be enabled already.

We’ll take a quick detour here to ensure that git is working and communicating.

  • Create an empty git repository at your git provider of choice.
  • Open up a terminal at the location of your new Unity project folder and git init.
  • Create a new .gitignore file and populate it with these Unity defaults:
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/
# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/
# Recordings can get excessive in size
/[Rr]ecordings/
# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*
# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*
# Visual Studio cache directory
.vs/
# Gradle cache directory
.gradle/
# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db
# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta
# Unity3D generated file on crash reports
sysinfo.txt
# Builds
*.apk
*.aab
*.unitypackage
*.app
# Crashlytics generated file
crashlytics-build.properties
# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

If you’re using a JetBrains product, you should also add this below the other .gitignore stuff above:

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

Now we’re going to verify a couple of Unity settings to ensure git will work as intended. In Unity, go to Edit -> Project Settings.

  • Under Editor -> Asset Serialization, ensure Mode is set to Force Text.
  • Under Editor -> Version Control, ensure Mode is set to Visible Meta Files.

Now, link your initialized git repo with your upstream, back in your terminal:

git remote add origin git@github.com:myusername/myrepo.git

Go ahead and commit and push your .gitignore file and then do the same for the remaining files just to verify this piece is done. If git barks about there not being an upstream branch, you can add:

git config --global --add --bool push.autoSetupRemote true

(Omit the --global if you don’t want this to be the case across your computer, but man I love being able to make a new git branch and not have to do the whole –set-upstream thing when I push.)

At this point you should have a working git repo that’s got no changes to commit. Now we can get started making the game.

Unity should be open and pretty barebones right now. At the top menu, hit Window -> Package Manager.

Within Package Manager, hit the dropdown at the top and change Packages: In Project to Unity Registry.

Choose Input System from the list and hit Install in the bottom right.

Hit Yes on this surprisingly scary looking pop-up and let the editor reload.

Now we actually get to write some code. Within Unity, close out the Package Manager and in the bottom window where the Assets folder is visible, right-click anywhere in that empty space, and choose Create -> C# Script.

(The bottom-left of the popup window on the left side corresponds to where I initially right-clicked.)

Name this file GameManager omitting the .cs at the end. Unity will handle file extensions for you. Double click the file you just made and it should open up in your IDE. If your IDE doesn’t pop up, double check your settings from farther up this page about setting the External IDE.

Notice that we’ve got some boilerplate code provided by Unity since we created the file from within it, and the class has already been named and given an upstream class that it inherits from (MonoBehaviour).

We have a number of changes to make to the default, so just make this the contents of your GameManager.cs to get started:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    
    void Awake()
    {
        
    }
}

(I will occasionally be using a slightly different style from what’s presented in the video series. This is mostly because the IDE barks at me occasionally when I do it his way, and I like to keep things clean on that front when it’s just stylistic stuff. For example, a convention of C# for public fields is that they’re capitalized.)

Now within that Awake method, we’ll check for the existence of a GameManager instance, and initialize one if it doesn’t yet exist.

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

Now we’ll need a spritesheet for the next step. Here’s one:

If you’re having trouble with that file, here’s a link to one.

Now, in Unity, right click in the same general area you used to make your GameManager file, and choose Import New Asset... and select that sprite sheet. Click on it in Unity and you’ll have a property inspector open up on the right side of the screen. You’ll need to adjust several things here:

  • Sprite Mode: Multiple
  • Pixels Per Unit: 10
  • Filter Mode: Point (no filter)
  • Max Size: 512
  • Compression: High Quality

Click Apply, then click Sprite Editor towards the middle of that window on the right.

In this window, you’ll hit the Slice dropdown, and make these changes:

  • Type: Grid By Cell Size
  • Pixel Size: X and Y both 10

Click Slice and then Apply in the upper right, and then close the window.

Now, click the Main Camera object in the hierarchy view below SampleScene. In the Inspector, set Camera -> Size to 10.

Now, save the project (Ctrl/Cmd+S) and let’s upload what we’ve got so far to git, because that’s the end of this part. If your git status looks more or less like this you’re on the right track, give or take the .idea folder and whether your IDE automatically adds new items to the git scope. As an aside, for the purposes of this tutorial, feel free to just push everything to main or master. I’ll talk about git strategy and general advice on that topic at the end.

If all looks well and you’re pushed and clean, let’s move on to part 1, instantiating the @ symbol and moving it around.

Pages: 1 2 3