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