Text101: Unity



We first look at the Unity layout, then we create many scriptable objects by creating a text advanture, understanding about state machines, and continuing to know scripting in C#. As you go through this course you might have different interested. 

We can design our own game and select our own theme in that way, and we start with images, and pictures and visuals. We'll put a game title, story text, scriptable objects, and all of the options that the player can take, so we ask players to choose their own path through out game. Don't do choose your own adventure choose your own name. You want to look a feeling of discovery, is to discover our world. Do we go left or go right, punch the guard and right away? We need to have 1-3 choices to have to progress, and we want some steampunk images. We should look for a couple of minutes of gameplay, with airship. The player is the injured airship pilot, small city under seige, the festers, and the goal is to get airborne. The challenge is to choose your own quest, or you wake up in a coma after 10 years or something, futuristic, militaristic, dungeons and dragons, etc. Who is the player, and what is the goal? 

A sprite is a 2D graphic object obtained from a bitmap image. Everything within a hierarchy is a game object. If we click on the main camera, we get all of the information about the main camera. 


UI is user interface, and there are buttons, text and menus. Free aspect ratio means we can change the window size. We can also get a text object, as follows: 


The order on canvas makes a difference between what is first and second. Whatever sits on the top is rendered first and at the bottom is rendered last. 



On the right, there's a checkmark to switch canvas on and off. ctrl + D will duplicate the text. On each compartment we have what we can consider components, with a Game Object, which is all the things that we might have on the home stereo system. These are components of the stereo system. Properties can be values or references in Unity. We can see objects, such as the stereo shelving unit consists of components, which are made up of properties, and the properties can be either values, or references. We can reset the position back to 0. 

It's standard practice that we reset our empty game object every time. There's several components, such as audio, effects, 2D animation components, etc. We can also make a script and we can make a class called AdventureGame. 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class AdventureGame : MonoBehaviour

{

    [SerializeField] Text textComponent;

    // Start is called before the first frame update

    void Start()

    {

        

    }


    // Update is called once per frame

    void Update()

    {

        

    }

}


So Text is a specific keyword in unity. We can do using using UnityEngine. Using UnityEngine.UI is using the engine user interface name space. Serialize field will display onto the interface. 

This is transmitted into an object format that can be stored an unpacked later. Now we can see the text component over here in the Unity Engine: 



Here, we're interested in story text, since we want to dynamically update all of our story text. Now we want to set the textcomponent text to the story text. Here's the text component:


These are the text values that we have inside the text component, so we want to have the textComponent within the text component, we are accessing the text property. The challenge is to add the text programmatically. 

We have the script component in the gameobject and the story text inside of the script component.

In order for us to create an interesting text adventure, we're going to have to deal with state machines, for a lot of text and a lot of adventure, which means state machines. A state is an action or process or behavior. A state machine assumes only one state at a time and conditions transition from one state to the next. There is a start symbol, which then gets to state A and have a decision point and from the decision we might say yes or no on the decision that leads to a specific decision. Here's an example:




In our game we're going to have many more decisions, like do you shoot the person, etc. We might have hundreds of states, and each state may have many lines of story in it and we need a way to manage this data. This is our requirement and we need to have all of these solutions. We need to do some concatenation of text and then we need to for each of these states see what the key input is, etc. 


We first want to look at the initial state of the game. A ScriptableObject is a class that lets us store data in stand alone assets. We want to display our story text, and we now want to figure out which words that we want to display to our player. We can easily pick and choose instead of having all of the content from the script. 



So now we need to create a new script. We have now created the following state class: 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class State : ScriptableObject

{

    // Start is called before the first frame update

    void Start()

    {

        

    }


    // Update is called once per frame

    void Update()

    {

        

    }

}


All scripts need to be attached to a game object. That's not the case with scriptable objects. This is already an exception to the rule, but our state we don't need to attach things to the game object. We first have to change the class to a scriptable object class. We then create a state scriptable object inside of the Unity Class. 


TextArea[14, 10] determines the minimum size of the inspector of our field. This is to keep our inspector nice and tidy. 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


//create the state meu.

[CreateAssetMenu(menuName = "State")]

public class State : ScriptableObject

{


    //serializing field makes things available inside of the script

   [TextArea(14, 10)] [SerializeField] string storyText;


}



We can refer to a variable as a box, to say where we are in a particular story. 


Private method: only the things in the same class/script can call that method. Public : method can be called accross all of the project. 



We're looking to create a new public method. 


Here's our stuff so far:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI; 


public class AdventureGame : MonoBehaviour

{

    [SerializeField] Text textComponent;

    [SerializeField] State startingState; 

    State state; 

    // Start is called before the first frame update

    void Start()

    {

        state = startingState;

        textComponent.text = state.GetStateStory();

        

    }


    // Update is called once per frame

    void Update()

    {

        

    }

}


So we know our starting state, and then we set the text to the GetStateStory() of the other state class. 

Arrays act very similar in C# as compared to JavaScript.


Initialization: int[] oddNumbers = {1, 3, 5, 6, 9};


Invokation: oddNumbers[3];


There's also a lock to lock the inspector to a specific starting state. 





Now we want to have the inspector remain on the starting state and drag both to the next states and size it to 2 and add the next elements into the array. We can use var() as a quicker keyword when our variable is declared. This is when we give the context to the particular variable, especially when you invoke the .getNextStates() method. 


Now, we want to call the method we just created and give the player. So as a result, we set the state results based on the button that was pressed. 


You can also use .xml files to add a particular state. So first, we will create a folder called scripts. We can organize things into folders and add states accordingly. 


We have created a lot of state files now. We can copy the story text and hook up what the next state is. 






You can also put 2 success states in addition to the "GAME OVER" state. Unity has a lot of goodies, and we can update the titles using the TextMesh Pro software. We will then get a .atlas file in the terms of text measurement. 


You need a text TMP object at first. 



We find the font asset at whatever's named "The Jungle Tribe" and with the Jungle Boy SD Font.



We can also do bevel, lighting and other features with the TMP library. 

Here's an example of what the font final text looks like:


We can also use the adobe color selectors in order to select the correct/proper colors. We can also use the adobe color changer, and change the hexadecimal color that way. Now this helps to give a full game experience. We manage TextMeshPro inside of the package manager. 


Comments

Popular Posts