ShopView.cs 14 KB

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