Make a Simple GUI in Unity

In this post, I want to make something similar to a user interface with a few lines of simple code in Unity, where the user can mount one of the two scopes on the rifle in the middle of the scene or just put a carrying handle on it. For convenience, I have prepared the scene that you can download from GitHub from this link or open it with GitHub desktop software, which is better because you can get the next updates easily. The name of the file is RifleScopes, which is located in the Scenes folder.

The prepared scene consists of four simple buttons, and I made an image for each button, which you can see in the image below. In the script file, I wrote a method for each button, which is executed by clicking on the corresponding button. I explained how to assign a method to a button in previous posts.

You can examine all the codes written for this example below. As I explained in the post about the Array, here I used an array variable to access the models inside the scene to which I can connect the models inside Unity. So far, there is no new content that is not in the previous posts.

using UnityEngine;

public class rifleScopes : MonoBehaviour
{
    public GameObject rifle;
    public GameObject[] scopesHandle;

    public void rifleOnOff() 
    {
        if (rifle.activeSelf)
        {
            rifle.SetActive(false);
        } 
        else
        {
            rifle.SetActive(true);
        }
    }
    
    public void handle()
    {
        for (int i = 0; i < scopesHandle.Length; i++)
        {
            scopesHandle[i].SetActive(false);
        }
        scopesHandle[0].SetActive(true);
    }
    
    public void scopeA()
    {
        for (int i = 0; i < scopesHandle.Length; i++)
        {
            scopesHandle[i].SetActive(false);
        }
        scopesHandle[1].SetActive(true);
    }
    
    public void scopeB()
    {
        for (int i = 0; i < scopesHandle.Length; i++)
        {
            scopesHandle[i].SetActive(false);
        }
        scopesHandle[2].SetActive(true);
    }
}

In the eighth line, I defined a method to enable or disable the entire rifle model in the scene, which means to turn the model on and off. The new thing here is a code inside the parentheses in line 10 that acts like a switch. What it does is that it checks to see if the desired game object is active in Unity or not, that is, whether the tick on the top of the inspector tab is on or not. Now, if it is on, it will be disabled with the code line inside the body and vice versa.

To activate a model like the red scope and disable the other models like the blue scope and the carrying handle, I chose a simple method that I used in the next three Methods in lines 20, 29 and 38. First, I disable all the models with a For Loop and then activate the model that is desired in the next line. You can use this method in all single choices between a number of models. The video below is a personal project that I am developing in Unity, in which I used the same way.

I downloaded the models of this project from the Sketchfab website, which you can check from the following links:

Leave a Reply

Your email address will not be published. Required fields are marked *