Tower_Info.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class Tower_Info : MonoBehaviour {
  7. Tower tower;
  8. public LayerMask mouseMask;
  9. public Text towerName;
  10. public Text property;
  11. bool mouseOnScreen = false;
  12. public Button levelUpBtn;
  13. public Button SellBtn;
  14. // Use this for initialization
  15. public Sprite[] towerImages;
  16. public Image T_image;
  17. void Awake () {
  18. levelUpBtn.gameObject.SetActive(false);
  19. SellBtn.gameObject.SetActive(false);
  20. towerName.text = property.text= "";
  21. T_image.enabled = false;
  22. }
  23. // Update is called once per frame
  24. void Update () {
  25. mouseOnScreen = Input.mousePresent;
  26. bool mouseOnUI = EventSystem.current.IsPointerOverGameObject();
  27. RaycastHit hitInfo;
  28. bool mouseIsOverSomething = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 100, mouseMask);
  29. if (!mouseOnUI)
  30. {
  31. if (mouseOnScreen)
  32. {
  33. if (mouseIsOverSomething && Input.GetMouseButtonDown(0))
  34. {
  35. tower = hitInfo.collider.GetComponentInParent<Tower>();
  36. TowerInfoUpdate();
  37. // levelUpBtn.GetComponentInChildren<Text>().text = "▲$" + tower.upgradeCosts[tower.level+1].ToString();
  38. }
  39. else if(!mouseIsOverSomething && (Input.GetMouseButton(0))){ panelclose(); }
  40. else if (Input.GetKeyDown(KeyCode.Escape)) { panelclose(); }
  41. }
  42. }
  43. if (tower!=null) {
  44. if (tower.level + 1 >= 5) levelUpBtn.gameObject.SetActive(false);
  45. }
  46. }
  47. public void OnClick()
  48. {
  49. tower.Upgrade();
  50. TowerInfoUpdate();
  51. }
  52. public void DeleteTower(AudioClip audio)
  53. {
  54. GameObject a = new GameObject();
  55. a.AddComponent<AudioSource>().clip=audio;
  56. a.GetComponent<AudioSource>().Play();
  57. a.GetComponent<AudioSource>().volume = 0.3f;
  58. Destroy(a, 2);
  59. tower.Sell();
  60. panelclose();
  61. }
  62. /// <summary>
  63. /// 更新塔信息
  64. /// </summary>
  65. void TowerInfoUpdate()
  66. {
  67. string tl = (tower.level + 1).ToString();
  68. if (tower.level + 1 >= 5) tl = "Max";
  69. else levelUpBtn.GetComponentInChildren<Text>().text = "▲$" + tower.upgradeCosts[tower.level + 1].ToString();
  70. towerName.text = tower.Name;
  71. property.text = "Level:" + tl + "\n攻撃力:" + tower.powerdisplay+"\n範囲:"+tower.Range.ToString("f0")+"\n射速:"+tower.ReloadRate;
  72. levelUpBtn.gameObject.SetActive(true);
  73. SellBtn.gameObject.SetActive(true);
  74. T_image.sprite = towerImages[tower.Tower_ID];
  75. T_image.enabled = true;
  76. }
  77. /// <summary>
  78. /// 清除塔信息
  79. /// </summary>
  80. void panelclose()
  81. {
  82. levelUpBtn.gameObject.SetActive(false); SellBtn.gameObject.SetActive(false);
  83. towerName.text = property.text = "";
  84. T_image.enabled = false;
  85. }
  86. }