用NGUI做游戏中的签到系统

                                                                      用NGUI做游戏中的签到系统

    最近的项目中要求做一个日常签到,累计签到一定次数送奖励的功能,最终效果如下图:

用NGUI做游戏中的签到系统_第1张图片

这里主要涉及到对DateTime的应用,我觉得难点在于每个月的排列显示吧,我把它从项目中分离出来,下次如果遇到或许能够快速集成到开发中,也希望能够帮到有需要的你们,客户端和服务端对接显示签到和奖励情况部分下面没写,只是做了日历显示功能,下面贴出代码研究一下,如果你有好的解决办法,欢迎下方留言,我们一起研究游戏开发

public class ClendarMgr : MonoBehaviour
{

    public GameObject DayPrefab;
    public Transform[] WeekTransform;
    public float OffstY = 0;
    private int totalDaysInMonth = 0; //记录当月的总天数
    private List DayList = new List();
    private UIButton btnLastMonth;
    private UIButton btnNextMonth;
    private UILabel lblMonth;
    private int month = 0;
    void Init(int mth = 0)
    {
          if(mth<=0||mth>12)
          {
              mth=DateTime.Now.Month;
          }
        transform.Find("Year").GetComponent().text = DateTime.Now.Year.ToString();
        transform.Find("Month").GetComponent().text = mth.ToString();
      
        totalDaysInMonth = DateTime.DaysInMonth(DateTime.Now.Year,mth);  //获得当月的总天数
        for (int i = 1; i <= totalDaysInMonth; i++)
        {
            DateTime DayInfo = new DateTime(DateTime.Now.Year, mth, i);
            string week = DayInfo.DayOfWeek.ToString();
            if (i == 1)
            {
                for (int j = 0; j < (int)DayInfo.DayOfWeek; j++)
                {
                    GameObject tempGo = new GameObject();
                    tempGo.transform.parent = WeekTransform[j];
                    DayList.Add(tempGo);
                }
            }
            Transform dayParent = transform.Find("Clendar/" + week);
            GameObject day= Instantiate(DayPrefab);
            day.transform.parent = dayParent;
            day.transform.localScale = Vector3.one;
            day.transform.localPosition = new Vector3(dayParent.transform.position.x, -dayParent.childCount * OffstY, 1);
            day.GetComponent().text = DayInfo.Day.ToString();
            DayList.Add(day);
        }
    }
    void ClearAllDay()
    {
        for (int i = 0; i < DayList.Count; i++)
        {
            DestroyImmediate(DayList[i]);
       }
    }
    void Start()
    {
        btnLastMonth = transform.Find("BtnLast").GetComponent();
        btnNextMonth = transform.Find("BtnNext").GetComponent();
        btnLastMonth.onClick.Add(new EventDelegate(OnBtnLastClicked));
        btnNextMonth.onClick.Add(new EventDelegate(OnBtnNextClicked));
        lblMonth = transform.Find("Month").GetComponent();
        Init();
    }
    void OnBtnNextClicked()
    {
        month++;
        if (month > 12) month = 1;
        
        ClearAllDay();
        Init(month);
    }
    void OnBtnLastClicked()
    {
        month--;
        if (month <= 0) month = 12;
        ClearAllDay();
        Init(month);
    }

}

Demo传送门:https://download.csdn.net/download/cjb_king/11050321

你可能感兴趣的:(Unity游戏开发)