Link to the PDF Version of this tutorial
http://tinyurl.com/obbunhe
Hello everyone. I'm At-a-loss. If you're at a loss when it comes to finding some help on uScript, then I'm here to educated you. Luckily for us, uScript's nodes are so versatile that they can be used to do things that most people wouldn't have thought of.
For today's tutorial, I'm going to share how I stumbled across a simple way to randomly choose a TextAsset from a list then display it's contents. I shouldn't say stumble because I intentionally spent an hour trying to use nodes that I'm very familiar with, to find a solution.
I'm not a genius coder like many of the developers on this site (I'm primarily a Designer). My setup of nodes isn't so perfect that it belongs in a Art museum (some people on here create artwork when they display their graphs, it's a marvelous sight to behold). The goal is for people like me that want results without having a lot of nodes and data to input.
If you're already familiar with uScript skip to the bottom where the entire Graph is displayed. If not, let's begin...one more thing, you already know to connect your nodes together so I won't mention it.
1- Create your scene. For this scene this is all I have.
SIDE NOTE
If you're wondering how I grouped the objects.
1. Create a Empty Game object
2. Select all the things you want to be grouped with that empty game object.
3. Drag them into that Game object (so they will become it's children)
4. Rename that game object.
You're done. That's how you can have groups or categories while you work. Game objects are very versatile on Unity. It's a shame Unity didn't come with this type of grouping system by default.
2- Create a GUI Events node. Of course you can search for it in the search bar, by typing GUI events.
Click on it so this can appear on your graph.
You'll notice that I'm using a “Owner Variable”. I've read on this site that when you use a Owner variable, it's equal to saying “Whatever has this graph attached to it, use what's on this graph when I press the play icon”. Of course I'm paraphrasing.
3- Create the Owner Game Object Variable. To get to the owner variable you: Right click anywhere on the graph then...
4- Connect the Owner GameObject to the GUI Event node.
5- Create the GUI Button node
6- Fill in the information for the node. Here is what I have with my node.
SIDE NOTE
You will need to make the position a public variable so that you can edit it within the Inspector window of Unity. To do that you place your mouse over the little white dot. Then right click on your mouse. Create Linked Variable.
This is what I have for my public variable. You can change it later in the Inspector window.
7- Create the Random Switch node. You can find it by searching in the sear bar.
Here is what I put in my node
SIDE NOTE
I also have this node bookmarked. To bookmark a node in uScript
1. click on a node.
2. click on the star
3. click on any of the available slots.
And you're done. It's now bookmarked for future use. All you do is click on the Favorites bar to select it.
7B- Create a Linked Variable (click on the blue triangle then choose “create Linked Variable”). This is what I put in my Linked Variable. I didn't make it editable in the Inspector window of Unity.
8- Create a Resources Folder inside your Unity Assets folder.
Create three text files inside that folder. You can name them whatever you want. But this is what I have in my folder.
Type whatever you want in each of the text files. But this is what I have inside each of mine.
9- Create a LoadText Asset node (use the search bar). Here is what I have inside of my node.
Click on Browse to select one of your three text files.
Right click on the Loaded Asset and create a Linked Variable. This is what I have on my Linked Variable.
I DID NOT choose a TextAsset in the Value section (that's why it says none). I just created it to be able to see what text would load and if it was working correctly.
10- Create a TextAsset to String node (use the search bar). No need to put any information into the TextAsset to String node, because it will be linked to the Load TextAsset node.
Place your mouse over the Green Triangle. Right Click, then choose Create linked Variable. Here is what I have in my node. I created this just to see if it was working.
11- Create a Print Text node (use the search bar), here is what I have in my node.
Link the Text Variable to the TextAsset to String Variable. Your graph should look like this.
12- Now it's time to Copy and paste. I don't know how you do it, but if you're litreally an infant to uScript this is how I copy and paste.
Zoom out the graph by using your mouse wheel
Hold down the left mouse button and drag a box around the nodes to highlight them
Right click on the highlighted nodes and choose copy
Left click anywhere on an the graph and click paste
13- Go back into your other Load TextAsset and choose one of your other text files. Basically each Load TextAsset should have a different text file. Like this
14- Now it's time to test out a loaded text. Click Save (I don't know if you're saving it as a debug or Released, but I save my texts as a Release). I named my Graph Random_Text. But you can name yours whatever you want.
When you see this message click yes (for the sake of this tutorial).
Assign the Generated Script to the master Gameobject. Theirs two ways to do this.
Method 1
Drag and drop the script from the uScript > uScripts> GeneratedCode folder. Remember you can only use the ones that end in “_Component”.
Method 2 (the method I use)
1. Click on the Game object I want the scripts to be assigned to.
2. Click Add Component
3. Scroll down to uScript and click on it
4. Click on Graphs
5. Click on the name of your graph
That's it.
15- Each time you click on the Load Text button a random text file will load and display the message you wrote within it.
This can be useful if you're creating a game and wish to have a random message congratulate the player. For example:You're Awesome!, Great Job!, You're the best!, etc.
I created it because I wanted random comments for my NPC's (non player controlled characters) for this game I'm creating. I don't know if this can work with Texture assets. But I will test it out and let you know.
I also have other “junk yard styled” graphs I created for procedural spawning an asset, changing Game objects colors at the push of a GUI button, a Mecanim animations, etc. Let me know if you want those tutorials made as well.
Thank you for your time and I'm sorry to all the developers and master coders on this site if my ghetto Graph and methods made your eyes bleed from such sloppiness. But when it comes to things in my life, I like the KISS (Keep It Simple, Stupid) method. KISS is an acronym for "Keep it simple, stupid" as a design principle noted by the U.S. Navy in 1960.
Source: http://en.wikipedia.org/wiki/KISS_principle