Creating An In-Game Shop

Checked with version: 5.2

-

Difficulty: Intermediate

In this live training session we will extend the Survival Shooter training project by adding an in-game shop where the player can purchase additional weapons. We'll achieve this using a combination of scriptable objects to store weapon data and the UI system.

Creating An In-Game Shop

Intermediate Scripting

WeaponObject

Code snippet

using UnityEngine;
using System.Collections;

[System.Serializable]
public class WeaponObject : ScriptableObject {

    public string weaponName = "Weapon Name Here";
    public int cost = 50;
    public string description;

    public float fireRate = .5f;
    public int damage = 10;
    public float range = 100;

}

MakeWeaponObject

Code snippet

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeWeaponObject 
{
    [MenuItem("Assets/Create/Weapon Object")]
    public static void Create()
    {
        WeaponObject asset = ScriptableObject.CreateInstance<WeaponObject> ();
        AssetDatabase.CreateAsset (asset, "Assets/NewWeaponObject.asset");
        AssetDatabase.SaveAssets ();
        EditorUtility.FocusProjectWindow ();
        Selection.activeObject = asset;
    }
    
}

WeaponButton

Code snippet

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace CompleteProject
{
    public class WeaponButton : MonoBehaviour {

        public PlayerShooting playerShooting;
        public int weaponNumber;

        public Text name;
        public Text cost;
        public Text description;

        private AudioSource source;

        // Use this for initialization
        void Start () 
        {
            source = GetComponent<AudioSource> ();
            SetButton ();
        }

        void SetButton()
        {
            string costString = playerShooting.weapons [weaponNumber].cost.ToString ();
            name.text = playerShooting.weapons [weaponNumber].name;
            cost.text = "$" + playerShooting.weapons [weaponNumber].cost;
            description.text = playerShooting.weapons [weaponNumber].description;
        }

        public void OnClick()
        {
            if (ScoreManager.score >= playerShooting.weapons [weaponNumber].cost) {
            
                ScoreManager.score -= playerShooting.weapons [weaponNumber].cost;
                playerShooting.currentWeapon = weaponNumber;

            } else 
            {
                source.Play();
            }
        }
        
    }

}

ShopController

Code snippet

using UnityEngine;
using System.Collections;

public class ShopController : MonoBehaviour {

    public GameObject shopPanel;

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag ("Player"))
            OpenShop ();

    }

    void OpenShop()
    {
        shopPanel.SetActive (true);
        Time.timeScale = 0;
    }

    public void CloseShop()
    {
        shopPanel.SetActive (false);
        Time.timeScale = 1;
    }
}

Related tutorials