본문 바로가기
개발/코드

Unity) 달력 만들기 코드 (Make Calendar)

by 테샤르 2022. 2. 3.

달력 만들기 코드 (Make Calendar)

간단하게 달력을 만드는 코드를 정리 겸 포스팅하게 되었다.

달력을 만드는 코드는 간단하게 DateTime을 활용해서 구현했다.

이전 달과 현재 달을 선택할수 있고, 토요일과 일요일을 구분했다.

 

현재 날짜(22.02.03일)를 기준으로

선택을 못하게 버튼으로 처리를 했고 Text Color를 좀 더 연하게 구성했다.

 

반응형

< Prefab &  Inspector >

Prefab과 Inspector의 속성은 다음과 같다.

OrgNumber를 기준으로 dayNumber를 계산했다.

반응형

< 달력 코드 예제 >

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Events;
using System;

public class CalendarDayButton : MonoBehaviour
{
    // Start is called before the first frame update

    [SerializeField]
    private TextMeshProUGUI textValue = null;

    [SerializeField]
    private CalendarDayButtonInfo info = null;

    [SerializeField]
    private Image select = null;

    private Button button = null;

    private void Start()
    {
        InitCompoent();
    }

    private void InitCompoent()
    {
        if (null != button)
            return;
        button = GetComponent<Button>();
    }

    public void Initalize(int _index , UnityEvent<DateTime, string> _event)
    {
        info.orgNumber = _index;
        _event.AddListener(UpdateButtonInfo);
    }
    

    public void OnButtonSelect()
    {
        DefineManager.Instance.GetDefineObject<CalendarController>("CalendarController").SelectButton = this;
        Logger.Log($"Select :: {info.orgNumber}");
    }


    public int FirstOfMonthDay(DateTime _date)
    {
        DateTime firstOfMonth = new DateTime(_date.Year, _date.Month, 1);
        return (int)firstOfMonth.DayOfWeek + 1;
    }

    public int DaysInMonth(DateTime _date)
    {
        return DateTime.DaysInMonth(_date.Year, _date.Month);
    }

    public void UpdateButtonInfo(DateTime _dateTime, string clearData)
    {
        info.dayNumber = ((info.orgNumber - FirstOfMonthDay(_dateTime) + 1) + 1);

        bool enable = false;
        string currentText = $"{info.dayNumber}";
        Color textColor = Color.black;

        bool include = (((info.dayNumber > 0) && (info.dayNumber < DaysInMonth(_dateTime))));
        DateTime toDay = DateTime.Now;

        info.dateTime = null;

        if ( include )
        {
            
            info.dateTime = new DateTime(_dateTime.Year, _dateTime.Month, info.dayNumber);

            if (info.dateTime?.Year == toDay.Year &&
                info.dateTime?.Month == toDay.Month &&
                info.dateTime?.Day == toDay.Day)
            {
                info.buttonType = CalendarDayButtonInfo.CalendarButtoType.Current;
                enable = true;
            }
            else if ((DateTime.Compare((DateTime)info.dateTime, toDay) > 0))
            {
                info.buttonType = CalendarDayButtonInfo.CalendarButtoType.Disable;
            }
            else if (include)
            {
                if (clearData.Substring((info.dayNumber - 1), 1).Equals("1"))
                    info.buttonType = CalendarDayButtonInfo.CalendarButtoType.Clear;
                else
                    info.buttonType = CalendarDayButtonInfo.CalendarButtoType.Enable;
                enable = true;
            }
        }
        else
        {
            info.buttonType = CalendarDayButtonInfo.CalendarButtoType.NotExist;
            currentText = string.Empty;
        }

        switch (info.buttonType) 
        {
            case CalendarDayButtonInfo.CalendarButtoType.Current: textColor = Color.green; break;
            case CalendarDayButtonInfo.CalendarButtoType.Clear: textColor = Color.cyan; break;
            
            default:
                {
                    if (include)
                    {
                        switch (info.dateTime?.DayOfWeek)
                        {
                            case DayOfWeek.Sunday: textColor = Color.red; break;
                            case DayOfWeek.Saturday: textColor = Color.blue; break;
                            default:
                                {
                                    
                                }
                                break;
                        }

                        if (info.buttonType == CalendarDayButtonInfo.CalendarButtoType.Disable)
                        {
                            textColor = new Color(textColor.r, textColor.g, textColor.b, 0.4f);
                        }
                    }
                    else
                    {
                        textColor = new Color(0, 0, 0, 0);
                    }
                }
                break;
        }


        if (button == null)
        {
            InitCompoent();
            Debug.Log($"[CalendarDayButton ] {this.name} :: {info.orgNumber}");
        }
         
        button.enabled = enable;
        textValue.text = currentText;
        textValue.color = textColor;

        if (false == info.dateTime.HasValue)
            return;
        if(CheckInit(info.dateTime.Value))
            OnButtonSelect();

        //다른 달일경우 첫번째 선택
    }

    private bool CheckInit(DateTime _info)
    {
        DateTime toDay = DateTime.Now;

        if (toDay.Year == _info.Year && toDay.Month == _info.Month)
        {
            return (toDay.Day == _info.Day);
        }
        else
        {
            return (_info.Day == 1);
        }

    }

    public void SetSelect(bool _select)
    {
        select.gameObject.SetActive(_select);
    }
}

 

Microsoft DateTime 구조체 : [링크]

 

DateTime 구조체 (System)

일반적으로 날짜와 시간으로 표시된 시간을 나타냅니다.Represents an instant in time, typically expressed as a date and time of day.

docs.microsoft.com

 

★★★☆☆

 

반응형

댓글