ShopView.cs 14 KB

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