ShopView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using JC;
  7. /* 商城界面 */
  8. public class ShopView : MonoBehaviour
  9. {
  10. [SerializeField] Sprite[] optionSprites;
  11. [SerializeField] GameObject options;
  12. [SerializeField] ScrollPanel productsPanel;
  13. [SerializeField] ScrollPanel bagPanel;
  14. [SerializeField] ScrollPanel equippedPanel;
  15. [SerializeField] GameObject introduceItem;
  16. void Start() {
  17. InitOptions();
  18. }
  19. void InitOptions() {
  20. for (int i = 0; i < this.options.transform.childCount; i++) {
  21. int index = i;
  22. Button button = this.options.transform.GetChild(i).gameObject.AddComponent<Button>();
  23. button.onClick.AddListener(delegate() {
  24. AudioMgr.ins.PlayBtn();
  25. this.SelectOption(index);
  26. });
  27. }
  28. SelectOption(0);
  29. }
  30. int optionID = 0;
  31. void SelectOption(int index) {
  32. optionID = index;
  33. for (int i = 0; i < this.options.transform.childCount; i++) {
  34. this.options.transform.GetChild(i).GetComponent<Image>().sprite = this.optionSprites[0];
  35. }
  36. this.options.transform.GetChild(index).GetComponent<Image>().sprite = this.optionSprites[1];
  37. productsPanel.gameObject.SetActive(index == 0);
  38. if (index == 0) InitProductsPanel();
  39. bagPanel.gameObject.SetActive(index == 1);
  40. if (index == 1) InitBagPanel();
  41. equippedPanel.gameObject.SetActive(index == 2);
  42. if (index == 2) InitEquippedPanel();
  43. }
  44. // void AddText1ToItem(GameObject gameObject, int scaleValue)
  45. // {
  46. // GameObject node = new GameObject();
  47. // node.transform.SetParent(gameObject.transform);
  48. // node.transform.localScale = new Vector3(1, 1, 1);
  49. // Text t = node.AddComponent<Text>();
  50. // node.transform.localPosition = new Vector3();
  51. // t.text = "X" + scaleValue;
  52. // node.layer = LayerMask.NameToLayer("UI");
  53. // t.font = Resources.Load<Font>("Fonts/智能黑体");
  54. // t.alignment = TextAnchor.MiddleCenter;
  55. // t.fontSize = 32;
  56. // t.color = Color.black;
  57. // }
  58. bool _InitProductsPanel = false;
  59. void InitProductsPanel()
  60. {
  61. List<PropConfig> propConfigs = PropMgr.ins.ListForShop();
  62. if (!_InitProductsPanel) {
  63. _InitProductsPanel = true;
  64. productsPanel.onReceiveItemViewInfo.AddListener(onReceiveItemViewInfo);
  65. productsPanel.onItemChange.AddListener(delegate(RectTransform item, int index) {
  66. PropConfig propConfig = propConfigs[index];
  67. Image img = introduceItem.transform.Find("Box/Image").GetComponent<Image>();
  68. img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
  69. img.SetNativeSize();
  70. TextAutoLanguage autoLanguage = introduceItem.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
  71. autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
  72. autoLanguage.SetText(int.Parse(propConfig.name[0]));
  73. TextAutoLanguage autoLanguage1 = introduceItem.transform.Find("Detail").GetComponent<TextAutoLanguage>();
  74. autoLanguage1.textFormatArgs = GetFormatArgs(propConfig.detail);
  75. autoLanguage1.SetText(int.Parse(propConfig.detail[0]));
  76. });
  77. foreach (var propConfig in propConfigs)
  78. {
  79. GameObject item = GameObject.Instantiate<GameObject>(
  80. productsPanel.prefabs[0],
  81. Vector3.zero, Quaternion.identity,
  82. productsPanel.GetComponent<ScrollRect>().content
  83. );
  84. item.SetActive(true);
  85. productsPanel.AddItem(item.GetComponent<RectTransform>());
  86. Image img = item.transform.Find("Box/Image").GetComponent<Image>();
  87. img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
  88. img.SetNativeSize();
  89. // if (propConfig.type == 1) {
  90. // AddText1ToItem(img.gameObject, ((PropScaleAim) propConfig).scaleValue);
  91. // } else if (propConfig.type == 2) {
  92. // AddText1ToItem(img.gameObject, ((PropScaleShoot) propConfig).scaleValue);
  93. // }
  94. TextAutoLanguage autoLanguage = item.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
  95. autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
  96. autoLanguage.SetText(int.Parse(propConfig.name[0]));
  97. Image costIcon = item.transform.Find("Cost/Icon").GetComponent<Image>();
  98. costIcon.sprite = Resources.Load<Sprite>("Textures/Common/Icon003");
  99. Text costValue = item.transform.Find("Cost/Text").GetComponent<Text>();
  100. costValue.text = propConfig.diamond.ToString();
  101. Button btnBuy = item.transform.Find("BtnBuy").GetComponent<Button>();
  102. if (PropMgr.ins.isSoldOut(propConfig))
  103. {
  104. btnBuy.interactable = false;
  105. btnBuy.GetComponentInChildren<TextAutoLanguage>().SetText(107);
  106. }
  107. else
  108. {
  109. btnBuy.onClick.AddListener(delegate() {
  110. PropMgr.ins.buyProp(propConfig);
  111. if (PropMgr.ins.isSoldOut(propConfig)) {
  112. btnBuy.interactable = false;
  113. btnBuy.GetComponentInChildren<TextAutoLanguage>().SetText(107);
  114. }
  115. });
  116. }
  117. }
  118. productsPanel.UpdateItems();
  119. productsPanel.SelectItemByStartIndex();
  120. }
  121. introduceItem.SetActive(propConfigs.Count > 0);
  122. }
  123. void InitBagPanel()
  124. {
  125. bagPanel.ClearItems();
  126. bagPanel.onReceiveItemViewInfo.RemoveAllListeners();
  127. bagPanel.onReceiveItemViewInfo.AddListener(onReceiveItemViewInfo);
  128. List<PropInfo> propInfos = PropMgr.ins.ListForBag();
  129. bagPanel.onItemChange.RemoveAllListeners();
  130. bagPanel.onItemChange.AddListener(delegate(RectTransform item, int index) {
  131. PropConfig propConfig = propInfos[index].config;
  132. Image img = introduceItem.transform.Find("Box/Image").GetComponent<Image>();
  133. img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
  134. img.SetNativeSize();
  135. TextAutoLanguage autoLanguage = introduceItem.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
  136. autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
  137. autoLanguage.SetText(int.Parse(propConfig.name[0]));
  138. TextAutoLanguage autoLanguage1 = introduceItem.transform.Find("Detail").GetComponent<TextAutoLanguage>();
  139. autoLanguage1.textFormatArgs = GetFormatArgs(propConfig.detail);
  140. autoLanguage1.SetText(int.Parse(propConfig.detail[0]));
  141. });
  142. foreach (var propInfo in propInfos)
  143. {
  144. var propConfig = propInfo.config;
  145. GameObject item = GameObject.Instantiate<GameObject>(
  146. bagPanel.prefabs[0],
  147. Vector3.zero, Quaternion.identity,
  148. bagPanel.GetComponent<ScrollRect>().content
  149. );
  150. item.SetActive(true);
  151. bagPanel.AddItem(item.GetComponent<RectTransform>());
  152. Image img = item.transform.Find("Box/Image").GetComponent<Image>();
  153. img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
  154. img.SetNativeSize();
  155. // if (propConfig.type == 1) {
  156. // AddText1ToItem(img.gameObject, ((PropScaleAim) propConfig).scaleValue);
  157. // } else if (propConfig.type == 2) {
  158. // AddText1ToItem(img.gameObject, ((PropScaleShoot) propConfig).scaleValue);
  159. // }
  160. TextAutoLanguage autoLanguage = item.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
  161. autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
  162. autoLanguage.SetText(int.Parse(propConfig.name[0]));
  163. Button btnUse = item.transform.Find("BtnUse").GetComponent<Button>();
  164. btnUse.interactable = !propInfo.inuse;
  165. btnUse.GetComponentInChildren<TextAutoLanguage>().SetText(propInfo.inuse ? 109 : 108);
  166. btnUse.onClick.AddListener(delegate() {
  167. PropMgr.ins.useProp(propInfo);
  168. int i = 0;
  169. foreach (var prop in propInfos)
  170. {
  171. Button btn1 = bagPanel.itemList[i++].transform.Find("BtnUse").GetComponent<Button>();
  172. btn1.interactable = !prop.inuse;
  173. btn1.GetComponentInChildren<TextAutoLanguage>().SetText(prop.inuse ? 109 : 108);
  174. }
  175. });
  176. }
  177. bagPanel.UpdateItems();
  178. bagPanel.SelectItemByStartIndex();
  179. introduceItem.SetActive(propInfos.Count > 0);
  180. }
  181. void InitEquippedPanel()
  182. {
  183. equippedPanel.ClearItems();
  184. equippedPanel.onReceiveItemViewInfo.RemoveAllListeners();
  185. equippedPanel.onReceiveItemViewInfo.AddListener(onReceiveItemViewInfo);
  186. List<PropInfo> propInfos = PropMgr.ins.ListForEquipped();
  187. equippedPanel.onItemChange.RemoveAllListeners();
  188. equippedPanel.onItemChange.AddListener(delegate(RectTransform item, int index) {
  189. propInfos = PropMgr.ins.ListForEquipped();//重新拉取,因为可能增删导致数据有变
  190. PropConfig propConfig = propInfos[index].config;
  191. Image img = introduceItem.transform.Find("Box/Image").GetComponent<Image>();
  192. img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
  193. img.SetNativeSize();
  194. TextAutoLanguage autoLanguage = introduceItem.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
  195. autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
  196. autoLanguage.SetText(int.Parse(propConfig.name[0]));
  197. TextAutoLanguage autoLanguage1 = introduceItem.transform.Find("Detail").GetComponent<TextAutoLanguage>();
  198. autoLanguage1.textFormatArgs = GetFormatArgs(propConfig.detail);
  199. autoLanguage1.SetText(int.Parse(propConfig.detail[0]));
  200. });
  201. foreach (var propInfo in propInfos)
  202. {
  203. var propConfig = propInfo.config;
  204. GameObject item = GameObject.Instantiate<GameObject>(
  205. equippedPanel.prefabs[0],
  206. Vector3.zero, Quaternion.identity,
  207. equippedPanel.GetComponent<ScrollRect>().content
  208. );
  209. item.SetActive(true);
  210. equippedPanel.AddItem(item.GetComponent<RectTransform>());
  211. Image img = item.transform.Find("Box/Image").GetComponent<Image>();
  212. img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
  213. img.SetNativeSize();
  214. // if (propConfig.type == 1) {
  215. // AddText1ToItem(img.gameObject, ((PropScaleAim) propConfig).scaleValue);
  216. // } else if (propConfig.type == 2) {
  217. // AddText1ToItem(img.gameObject, ((PropScaleShoot) propConfig).scaleValue);
  218. // }
  219. TextAutoLanguage autoLanguage = item.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
  220. autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
  221. autoLanguage.SetText(int.Parse(propConfig.name[0]));
  222. Button btnCancel = item.transform.Find("BtnCancel").GetComponent<Button>();
  223. btnCancel.onClick.AddListener(delegate() {
  224. PropMgr.ins.useProp(propInfo);
  225. if (!propInfo.inuse) {
  226. equippedPanel.RemoveItem(item.GetComponent<RectTransform>());
  227. equippedPanel.UpdateItems();
  228. equippedPanel.MoveNearestItemToCenter();
  229. introduceItem.SetActive(propInfos.Count > 0);
  230. }
  231. });
  232. }
  233. equippedPanel.UpdateItems();
  234. equippedPanel.SelectItemByStartIndex();
  235. introduceItem.SetActive(propInfos.Count > 0);
  236. }
  237. string[] GetFormatArgs(string[] array)
  238. {
  239. string[] newArray = new string[array.Length - 1];
  240. for (int i = 1; i < array.Length; i++)
  241. {
  242. newArray[i - 1] = array[i];
  243. }
  244. return newArray;
  245. }
  246. void onReceiveItemViewInfo(RectTransform item, Vector3 positionInView, Vector2 viewSize)
  247. {
  248. float distanceRate = Mathf.Abs(positionInView.x * 2) / viewSize.x;
  249. float scaleRate = 1f - 0.2f * distanceRate;
  250. item.Find("Box").localScale = new Vector3(scaleRate, scaleRate, 1);
  251. }
  252. public void PointerLeft()
  253. {
  254. if (optionID == 0) productsPanel.MoveNextToCenter(-1);
  255. if (optionID == 1) bagPanel.MoveNextToCenter(-1);
  256. if (optionID == 2) equippedPanel.MoveNextToCenter(-1);
  257. }
  258. public void PointerRight()
  259. {
  260. if (optionID == 0) productsPanel.MoveNextToCenter(1);
  261. if (optionID == 1) bagPanel.MoveNextToCenter(1);
  262. if (optionID == 2) equippedPanel.MoveNextToCenter(1);
  263. }
  264. public void Back() {
  265. AudioMgr.ins.PlayBtn();
  266. Destroy(this.gameObject);
  267. }
  268. }