标签:etc today onclick pos items tin art oca prefab
今天完成的主要任务是活动窗口的显示与关闭,以及领取金币的逻辑。用到了数据持久化技术。我想记录的主要是领取金币的逻辑。
领取金币算法:如果今天没有领取金币,即可领取,否则什么都不做。
一句话描述足矣,但实现起来并不是那么简单。涉及到保存是否领金币状态,以及第几次领取金币。还是用代码说话吧:
1 using System; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 7 public class ActionController : MonoBehaviour 8 { 9 public Button closeBtn; 10 public Button recvBtn; 11 public Sprite[] goldSprite = new Sprite[7]; 12 public Sprite recvSprite; 13 14 public Transform parent; 15 private List<GameObject> itemList = new List<GameObject>(); 16 //今天点击日期 17 private string currentClickDate = ""; 18 //上一次点击日期 19 private string lastClickDate = ""; 20 private int dateFlag = 0; 21 private void Start() 22 { 23 // PlayerPrefs.DeleteAll(); 24 Init(); 25 currentClickDate = DateTime.Today.ToLongDateString(); 26 lastClickDate = PlayerPrefs.GetString("LastClickDate"); 27 LoadActionItems(); 28 29 } 30 31 private void Init() 32 { 33 34 closeBtn.onClick.AddListener(OnCloseBtn); 35 recvBtn.onClick.AddListener(OnRecvBtn); 36 37 } 38 39 private void OnCloseBtn() 40 { 41 gameObject.SetActive(false); 42 } 43 //如果上一次点击日期和今天日期不同,并且今天还没有点击,则更新UI。 44 private void OnRecvBtn() 45 { 46 47 //从来没有点击过 48 if (string.IsNullOrEmpty(lastClickDate)) 49 { 50 dateFlag = 1; 51 52 } 53 else 54 { 55 ++dateFlag; 56 dateFlag %= 7; 57 } 58 PlayerPrefs.SetInt("DateFlag", dateFlag); 59 //不是今天第一次点击,则什么都不做 60 if (lastClickDate.Equals(currentClickDate)) 61 { 62 Debug.Log("今天的金币已领取"); 63 return; 64 } 65 else 66 { 67 68 lastClickDate = currentClickDate; 69 PlayerPrefs.SetString("LastClickDate", lastClickDate); 70 itemList[dateFlag - 1].transform.GetChild(2).gameObject.SetActive(true); 71 } 72 } 73 private void LoadActionItems() 74 { 75 76 ActionData temp = new ActionData(); 77 for (int i = 0; i < 7; i++) 78 { 79 //加载资源 80 UnityEngine.Object obj = Resources.Load("Prefabs/actionitem"); 81 GameObject go = Instantiate(obj) as GameObject; 82 itemList.Add(go); 83 //初始化资源 84 go.transform.SetParent(parent); 85 go.transform.localPosition = new Vector3(0, 0, 0); 86 go.transform.localScale = new Vector3(1, 1, 1); 87 88 temp.day = "第" + (i + 1) + "天"; 89 temp.goldNum = (i + 1).ToString(); 90 temp.goldSprite = goldSprite[i]; 91 temp.receiveSprite = recvSprite; 92 93 go.GetComponent<ActionItem>().SetActionItem(temp); 94 } 95 //加载完资源时,更新UI 96 if (lastClickDate.Equals(currentClickDate)) 97 { 98 itemList[dateFlag].transform.GetChild(2).gameObject.SetActive(true); 99 } 100 } 101 }
效果图:
标签:etc today onclick pos items tin art oca prefab
原文地址:https://www.cnblogs.com/blackteeth/p/10209201.html