Posted June 17, 2024 by Belz
Hi there,
My name is Zeb. I'm the Lead Engineer on Trade N' Transit and I want to show you how I made a world space tutorial interface with a type-text effect like this:
To begin with, it's nice to start with a prop such as this computer monitor that our artist Jacob Cooper kindly made for me.
The first thing that we're going to do is make a canvas in the Unity hierarchy. It's important to make a new one even if you already have one in the scene because we're going to set it to world space which allows us to position it in front of the computer monitor screen.
Next, we're going to set the canvas to world space. This lets us position it in the actual game world, rather than overlaying the game camera directly.
Now, it's very important to set the [Width] and [Height] of your canvas in its [Rect Transform] component to the final desired resolution of your canvas. I ended up choosing 1700x1080 for mine because it fit the prop screen well. It's also very important to set the [Event Camera] slot to a reference of the Main Camera in your scene, otherwise your clicks won't register. You can ignore the other values for now.
Next, you want to use the scale tool to bring your canvas all the way down to a reasonable size in your scene. I find it helpful to directly set the [Pos X] and [Pos Y] values to 0 in order to center the canvas in the scene. You also want to do this with your main prop, so that everything is nice and tidy when you align them.
Once your canvas is a reasonable size, we're going to align it with the screen of the prop like so:
Then, we're going to
Make sure you're naming everything meaningfully!
At this point, I recommend making sure that your canvas and text are displaying properly. If they are backwards, you may need to rotate your canvas 180 degrees to flip it around.
Next, we're going to add a button, resize it and place it in the lower center portion of the interface. Mine is stylized, but yours can be the default button for now.
Now it's time to code!
Let's create a C# class which we will place onto our Canvas called MonitorInterface.cs. It's going to need the following references:
Lastly, we have [tutText] which is a mysterious type called TutorialText. What is that you might ask? Well, it's a custom C# class which is going to hold our text data. This lets us containerize that part of the code, and place it into its own area which is a better programming practice than having everything inside of a single class. It looks like this:
There are a couple of different ways to approach this, but this is how I chose to do it. This is a single non-monobehaviour inheriting class which stores a single list of strings called [tutorialSteps]. This makes it much safer for an artist to add some lines to the tutorial! Next, we're going to add the following code to the MonitorInterface.cs class:
That's a lot going on! Let's break it down.
With any luck, this should create a typing text effect. Lets play it in the editor and see.
Hooray!
Next, you could extend this to use button functionality, or even use a state machine to make an interactive tutorial.
I hope that was helpful. See ya!