极致21点开发DAY3

今天完成的主要任务是活动窗口的显示与关闭,以及领取金币的逻辑。用到了数据持久化技术。我想记录的主要是领取金币的逻辑。

领取金币算法:如果今天没有领取金币,即可领取,否则什么都不做。

一句话描述足矣,但实现起来并不是那么简单。涉及到保存是否领金币状态,以及第几次领取金币。还是用代码说话吧:

 using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class ActionController : MonoBehaviour
{
public Button closeBtn;
public Button recvBtn;
public Sprite[] goldSprite = new Sprite[];
public Sprite recvSprite; public Transform parent;
private List<GameObject> itemList = new List<GameObject>();
//今天点击日期
private string currentClickDate = "";
//上一次点击日期
private string lastClickDate = "";
private int dateFlag = ;
private void Start()
{
// PlayerPrefs.DeleteAll();
Init();
currentClickDate = DateTime.Today.ToLongDateString();
lastClickDate = PlayerPrefs.GetString("LastClickDate");
LoadActionItems(); } private void Init()
{ closeBtn.onClick.AddListener(OnCloseBtn);
recvBtn.onClick.AddListener(OnRecvBtn); } private void OnCloseBtn()
{
gameObject.SetActive(false);
}
//如果上一次点击日期和今天日期不同,并且今天还没有点击,则更新UI。
private void OnRecvBtn()
{ //从来没有点击过
if (string.IsNullOrEmpty(lastClickDate))
{
dateFlag = ; }
else
{
++dateFlag;
dateFlag %= ;
}
PlayerPrefs.SetInt("DateFlag", dateFlag);
//不是今天第一次点击,则什么都不做
if (lastClickDate.Equals(currentClickDate))
{
Debug.Log("今天的金币已领取");
return;
}
else
{ lastClickDate = currentClickDate;
PlayerPrefs.SetString("LastClickDate", lastClickDate);
itemList[dateFlag - ].transform.GetChild().gameObject.SetActive(true);
}
}
private void LoadActionItems()
{ ActionData temp = new ActionData();
for (int i = ; i < ; i++)
{
//加载资源
UnityEngine.Object obj = Resources.Load("Prefabs/actionitem");
GameObject go = Instantiate(obj) as GameObject;
itemList.Add(go);
//初始化资源
go.transform.SetParent(parent);
go.transform.localPosition = new Vector3(, , );
go.transform.localScale = new Vector3(, , ); temp.day = "第" + (i + ) + "天";
temp.goldNum = (i + ).ToString();
temp.goldSprite = goldSprite[i];
temp.receiveSprite = recvSprite; go.GetComponent<ActionItem>().SetActionItem(temp);
}
//加载完资源时,更新UI
if (lastClickDate.Equals(currentClickDate))
{
itemList[dateFlag].transform.GetChild().gameObject.SetActive(true);
}
}
}

效果图:极致21点开发DAY3

上一篇:Ubuntu 安装mod_python配置Apache2


下一篇:在eclipse中怎么安装插件