You are here

Unity RTS Tutorial Part 2 - Fully Working Resource System

By brotherkennyh on Thu, 03/16/2017 - 07:29

Adding a new resource

So first up, I have decided to include money in my game. So will update the scripts to include this resource. We need a sprite to include for display in the HUD. Open the Player prefab select the HUD and add a new sprite to the "Resources" List. If you edit this in the Hierarchy be sure to apply the changes to the prefab. Next we need to look at the code. Several modifications are required to add a new resource. Add the following at the end of the switch statement in the "Start()" method of "HUD.cs";

case "Money":

resourceImages.Add(ResourceType.Money, resources[i]);

resourceValues.Add(ResourceType.Money, 0);

resourceLimits.Add(ResourceType.Money, 0);

break;

This creates the HUD buttons and text for money as the script starts. For the "DrawResourceBar()" method add the following just below the similar code for the other resources.

As well as adding the new "Money" resource I have expanded this the fully develop the other resources. For my "Stone", "Rock" will be the resource while it is being collected. "Ore" is "Metal" during the collection. "Vegetables" are "Food" during the collection. "Gold" is "Money" during the collection. Running the game now we should see the new resource available in the resource bar.

Removing Resources

To start with my game already has the ability to remove stone as units are created. This is handles in the "CreateUnit()" method in "Building.cs". The follwoing line controls this.

This statement needs to be expanded to handle multiple resources and the similar code in the "Player.cs" script to do the same when buildings are created. To start with "WorldObject.cs" needs to be updated to account for the new resources and the sale values of them. The following line setting variables needs to be updated;

Checking for available resources

We need to add some checks here to see if we have enough resources avaialble before starting creating an object. For units we need to prevent the unit form being added to the build queue. It would also be good here to prevent the user from clicking the button in the first place and it would also be nice to be able to see which buttons cant be clicked. Fortunately Unity can already handle this for us. We just need to disable the button. The "DrawActions()" method is called by "DrawOrdersBar()", which is itself called by "OnGUI()". "OnGUI()" can actually be called a number of times per frame so the code we add to the called methods will constantly be rerun to setup our buttons again if our available resources change. Unity can already handle this kind of UI. When the GUI buttons are set to disabled they are greyed out and cannot be clicked. We are not currently setting them to enabled or disabled, so they default to enabled. We need to set this just before the button is created. Add this line;

GUI.enabled = EnoughResource(actions[i]);

Just before the following line in the "DrawActions()" method;

if (GUI.Button(pos, action))

The new line of code calls the "EnoughResource()" method, which we will get to in a moment. It returns true or false. Setting the state of the button to true of false. Before we end the group we need to include a line to make sure it is always true at the end of the method so any later GUI components are not set to false. The end of the method should now be as follows;

This method checks through the list of buildings and units to find the costs for the relevant button. It returns a list of the integer values. These values can then be checked against available resources in "EnoughResource()". At this point you should be able to run the game and test the new resource system.