• 极致21点开发DAY4


    完成的内容:1.修改上一篇博文中的Bug  2.完成任务窗口逻辑

      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 = string.Empty;
     18     //上一次点击日期
     19     private string lastClickDate = string.Empty;
     20     //已领取金币的天数                                      
     21     private int recvCount;
     22   ///  private int totalRecvCount = 0;
     23     ////是否已重置recvCount
     24     //private int resetRecvCount = -1;         //  0表示没有重置,其他值表示已重置
     25     ////是否从未领取金币        非 0领取过,其他值表示未领取过
     26     private int hasRecv = 0;
     27     private void Start()
     28     {
     29         PlayerPrefs.DeleteAll();
     30         Init();
     31        //获取当前日期
     32         currentClickDate = DateTime.Today.ToLongDateString();
     33         //currentClickDate = "2019年1月9日";
     34        
     35         Debug.Log(currentClickDate);
     36         Debug.Log(lastClickDate);
     37         Debug.Log(recvCount.ToString());
     38         LoadActionItems();
     39 
     40     }
     41 
     42     private void Init()
     43     {
     44 
     45         closeBtn.onClick.AddListener(OnCloseBtn);
     46         recvBtn.onClick.AddListener(OnRecvBtn);
     47 
     48         lastClickDate = PlayerPrefs.GetString("LastClickDate");
     49         recvCount = PlayerPrefs.GetInt("RecvCount");                  //空返回null
     50         hasRecv = PlayerPrefs.GetInt("HasRecv");
     51         //resetRecvCount = PlayerPrefs.GetInt("ResetRecvCount");        //空返回0
     52     }
     53 
     54     private void OnCloseBtn()
     55     {
     56         gameObject.SetActive(false);
     57     }
     58     //如果上一次点击日期和今天日期不同,并且今天还没有点击,则更新UI。
     59     private void OnRecvBtn()
     60     {
     61         
     62         //不是今天第一次点击,则什么都不做
     63         if (lastClickDate.Equals(currentClickDate))
     64         {
     65             Debug.Log("今天的金币已领取");
     66             return;
     67         }
     68         else
     69         {
     70             recvCount++;
     71             //totalRecvCount++;
     72             if (recvCount == itemList.Count)
     73             {
     74                 recvCount = 0;
     75                 //resetRecvCount = 1;      //已重置领取天数标志
     76                 PlayerPrefs.SetInt("ResetRecvCount", 1);
     77             }
     78 
     79             if(recvCount == 0)
     80             {
     81                 itemList[itemList.Count - 1].transform.GetChild(2).gameObject.SetActive(true);
     82             }
     83             else
     84             {
     85                 //领取金币次数+1
     86                 itemList[recvCount - 1].transform.GetChild(2).gameObject.SetActive(true);
     87 
     88             }
     89             lastClickDate = currentClickDate;
     90             PlayerPrefs.SetInt("RecvCount", recvCount);
     91             //PlayerPrefs.SetInt("TotalRecvCount", totalRecvCount);
     92             PlayerPrefs.SetString("LastClickDate", lastClickDate);
     93             PlayerPrefs.SetInt("HasRecv", 1);
     94         }
     95     }
     96     private void LoadActionItems()
     97     {
     98 
     99         ActionData temp = new ActionData();
    100         for (int i = 0; i < goldSprite.Length; i++)
    101         {
    102             //加载资源
    103             UnityEngine.Object obj = Resources.Load("Prefabs/actionitem");
    104             GameObject go = Instantiate(obj) as GameObject;
    105             itemList.Add(go);
    106             //初始化资源
    107             go.transform.SetParent(parent);
    108             go.transform.localPosition = new Vector3(0, 0, 0);
    109             go.transform.localScale = new Vector3(1, 1, 1);
    110 
    111             temp.day = "" + (i + 1) + "";
    112             temp.goldNum = (i + 1).ToString();
    113             temp.goldSprite = goldSprite[i];
    114             temp.receiveSprite = recvSprite;
    115 
    116             go.GetComponent<ActionItem>().SetActionItem(temp);
    117         }
    118         //加载完资源时,更新之前已领取金币的UI
    119         UpdateRecvInfo();
    120         Debug.Log(recvCount);
    121     }
    122 
    123     /// <summary>
    124     /// 更新领取金币UI
    125     /// </summary>
    126     private void UpdateRecvInfo()
    127     {
    128         //如果从未领取过,则什么都不做
    129         if(hasRecv == 0)
    130         {
    131             return;
    132         }
    133         //读取的recvCount值总是在0-6之间
    134         
    135         //      如果recvCounmt = 0并且今天点击过,说明重置了recvCount,在显示时应该按实际天数显示。
    136         if(lastClickDate.Equals(currentClickDate) && recvCount == 0)
    137         {
    138             //还原recvCount,但是不写入文件
    139             recvCount = itemList.Count;
    140         }
    141         for (int i = 0; i < recvCount; i++)
    142         {
    143             itemList[i].transform.GetChild(2).gameObject.SetActive(true);
    144 
    145         }
    146     }
    147 }
     1 //数据持久化 TO DO
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 using UnityEngine.UI;
     5 namespace PureMVCDemo
     6 {
     7     public class TaskController : MonoBehaviour
     8     {
     9         public GameObject taskItemPrefab;
    10         public string[] names;
    11         public Transform parent;
    12         public int taskItemCount = 3;
    13         public Sprite receivedSprite;
    14         public Sprite nonReceivedSprite;
    15         public Button closeBtn;
    16         private List<TaskItem> taskItemList = new List<TaskItem>();
    17         
    18         private void Start()
    19         {
    20             closeBtn.onClick.AddListener(OnCloseBtn);
    21             Init();
    22         }
    23 
    24         private void OnCloseBtn()
    25         {
    26             this.gameObject.SetActive(false);
    27         }
    28 
    29         private void Init()
    30         {
    31             for (int i = 0; i < taskItemCount; i++)
    32             {
    33                 GameObject go = Instantiate(taskItemPrefab);
    34                 go.transform.SetParent(parent);
    35                 go.transform.localScale = new Vector3(1, 1, 1);
    36                 taskItemList.Add(go.GetComponent<TaskItem>());
    37                 UpdateTaskData(taskItemList[i], i);
    38             }
    39         }
    40         private void UpdateTaskData(TaskItem taskItem, int nameIndex)
    41         {
    42             taskItem.nameText.text = names[nameIndex];
    43             if(taskItem.hasReceived)
    44             {
    45                 taskItem.recvImg.sprite = receivedSprite;
    46             }
    47             else
    48             {
    49                 taskItem.recvImg.sprite = nonReceivedSprite;
    50             }
    51            
    52         }
    53     }
    54       
    55 }
    任务系统的数据结构更加复杂,还有计时系统也比较困难,数据持久化还没处理,为了尽快完成游戏雏形,这些功能放在以后在做。
  • 相关阅读:
    联赛练习:好数
    CF703D Mishka and Interesting sum
    POJ2689 Prime Distance
    联赛练习:draw
    题解:luogu P2634 [国家集训队]聪聪可可
    题解:luoguP3806 【模板】点分治1(在线处理询问做法)
    题解:luogu P1073 最优贸易
    题解报告——聪聪与可可
    题解报告——森林
    题解报告——星际战争
  • 原文地址:https://www.cnblogs.com/blackteeth/p/10219769.html
Copyright © 2020-2023  润新知