前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用NGUI做游戏中的签到系统

用NGUI做游戏中的签到系统

作者头像
bering
发布2019-12-03 15:53:24
1K0
发布2019-12-03 15:53:24
举报
文章被收录于专栏:游戏开发之旅

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/88786825

用NGUI做游戏中的签到系统

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

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

代码语言:javascript
复制
public class ClendarMgr : MonoBehaviour
{

    public GameObject DayPrefab;
    public Transform[] WeekTransform;
    public float OffstY = 0;
    private int totalDaysInMonth = 0; //记录当月的总天数
    private List<GameObject> DayList = new List<GameObject>();
    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<UILabel>().text = DateTime.Now.Year.ToString();
        transform.Find("Month").GetComponent<UILabel>().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<UILabel>().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<UIButton>();
        btnNextMonth = transform.Find("BtnNext").GetComponent<UIButton>();
        btnLastMonth.onClick.Add(new EventDelegate(OnBtnLastClicked));
        btnNextMonth.onClick.Add(new EventDelegate(OnBtnNextClicked));
        lblMonth = transform.Find("Month").GetComponent<UILabel>();
        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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/03/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档