2021-04-15 15:58:37 +08:00

88 lines
3.3 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Scripts.UI.Prefab.Login
{
public static class DayOptions
{
private static List<Dropdown.OptionData> SetOptions(int day)
{
var op = new Dropdown.OptionDataList();
for (int m = 1; m <= day; m++)
{
op.options.Add(new Dropdown.OptionData(m.ToString()));
}
return op.options;
}
public static Dictionary<int, List<Dropdown.OptionData>> dayOption = new Dictionary<int, List<Dropdown.OptionData>>()
{
{28,SetOptions(28) },{29,SetOptions(29) },{30,SetOptions(30) },{31,SetOptions(31) }
};
}
public class CountryModel
{
public string country { get;set; }
public string abbreviation { get; set; }
public string source { get; set; }
}
public class LoginRegOptions
{
public List<Dropdown.OptionData> years;
public List<Dropdown.OptionData> months;
public List<Dropdown.OptionData> days;
public List<Dropdown.OptionData> genders;
public List<Dropdown.OptionData> countrys;
public List<Dropdown.OptionData> units;
public int countryDefaultValue;
private List<CountryModel> countryList;
public LoginRegOptions()
{
years = new List<Dropdown.OptionData>();
for (int y = 1970; y <= 2021; y++)
{
years.Add(new Dropdown.OptionData(y.ToString()));
}
months = new List<Dropdown.OptionData>();
for (int m = 1; m <= 12; m++)
{
months.Add(new Dropdown.OptionData(m.ToString()));
}
days = new List<Dropdown.OptionData>();
genders = new List<Dropdown.OptionData>() {new Dropdown.OptionData("Male"), new Dropdown.OptionData("FeMale") };
var countryJson = Resources.Load<TextAsset>("UI/flags-mini").text;
countryList = JsonConvert.DeserializeObject<List<CountryModel>>(countryJson);
countrys = countryList.Select(x => new Dropdown.OptionData(x.country)).ToList();
countryDefaultValue = countryList.FindIndex(x => x.country == "中国");
units = new List<Dropdown.OptionData>() { new Dropdown.OptionData("Metric"), new Dropdown.OptionData("Imperial") };
}
public Texture GetCountryImage(int index)
{
var c = countryList[index];
if (c == null) return null;
return Resources.Load<Texture>(c.source);
}
public Texture GetCountryImage(string code)
{
return GetCountryImage(GetCountryIndexByCode(code));
}
public int GetCountryIndexByCode(string code)
{
var c = countryList.FindIndex(x => x.abbreviation.ToLower() == code.ToLower());
if (c == -1) return countryDefaultValue;
return c;
}
public List<Dropdown.OptionData> GetDayOptions(int year,int month)
{
int day = DateTime.DaysInMonth(year, month);
return DayOptions.dayOption[day];
}
}
}