| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JC;
- /* 商城界面 */
- public class ShopView : JCUnityLib.ViewBase, MenuBackInterface
- {
- [SerializeField] Sprite[] optionSprites;
- [SerializeField] GameObject options;
- [SerializeField] ScrollPanel productsPanel;
- [SerializeField] ScrollPanel bagPanel;
- [SerializeField] ScrollPanel equippedPanel;
- [SerializeField] GameObject introduceItem;
- public static ShopView ins;
- void Start() {
- ins = this;
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- TopBarView.NeedShowIt(this);
- InitOptions();
- }
- void OnDestroy()
- {
- if (ins == this) ins = null;
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- TopBarView.DontNeedShowIt(this);
- }
- public bool OnMenuBack() {
- ViewMgr.Instance.DestroyView<ShopView>();
- return true;
- }
- void InitOptions() {
- for (int i = 0; i < this.options.transform.childCount; i++) {
- int index = i;
- Button button = this.options.transform.GetChild(i).gameObject.AddComponent<Button>();
- button.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- this.SelectOption(index);
- });
- }
- SelectOption(0);
- }
- int optionID = 0;
- void SelectOption(int index) {
- optionID = index;
- for (int i = 0; i < this.options.transform.childCount; i++) {
- this.options.transform.GetChild(i).GetComponent<Image>().sprite = this.optionSprites[0];
- }
- this.options.transform.GetChild(index).GetComponent<Image>().sprite = this.optionSprites[1];
- productsPanel.gameObject.SetActive(index == 0);
- if (index == 0) InitProductsPanel();
- bagPanel.gameObject.SetActive(index == 1);
- if (index == 1) InitBagPanel();
- equippedPanel.gameObject.SetActive(index == 2);
- if (index == 2) InitEquippedPanel();
- }
- // void AddText1ToItem(GameObject gameObject, int scaleValue)
- // {
- // GameObject node = new GameObject();
- // node.transform.SetParent(gameObject.transform);
- // node.transform.localScale = new Vector3(1, 1, 1);
- // Text t = node.AddComponent<Text>();
- // node.transform.localPosition = new Vector3();
- // t.text = "X" + scaleValue;
- // node.layer = LayerMask.NameToLayer("UI");
- // t.font = Resources.Load<Font>("Fonts/智能黑体");
- // t.alignment = TextAnchor.MiddleCenter;
- // t.fontSize = 32;
- // t.color = Color.black;
- // }
- bool _InitProductsPanel = false;
- void InitProductsPanel()
- {
- List<PropConfig> propConfigs = PropMgr.ins.ListForShop();
- if (!_InitProductsPanel) {
- _InitProductsPanel = true;
- productsPanel.onReceiveItemViewInfo.AddListener(onReceiveItemViewInfo);
- productsPanel.onItemChange.AddListener(delegate(RectTransform item, int index) {
- PropConfig propConfig = propConfigs[index];
- Image img = introduceItem.transform.Find("Box/Image").GetComponent<Image>();
- img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
- img.SetNativeSize();
- TextAutoLanguage autoLanguage = introduceItem.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
- autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
- autoLanguage.SetText(int.Parse(propConfig.name[0]));
- TextAutoLanguage autoLanguage1 = introduceItem.transform.Find("Detail").GetComponent<TextAutoLanguage>();
- autoLanguage1.textFormatArgs = GetFormatArgs(propConfig.detail);
- autoLanguage1.SetText(int.Parse(propConfig.detail[0]));
- });
- foreach (var propConfig in propConfigs)
- {
- GameObject item = GameObject.Instantiate<GameObject>(
- productsPanel.prefabs[0],
- Vector3.zero, Quaternion.identity,
- productsPanel.GetComponent<ScrollRect>().content
- );
- item.SetActive(true);
- productsPanel.AddItem(item.GetComponent<RectTransform>());
- Image img = item.transform.Find("Box/Image").GetComponent<Image>();
- img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
- img.SetNativeSize();
- // if (propConfig.type == 1) {
- // AddText1ToItem(img.gameObject, ((PropScaleAim) propConfig).scaleValue);
- // } else if (propConfig.type == 2) {
- // AddText1ToItem(img.gameObject, ((PropScaleShoot) propConfig).scaleValue);
- // }
- TextAutoLanguage autoLanguage = item.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
- autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
- autoLanguage.SetText(int.Parse(propConfig.name[0]));
- Image costIcon = item.transform.Find("Cost/Icon").GetComponent<Image>();
- costIcon.sprite = Resources.Load<Sprite>("Textures/Common/Icon003");
- Text costValue = item.transform.Find("Cost/Text").GetComponent<Text>();
- costValue.text = propConfig.diamond.ToString();
- Button btnBuy = item.transform.Find("BtnBuy").GetComponent<Button>();
-
- if (PropMgr.ins.isSoldOut(propConfig))
- {
- btnBuy.interactable = false;
- btnBuy.GetComponentInChildren<TextAutoLanguage>().SetText(107);
- }
- else
- {
- btnBuy.onClick.AddListener(delegate() {
- PropMgr.ins.buyProp(propConfig);
- if (PropMgr.ins.isSoldOut(propConfig)) {
- btnBuy.interactable = false;
- btnBuy.GetComponentInChildren<TextAutoLanguage>().SetText(107);
- }
- });
- }
- }
- productsPanel.UpdateItems();
- productsPanel.SelectItemByStartIndex();
- }
- introduceItem.SetActive(propConfigs.Count > 0);
- }
- void InitBagPanel()
- {
- bagPanel.ClearItems();
- bagPanel.onReceiveItemViewInfo.RemoveAllListeners();
- bagPanel.onReceiveItemViewInfo.AddListener(onReceiveItemViewInfo);
- List<PropInfo> propInfos = PropMgr.ins.ListForBag();
- bagPanel.onItemChange.RemoveAllListeners();
- bagPanel.onItemChange.AddListener(delegate(RectTransform item, int index) {
- PropConfig propConfig = propInfos[index].config;
- Image img = introduceItem.transform.Find("Box/Image").GetComponent<Image>();
- img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
- img.SetNativeSize();
- TextAutoLanguage autoLanguage = introduceItem.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
- autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
- autoLanguage.SetText(int.Parse(propConfig.name[0]));
- TextAutoLanguage autoLanguage1 = introduceItem.transform.Find("Detail").GetComponent<TextAutoLanguage>();
- autoLanguage1.textFormatArgs = GetFormatArgs(propConfig.detail);
- autoLanguage1.SetText(int.Parse(propConfig.detail[0]));
- });
- foreach (var propInfo in propInfos)
- {
- var propConfig = propInfo.config;
- GameObject item = GameObject.Instantiate<GameObject>(
- bagPanel.prefabs[0],
- Vector3.zero, Quaternion.identity,
- bagPanel.GetComponent<ScrollRect>().content
- );
- item.SetActive(true);
- bagPanel.AddItem(item.GetComponent<RectTransform>());
- Image img = item.transform.Find("Box/Image").GetComponent<Image>();
- img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
- img.SetNativeSize();
- // if (propConfig.type == 1) {
- // AddText1ToItem(img.gameObject, ((PropScaleAim) propConfig).scaleValue);
- // } else if (propConfig.type == 2) {
- // AddText1ToItem(img.gameObject, ((PropScaleShoot) propConfig).scaleValue);
- // }
- TextAutoLanguage autoLanguage = item.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
- autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
- autoLanguage.SetText(int.Parse(propConfig.name[0]));
- Button btnUse = item.transform.Find("BtnUse").GetComponent<Button>();
- btnUse.interactable = !propInfo.inuse;
- btnUse.GetComponentInChildren<TextAutoLanguage>().SetText(propInfo.inuse ? 109 : 108);
- btnUse.onClick.AddListener(delegate() {
- PropMgr.ins.useProp(propInfo);
- int i = 0;
- foreach (var prop in propInfos)
- {
- Button btn1 = bagPanel.itemList[i++].transform.Find("BtnUse").GetComponent<Button>();
- btn1.interactable = !prop.inuse;
- btn1.GetComponentInChildren<TextAutoLanguage>().SetText(prop.inuse ? 109 : 108);
- }
- });
- }
- bagPanel.UpdateItems();
- bagPanel.SelectItemByStartIndex();
- introduceItem.SetActive(propInfos.Count > 0);
- }
- void InitEquippedPanel()
- {
- equippedPanel.ClearItems();
- equippedPanel.onReceiveItemViewInfo.RemoveAllListeners();
- equippedPanel.onReceiveItemViewInfo.AddListener(onReceiveItemViewInfo);
- List<PropInfo> propInfos = PropMgr.ins.ListForEquipped();
- equippedPanel.onItemChange.RemoveAllListeners();
- equippedPanel.onItemChange.AddListener(delegate(RectTransform item, int index) {
- propInfos = PropMgr.ins.ListForEquipped();//重新拉取,因为可能增删导致数据有变
- PropConfig propConfig = propInfos[index].config;
- Image img = introduceItem.transform.Find("Box/Image").GetComponent<Image>();
- img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
- img.SetNativeSize();
- TextAutoLanguage autoLanguage = introduceItem.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
- autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
- autoLanguage.SetText(int.Parse(propConfig.name[0]));
- TextAutoLanguage autoLanguage1 = introduceItem.transform.Find("Detail").GetComponent<TextAutoLanguage>();
- autoLanguage1.textFormatArgs = GetFormatArgs(propConfig.detail);
- autoLanguage1.SetText(int.Parse(propConfig.detail[0]));
- });
- foreach (var propInfo in propInfos)
- {
- var propConfig = propInfo.config;
- GameObject item = GameObject.Instantiate<GameObject>(
- equippedPanel.prefabs[0],
- Vector3.zero, Quaternion.identity,
- equippedPanel.GetComponent<ScrollRect>().content
- );
- item.SetActive(true);
- equippedPanel.AddItem(item.GetComponent<RectTransform>());
- Image img = item.transform.Find("Box/Image").GetComponent<Image>();
- img.sprite = Resources.Load<Sprite>("Textures/Prop/" + propConfig.iconID);
- img.SetNativeSize();
- // if (propConfig.type == 1) {
- // AddText1ToItem(img.gameObject, ((PropScaleAim) propConfig).scaleValue);
- // } else if (propConfig.type == 2) {
- // AddText1ToItem(img.gameObject, ((PropScaleShoot) propConfig).scaleValue);
- // }
- TextAutoLanguage autoLanguage = item.transform.Find("TextFrame/Text").GetComponent<TextAutoLanguage>();
- autoLanguage.textFormatArgs = GetFormatArgs(propConfig.name);
- autoLanguage.SetText(int.Parse(propConfig.name[0]));
- Button btnCancel = item.transform.Find("BtnCancel").GetComponent<Button>();
-
- btnCancel.onClick.AddListener(delegate() {
- PropMgr.ins.useProp(propInfo);
- if (!propInfo.inuse) {
- equippedPanel.RemoveItem(item.GetComponent<RectTransform>());
- equippedPanel.UpdateItems();
- equippedPanel.MoveNearestItemToCenter();
- introduceItem.SetActive(propInfos.Count > 0);
- }
- });
- }
- equippedPanel.UpdateItems();
- equippedPanel.SelectItemByStartIndex();
- introduceItem.SetActive(propInfos.Count > 0);
- }
- string[] GetFormatArgs(string[] array)
- {
- string[] newArray = new string[array.Length - 1];
- for (int i = 1; i < array.Length; i++)
- {
- newArray[i - 1] = array[i];
- }
- return newArray;
- }
- void onReceiveItemViewInfo(RectTransform item, Vector3 positionInView, Vector2 viewSize)
- {
- float distanceRate = Mathf.Abs(positionInView.x * 2) / viewSize.x;
- float scaleRate = 1f - 0.2f * distanceRate;
- item.Find("Box").localScale = new Vector3(scaleRate, scaleRate, 1);
- }
- public void PointerLeft()
- {
- if (optionID == 0) productsPanel.MoveNextToCenter(-1);
- if (optionID == 1) bagPanel.MoveNextToCenter(-1);
- if (optionID == 2) equippedPanel.MoveNextToCenter(-1);
- }
- public void PointerRight()
- {
- if (optionID == 0) productsPanel.MoveNextToCenter(1);
- if (optionID == 1) bagPanel.MoveNextToCenter(1);
- if (optionID == 2) equippedPanel.MoveNextToCenter(1);
- }
- public void Back() {
- AudioMgr.ins.PlayBtn();
- ViewMgr.Instance.DestroyView<ShopView>();
- }
- }
|