Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Cost to Click - Idle Clicker Kit help

Discussion in 'Scripting' started by StyledSnail, Sep 3, 2020.

  1. StyledSnail

    StyledSnail

    Joined:
    Aug 23, 2020
    Posts:
    1
    Anyone that has advanced knowledge of the free Idle Clicker Kit template asset:
    I'm trying to add a clicker that costs multiple types of clicks to click it.
    Here's the files that the asset comes with that I would think would include any specific code needed to help.
    (I'm a unity newbie)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. namespace IdleClickerKit {
    6.  
    7.     /// <summary>
    8.     /// A thing that is clicked.
    9.     /// </summary>
    10.     public class Clicker : Persistable {
    11.  
    12.         /// <summary>
    13.         /// The click type to get or blank for default.
    14.         /// </summary>
    15.         [Tooltip ("The click type to get or blank for default.")]
    16.         [SerializeField]
    17.         protected string clickName;
    18.  
    19.         /// <summary>
    20.         /// Attach the UI click event to this to do a click.
    21.         /// </summary>
    22.         virtual public void Click() {
    23.             ClickManager.GetInstance(clickName).AddClicks (ClickManager.GetInstance(clickName).ClickIncrement);
    24.         }
    25.        
    26.  
    27.  
    28.         // Note although clickers don't save anything we extend persistable so that extensions class like the cool down
    29.         // clicker can persiste something
    30.  
    31.         #region Persistable
    32.  
    33.         /// <summary>
    34.         /// Gets the unique save key.
    35.         /// </summary>
    36.         override public string UniqueSaveKey {
    37.             get {
    38.                 return "Data_Clicker_" + clickName + "_" + gameObject.name;
    39.             }
    40.         }
    41.  
    42.         /// <summary>
    43.         /// Gets the save data.
    44.         /// </summary>
    45.         /// <value>The save data.</value>
    46.         override public object SaveData {
    47.             get {
    48.                 return new object[] {0};
    49.             }
    50.             set {
    51.             }
    52.         }
    53.  
    54.         /// <summary>
    55.         /// Get the type of object this Persistable saves.
    56.         /// </summary>
    57.         override public System.Type SavedObjectType {
    58.             get {
    59.                 return typeof(object[]);
    60.             }
    61.         }
    62.  
    63.     #endregion
    64.     }
    65. }
    66.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. namespace IdleClickerKit {
    6.     /// <summary>
    7.     /// Handles the clicks.
    8.     /// </summary>
    9.     public class ClickManager : Manager <ClickManager> {
    10.  
    11.         [Tooltip ("What we call the main click attribute")]
    12.         [SerializeField]
    13.         [ContextMenuItem ("Add 1000 Clicks", "Add1000Clicks")]
    14.         [ContextMenuItem ("Add 100000 Clicks", "Add100000Clicks")]
    15.         protected string clickName = "Clicks";
    16.  
    17.         [Tooltip ("If true this is the main click type the game is about.")]
    18.         [SerializeField]
    19.         protected bool isMainClick = true;
    20.  
    21.         protected static Dictionary<string, ClickManager> clickManagers;
    22.  
    23.         /// <summary>
    24.         /// The clicks. This is persisted.
    25.         /// </summary>
    26.         protected long clicks;
    27.  
    28.         /// <summary>
    29.         /// The clicks. This is persisted.
    30.         /// </summary>
    31.         protected long totalClicks;
    32.  
    33.         /// <summary>
    34.         /// How much we increment clicks by each click. This is caculated on start, and each new upgrade
    35.         /// affects this value. It is NOT persisted.
    36.         /// </summary>
    37.         protected int clickIncrement = 1;
    38.  
    39.         /// <summary>
    40.         /// If there is already a manager destroy self, else initialise and assign to the static reference.
    41.         /// </summary>
    42.         void Awake(){
    43.             if (isMainClick) {
    44.                 if (manager == null) {
    45.                     if (!initialised)
    46.                         Init ();
    47.                     manager = this;
    48.                 } else if (manager != this) {
    49.                     Destroy (gameObject);  
    50.                 } else if (!initialised) {
    51.                     Init ();
    52.                 }
    53.             }
    54.         }
    55.  
    56.         /// <summary>
    57.         /// Registers the new type of click manager
    58.         /// </summary>
    59.         /// <param name="manager">Manager.</param>
    60.         protected virtual void RegisterNewClickManager(ClickManager manager) {
    61.             if (clickManagers == null) clickManagers = new Dictionary<string, ClickManager> ();
    62.             if (clickManagers.ContainsKey (clickName)) {
    63.                 Debug.LogError ("Multiple ClickManagers with the same clickName defined. Destroying self.");
    64.                 Destroy (gameObject);  
    65.             } else {
    66.                 clickManagers.Add (manager.clickName, manager);
    67.             }
    68.         }
    69.  
    70.         /// <summary>
    71.         /// Gets the Instance the specified clickName.
    72.         /// </summary>
    73.         /// <param name="clickName">Click name.</param>
    74.         public static ClickManager GetInstance(string clickName) {
    75.             if (clickName == null || clickName == "") return Instance;
    76.             if (clickManagers == null) clickManagers = new Dictionary<string, ClickManager> ();
    77.             if (clickManagers.ContainsKey(clickName)) {
    78.                 return clickManagers [clickName];  
    79.             }
    80.             // Couldn't find it, lets search
    81.             ClickManager[] cms = FindObjectsOfType<ClickManager> ();
    82.             foreach (ClickManager cm in cms) {
    83.                 cm.Init ();
    84.             }
    85.             if (clickManagers.ContainsKey(clickName)) {
    86.                 return clickManagers [clickName];  
    87.             }
    88.             Debug.LogWarning(string.Format("ClickManagers with the clickName '{0}' could not be found. Returning default.", clickName));
    89.             return Instance;
    90.         }
    91.  
    92.         /// <summary>
    93.         /// Determines whether this instance is valid manager to be returned by a call to Instance.
    94.         /// </summary>
    95.         /// <returns><c>true</c> if this instance is valid manager; otherwise, <c>false</c>.</returns>
    96.         override protected bool IsValidManager {
    97.             get {
    98.                 return isMainClick;
    99.             }
    100.         }
    101.  
    102.         /// <summary>
    103.         /// Get the name of the clicks.
    104.         /// </summary>Create
    105.         /// <value>The name of the click.</value>
    106.         virtual public string ClickName {
    107.             get { return clickName; }
    108.         }
    109.  
    110.         /// <summary>
    111.         /// Publicly accessible clicks.
    112.         /// </summary>
    113.         /// <value>The clicks.</value>
    114.         virtual public long Clicks {
    115.             get  { return clicks; }
    116.         }
    117.  
    118.         /// <summary>
    119.         /// Publicly accessible total clicks.
    120.         /// </summary>
    121.         /// <value>The total clicks.</value>
    122.         virtual public long TotalClicks {
    123.             get  { return totalClicks; }
    124.         }
    125.  
    126.         /// <summary>
    127.         /// Publicly accessible click increment.
    128.         /// </summary>
    129.         /// <value>The clicks.</value>
    130.         virtual public int ClickIncrement {
    131.             get  { return clickIncrement; }
    132.         }
    133.  
    134.         /// <summary>
    135.         /// Init this instance.
    136.         /// </summary>
    137.         override protected void Init() {
    138.             if (!initialised) {
    139.                 initialised = true;
    140.                 clicks = 0;
    141.                 totalClicks = 0;
    142.                 Load (this);
    143.                 RegisterNewClickManager (this);
    144.             }
    145.         }
    146.  
    147.         /// <summary>
    148.         /// Add clicks.
    149.         /// </summary>
    150.         /// <param name="amount">Amount.</param>
    151.         public void AddClicks(int amount) {
    152.             clicks += amount;
    153.             totalClicks +=  amount;
    154.             Save (this);
    155.         }
    156.  
    157.         /// <summary>
    158.         /// Purchase something for the specified amount of clicks. Fails if there aren't enough clicks.
    159.         /// </summary>
    160.         /// <param name="amount">Amount.</param>
    161.         /// <returns>true if purchase was successful, false otherwise.</returns>
    162.         public bool Purchase(int amount) {
    163.             if (clicks >= amount) {
    164.                 clicks -= amount;
    165.                 Save (this);
    166.                 return true;
    167.             }
    168.             return false;
    169.         }
    170.  
    171.         /// <summary>
    172.         /// Increases the click increment.
    173.         /// </summary>
    174.         /// <param name="amount">Amount to increase by.</param>
    175.         public void IncreaseClickIncrement(int amount) {
    176.             clickIncrement += amount;
    177.         }
    178.  
    179.         /// <summary>
    180.         /// Reset clicks and total clicks to zero.
    181.         /// </summary>
    182.         public void ResetClicksToZero() {
    183.             clicks = 0;
    184.             totalClicks = 0;
    185.             Save (this);
    186.         }
    187.  
    188. #region Persistable
    189.  
    190.         /// <summary>
    191.         /// Gets the unique save key.
    192.         /// </summary>
    193.         override public string UniqueSaveKey {
    194.             get {
    195.                 return "Data_ClickManager_" + clickName;
    196.             }
    197.         }
    198.  
    199.         /// <summary>
    200.         /// Gets the save data.
    201.         /// </summary>
    202.         /// <value>The save data.</value>
    203.         override public object SaveData {
    204.             get {
    205.                 return new object[] {clicks, totalClicks};
    206.             }
    207.             set {
    208.                 if (value.GetType () == SavedObjectType) {
    209.                     clicks = (long)((object[])value) [0];
    210.                     totalClicks = (long)((object[])value) [0];
    211.                 }
    212.             }
    213.         }
    214.  
    215.         /// <summary>
    216.         /// Get the type of object this Persistable saves.
    217.         /// </summary>
    218.         override public System.Type SavedObjectType {
    219.             get {
    220.                 return typeof(object[]);
    221.             }
    222.         }
    223.  
    224.         /// <summary>
    225.         /// Things to do after reset.
    226.         /// </summary>
    227.         override public void PostResetAction() {
    228.             clicks = 0;
    229.             totalClicks = 0;
    230.         }
    231.  
    232.         /// <summary>
    233.         /// Adds 1,000 clicks. Used for the context menu.
    234.         /// </summary>
    235.         protected void Add1000Clicks() {
    236.             AddClicks (1000);
    237.         }
    238.  
    239.         /// <summary>
    240.         /// Adds 100,000 clicks. Used for the context menu.
    241.         /// </summary>
    242.         protected void Add100000Clicks() {
    243.             AddClicks (100000);
    244.         }
    245.  
    246. #endregion
    247.     }
    248. }
    249.  
    250.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace IdleClickerKit {
    5.    
    6.     public class ClickConverter : MonoBehaviour {
    7.  
    8.         [Tooltip ("Click name of the resource that is consumed.")]
    9.         [SerializeField]
    10.         protected string costClickName;
    11.  
    12.         [Tooltip ("Number of resources the process costs.")]
    13.         [SerializeField]
    14.         protected int costClickAmount;
    15.  
    16.         [Tooltip ("Click name of the resource that is rewarded.")]
    17.         [SerializeField]
    18.         protected string rewardClickName;
    19.  
    20.         [Tooltip ("Number of resources the process rewards.")]
    21.         [SerializeField]
    22.         protected int rewardClickAmount;
    23.  
    24.         public string CostName {
    25.             get { return costClickName; }
    26.         }
    27.  
    28.         public int CostAmount {
    29.             get { return costClickAmount; }
    30.         }
    31.  
    32.         public string RewardName {
    33.             get { return rewardClickName; }
    34.         }
    35.  
    36.         public int RewardAmount {
    37.             get { return rewardClickAmount; }
    38.         }
    39.  
    40.         public void Click() {
    41.             if (ClickManager.GetInstance (costClickName).Clicks >= costClickAmount) {
    42.                 if (ClickManager.GetInstance (costClickName).Purchase (costClickAmount)) {
    43.                     ClickManager.GetInstance (rewardClickName).AddClicks (rewardClickAmount);
    44.                 }
    45.             }
    46.         }
    47.     }
    48. }
    49.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Well,

    you would need to make a "ClickCounter" on your Clicked object and after x clicks you call the function to increment your currency