NGUICostumeItemScript.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Security.AccessControl;
  2. using UnityEngine;
  3. using System.Collections;
  4. public class NGUICostumeItemScript : MonoBehaviour {
  5. //the characters material used by costumes menu
  6. //to change the skin
  7. public Material characterMaterial;
  8. public Texture characterCostume;//the character costumes
  9. public int costumeCost;
  10. private GameObject goBuyEquipButton;
  11. private UILabel uilBuyEquipButton;
  12. private UILabel uilCostDescription;
  13. //state of the costume (false = not owned; true = owned)
  14. private bool costumeOwned;
  15. //script references
  16. private InGameScriptCS hInGameScriptCS;
  17. private NGUIMenuScript hNGUIMenuScript;
  18. public Finances.Costumes Costume;
  19. void Start ()
  20. {
  21. hInGameScriptCS = (InGameScriptCS)GameObject.Find("Player").GetComponent(typeof(InGameScriptCS));
  22. hNGUIMenuScript = (NGUIMenuScript)GameObject.Find("UI Root (2D)").GetComponent(typeof(NGUIMenuScript));
  23. goBuyEquipButton = (GameObject)this.transform.Find("Button_BuyEquip").gameObject;
  24. uilBuyEquipButton = (UILabel)this.transform.Find("Button_BuyEquip/Label").GetComponent(typeof(UILabel));
  25. uilCostDescription = (UILabel)this.transform.Find("Text_Cost").GetComponent(typeof(UILabel));
  26. //check if a meterial, texture and cost has been assigned to exposed variables
  27. if (characterMaterial == null)
  28. Debug.Log("EXCEPTION: Character material not assigned to costume shop element. Check the user documentation.");
  29. else if (characterCostume == null)
  30. Debug.Log("EXCEPTION: Character texture not assigned to costume shop element. Check the user documentation.");
  31. else if (costumeCost <= 0)
  32. Debug.Log("EXCEPTION: No cost assigned to the costume shop element. Check the user documentation.");
  33. //is this the currently applied texture?
  34. if (characterMaterial.GetTexture("_MainTex") == characterCostume)
  35. {
  36. costumeOwned = true;
  37. uilBuyEquipButton.text = "EQUIP";
  38. }
  39. else//not the currently equiped texture
  40. {
  41. costumeOwned = false;
  42. uilBuyEquipButton.text = "BUY";
  43. }
  44. //set the price on the label
  45. uilCostDescription.text = costumeCost.ToString();
  46. }//end of Start
  47. void Update()
  48. {
  49. if (Finances.HasCostume(Costume))
  50. {
  51. costumeOwned = true;
  52. uilBuyEquipButton.text = "EQUIP";
  53. uilCostDescription.text = "";
  54. }
  55. else
  56. {
  57. costumeOwned = false;
  58. uilBuyEquipButton.text = "BUY";
  59. uilCostDescription.text = Finances.GetCostumeCost(Costume).ToString();
  60. }
  61. }
  62. void OnClick()
  63. {
  64. if (!costumeOwned)//buy button tapped
  65. {
  66. Finances.BuyCostume(Costume);
  67. /*if (hInGameScriptCS.getCurrencyCount() >= costumeCost)//check if user has enough currency
  68. {
  69. //deduct the cost of costume
  70. hInGameScriptCS.alterCurrencyCount(-costumeCost);
  71. //change the texture of the character
  72. characterMaterial.SetTexture("_MainTex", characterCostume);
  73. //turn off buy and show equip button
  74. uilBuyEquipButton.text = "EQUIP";
  75. //change the costumeOwned
  76. costumeOwned = true;
  77. //take the user to the main menu
  78. hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.MainMenu);
  79. hNGUIMenuScript.CloseMenu(NGUIMenuScript.NGUIMenus.ShopCostumes);
  80. }//end of if cost == cash */
  81. }
  82. else //equip button tapped
  83. {
  84. //change the texture of the character
  85. //characterMaterial.SetTexture("_MainTex", characterCostume);
  86. Costumes.Equip(Costume);
  87. //take the user to the main menu
  88. hNGUIMenuScript.ShowMenu(NGUIMenuScript.NGUIMenus.MainMenuNew);
  89. hNGUIMenuScript.CloseMenu(NGUIMenuScript.NGUIMenus.ShopCostumes);
  90. }
  91. }//end of On Click function
  92. }