ShopView.cs 13 KB

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