先上代码:
public List<Button> btnList = new List<Button>();
public Text chooseDay;
public int year;
public int month;
private void Start()
{
InitilCalender();
}
private void InitilCalender()
{
for (int i = 0; i < btnList.Count; i++)
{
var tempBtn = btnList[i];
tempBtn.interactable = false;
tempBtn.transform.GetChild(0).GetComponent<Text>().text = string.Empty;
}
var days = DateTime.DaysInMonth(year, month);
List<int> intList = new List<int>();
int index = 0;
for (int i = 1; i < days + 1; i++)
{
var tempDate = year.ToString() + "-" + month.ToString() + "-" + i.ToString();
int num = Week2Num(DateTime.Parse(tempDate).DayOfWeek);
if (i == 1)
{
if(num == 7)
{
num -= 1;
index++;
}
else
{
num -= 1;
}
}
else
{
if (num == 7)
{
num = 7 * index + num - 1;
index++;
}
else
{
num = 7 * index + num - 1;
}
}
var tempBtn = btnList[num];
tempBtn.interactable = true;
tempBtn.transform.GetChild(0).GetComponent<Text>().text = i.ToString("00");
tempBtn.onClick.AddListener(BtnClick);
}
}
private void BtnClick()
{
var curBtn = EventSystem.current.currentSelectedGameObject;
var curDay = int.Parse(curBtn.transform.GetChild(0).GetComponent<Text>().text);
chooseDay.text = year.ToString() + "-" + month.ToString("00") + "-" + curDay.ToString("00");
}
private int Week2Num(DayOfWeek dayOfWeek)
{
int num = 0;
switch (dayOfWeek)
{
case DayOfWeek.Friday:
num = 5;
break;
case DayOfWeek.Monday:
num = 1;
break;
case DayOfWeek.Saturday:
num = 6;
break;
case DayOfWeek.Sunday:
num = 7;
break;
case DayOfWeek.Thursday:
num = 4;
break;
case DayOfWeek.Tuesday:
num = 2;
break;
case DayOfWeek.Wednesday:
num = 3;
break;
}
return num;
}
再上UI:
|