UGUI 制作日历

给予UGUI的简单日历制作,目前只有数2017的,暂不支持跨年,

上代码:

//上个月显示在本月的天数, 本月首天
    private int _lastDaysCount, _firstDay;
    //当前月份
    private int _curMonth;
    //加载标志
    private bool _isLoad = false;
    //每月的天数,0只是占位置,不用
    int[] _monthDaysCount = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 30, 30, 31 };
    //天数父节点
    public Transform _daysRoot;
    //年月
    public Text _txtYearMon;

    /// <summary>
    /// 设置上个月剩余天数
    /// </summary>
    /// <param name="curMon"></param>
    void SetSurplusDay(int curMon)
    {
        _curMonth = curMon;
        _txtYearMon.text = "2017年 " + "<color=#117708>" + curMon + "</color>" + " 月";

        switch (curMon)
        {
            case 1:
                _lastDaysCount = 6;
                _firstDay = 26;
                break;
            case 2:
                _lastDaysCount = 2;
                _firstDay = 30;
                break;
            case 3:
                _lastDaysCount = 2;
                _firstDay = 27;
                break;
            case 4:
                _lastDaysCount = 5;
                _firstDay = 27;
                break;
            case 5:
                _lastDaysCount = 0;
                _firstDay = 0;
                break;
            case 6:
                _lastDaysCount = 3;
                _firstDay = 29;
                break;
            case 7:
                _lastDaysCount = 5;
                _firstDay = 26;
                break;
            case 8:
                _lastDaysCount = 1;
                _firstDay = 31;
                break;
            case 9:
                _lastDaysCount = 4;
                _firstDay = 28;
                break;
            case 10:
                _lastDaysCount = 6;
                _firstDay = 25;
                break;
            case 11:
                _lastDaysCount = 2;
                _firstDay = 30;
                break;
            case 12:
                _lastDaysCount = 4;
                _firstDay = 27;
                break;
        }
    }

    void CreateDays(int month)
    {
        if (month < 1 || month > 12)
            return;
        SetSurplusDay(month);

        //所有天数
        int allDays = _monthDaysCount[month] + _lastDaysCount;
        int lastCount = _lastDaysCount, firstDay = _firstDay;

        for (int i = 0; i < _daysRoot.childCount; ++i)
        {
            Text txt = _daysRoot.GetChild(i).GetComponent<Text>();
            txt.fontSize = 40;
            if (i < allDays)
            {
                if (lastCount > 0)
                {
                    lastCount--;
                    txt.text = firstDay.ToString();
                    txt.color = Color.gray;
                    firstDay++;
                }
                else
                {
                    txt.text = (i - _lastDaysCount + 1).ToString();
                    txt.color = Color.white;
                }
            }
            else
            {
                txt.text = "";
            }
        }
    }

    //下个月
    public void BtnEvent_NextMon()
    {
        if (_curMonth < 12)
        {
            _curMonth++;

            CreateDays(_curMonth);
        }
   
    }

    //上个月
    public void BtnEvent_PreMon()
    {
        if (_curMonth > 1)
        {
            _curMonth--;
            CreateDays(_curMonth);
        }
        
    }
    

实例:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值