国家|省份|城市和年|月|日 数据库查询 没有做级联菜单
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Text;
public class Profile : MonoBehaviour {
    private Texture Pic_WndBg;
    public GUISkin WndSkin;
    public Rect Rect_Wnd ;
    public GUIStyle Style_Bg;
    public Texture Pic_Avatar;
    public Vector2 InfoPos;
    public string Str_NickName;
    public string Str_RealName;
    public string Str_SelfIntro;
    public string Str_Location;
    public int Age;
    public string Str_BirthYear;
    public string Str_BirthMonth;
    public string Str_BirthDay;
    public string Str_Gender;
    public string Str_Country;
    public string Str_Province;
    public string Str_City;
    public string Str_Phone;
    public string Str_Mobile;
    public string Str_Email;
    public string Str_QQ;
    public string Str_MSN;
    //~ string Birthyear;
    //~ string BirthMonth;
    //~ string BirthDay;
    public static bool ShowGender;
    public static bool ShowBirthYear;
    public static bool ShowBirthMonth;
    public static bool ShowBirthDay;
    public static bool ShowCountry;
    public static bool ShowProvince;
    public static bool ShowCity;
    public int SelectedGenderItem = -1;
    public int SelectedBirthYearItem = -1;
    public int SelectedBirthMonthItem = -1;
    public int SelectedBirthDayItem = -1;
    public int SelectedCountryItem = -1;
    public int SelectedProvinceItem = -1;
    public int SelectedCityItem = -1;
    public Vector2 GenderPos;
    public Vector2 BirthYearPos;
    public Vector2 BirthMonthPos;
    public Vector2 BirthDayPos;
    public Vector2 CountryPos;
    public Vector2 ProvincePos;
    public Vector2 CityPos;
    //———test—————
    public int aa;
    public List<string> CountryID = new List<string>();
    public List<string> ProvinceID = new List<string>();
    public List<string> CountryID_Province= new List<string>();
    public List<string> CityID = new List<string>();
    public List<string> CityID_Province = new List<string>();
    public List<string> CityID_Province_Country = new List<string>();
    //—————————–
    public class ListItem
    {
        public bool Selected;
        public string Name;
        public ListItem(bool mSelected, string mName)
        {
            Selected = mSelected;
            Name = mName;
        }
        public ListItem(string mName)
        {
            Selected = false;
            Name = mName;
        }
        public void enable()
        {
            Selected = true;
        }
        public void disable()
        {
            Selected = false;
        }
    }
    private List<ListItem> GenderList = new List<ListItem>();
    private List<ListItem> BirthYearList = new List<ListItem>();
    private List<ListItem> BirthMonthList = new List<ListItem>();
    private List<ListItem> BirthDayList = new List<ListItem>();
    private List<ListItem> CountryList = new List<ListItem>();
    private List<ListItem> ProvinceList = new List<ListItem>();
    private List<ListItem> CityList = new List<ListItem>();
    public bool DoList;
    // Use this for initialization
    void Start ()
    {
        Rect_Wnd = new Rect((Screen.width – 546)/2,(Screen.height – 410)/2,546,410);
        Str_NickName = "昵称";
        //~ Str_SelfIntro = "速度快了赶进度过来的个端口劳动法孤苦伶仃没癫凤狂龙";
        Str_SelfIntro ="";
        GenderList.Add(new ListItem("男"));
        GenderList.Add(new ListItem("女"));
        int year=0,month=12,day=0;
        System.DateTime currentTime=new System.DateTime();
        currentTime=System.DateTime.Now;
        year=currentTime.Year;
        for(int i = year;1980<i;i–)
        {
            BirthYearList.Add(new ListItem((i)+"年"));
        }
        for(int i = 0; i <month; i++)
        {
            BirthMonthList.Add(new ListItem((i+1)+"月"));
        }
        if(year%4==0&&month==2)
        {
            day=29;
        }
        else
        {
            day=28;
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        {
            day=31;
        }
        if(month==4||month==6||month==9||month==11)
        {
            day=30;
        }
        for(int i = 0 ; i<day;i++)
        {
            BirthDayList.Add(new ListItem((i+1)+"日"));
        }
        //~ CountryList.Add(new ListItem("中国"));
        //~ CountryList.Add(new ListItem("美国"));
        //~ ProvinceList.Add(new ListItem("上海"));
        //~ ProvinceList.Add(new ListItem("北京"));
        //~ ProvinceList.Add(new ListItem("广东"));
        //~ CityList.Add(new ListItem("上海"));
        //~ CityList.Add(new ListItem("北京"));
        //~ CityList.Add(new ListItem("深圳"));
        StartCoroutine(DoGetMultitermData());
        //~ SelectedBirthMonthItem = int.Parse(Str_BirthMonth)-1;
        //~ print("%%%SelectedBirthMonthItem"+int.Parse(Str_BirthMonth));
    }
    // Update is called once per frame
    void Update ()
    {
    }
    void OnGUI()
    {
        GUI.skin = WndSkin;
        Rect_Wnd = GUI.Window(0,Rect_Wnd,DoProfileWnd,"个人资料");
    }
    void DoProfileWnd(int WndID)
    {
        if(GUI.Button(new Rect(Rect_Wnd.width – 35,18,20,19),"X"))
        {
            this.enabled = false;
        }
        GUILayout.BeginArea(new Rect(18,70,Rect_Wnd.width – 30,400));
        GUILayout.BeginArea(new Rect(19,10,50,50));
        GUILayout.Button(Pic_Avatar,GUILayout.Width(48),GUILayout.Height(48));
        GUILayout.EndArea();
        GUILayout.BeginArea(new Rect(85,10,Rect_Wnd.width – 116,290)); //滚动区域,所以高度大些,但不能太大
        InfoPos = GUILayout.BeginScrollView(InfoPos);
        GUILayout.BeginHorizontal();
        GUILayout.BeginHorizontal(GUILayout.Width(170));
        GUILayout.Label("昵称:",GUILayout.Width(36));
        Str_NickName = GUILayout.TextField(Str_NickName,10,GUILayout.Width(163));
        GUILayout.EndHorizontal();
        GUILayout.EndHorizontal();
        GUILayout.BeginVertical();
        GUILayout.Label("我的简介:");
        Str_SelfIntro = GUILayout.TextArea(Str_SelfIntro,GUILayout.Height(60));
        GUILayout.EndVertical();
        GUILayout.Label("",GUILayout.Height(106)); //占位,一以便滚动条可以控制
        GUILayout.BeginHorizontal();
        GUILayout.BeginVertical();
        GUILayout.Label("电话:");
        Str_Phone = GUILayout.TextField(Str_Phone,12);
        GUILayout.EndVertical();
        GUILayout.BeginVertical();
        GUILayout.Label("手机:");
        Str_Mobile = GUILayout.TextField(Str_Mobile,11);
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.BeginVertical();
        GUILayout.Label("QQ:");
        Str_QQ = GUILayout.TextField(Str_QQ,11);
        GUILayout.EndVertical();
        GUILayout.BeginVertical();
        GUILayout.Label("MSN:");
        Str_MSN = GUILayout.TextField(Str_MSN);
        GUILayout.EndVertical();
        GUILayout.BeginVertical();
        GUILayout.Label("eMail:");
        Str_Email = GUILayout.TextField(Str_Email);
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUILayout.Label("",GUILayout.Height(80));
        //放在此处原因是由于GUI绘制有先后顺序
        GUILayout.BeginHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.BeginArea(new Rect(6,170,Rect_Wnd.width – 105,200));
        GUILayout.BeginHorizontal();
        GUILayout.Label("国家/地区:",GUILayout.Width(200));
        GUILayout.Label("省份:",GUILayout.Width(98));
        GUILayout.Label("城市:");
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        PutDownMenu(ref Str_Country,ref ShowCountry,new Rect(0,50,200,200),CountryList,ref SelectedCountryItem,ref CountryPos);
        //~ CountryList[SelectedCountryItem]
        PutDownMenu(ref Str_Province,ref ShowProvince,new Rect(204,50,98,200),ProvinceList,ref SelectedProvinceItem,ref ProvincePos);
        PutDownMenu(ref Str_City,ref ShowCity,new Rect(306,50,98,200),CityList,ref SelectedCityItem,ref CityPos);
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
        GUILayout.EndHorizontal();
        GUILayout.BeginArea(new Rect(6,120,98,250));
        GUILayout.BeginVertical();
        GUILayout.Label("性别:");
        Rect Rect_GenderList = new Rect(0,50,98,25);
        PutDownMenu(ref Str_Gender,ref ShowGender,Rect_GenderList,GenderList,ref SelectedGenderItem,ref GenderPos);
        GUILayout.EndVertical();
        GUILayout.EndArea();
        GUILayout.BeginArea(new Rect(108,120,Rect_Wnd.width – 180,200));
        GUILayout.Label("生日:"+Str_Location);
        GUILayout.BeginHorizontal();
        PutDownMenu(ref Str_BirthYear,ref ShowBirthYear,new Rect(0,50,98,25),BirthYearList,ref SelectedBirthYearItem,ref BirthYearPos);
        PutDownMenu(ref Str_BirthMonth,ref ShowBirthMonth,new Rect(102,50,98,25),BirthMonthList,ref SelectedBirthMonthItem,ref BirthMonthPos);
        PutDownMenu(ref Str_BirthDay,ref ShowBirthDay,new Rect(204,50,98,25),BirthDayList,ref SelectedBirthDayItem,ref BirthDayPos);
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
        GUILayout.EndHorizontal();
        //下拉部分
        GUILayout.EndScrollView();
        GUILayout.EndArea();
        GUILayout.EndArea();
        GUILayout.BeginArea(new Rect(Rect_Wnd.width/2-110,Rect_Wnd.height – 35,210,30));
        GUILayout.BeginHorizontal();
        if(GUILayout.Button("确定",GUILayout.Width(70),GUILayout.Height(20)))
        {
        }
        GUILayout.Button("取消",GUILayout.Width(70),GUILayout.Height(20));
        if(GUILayout.Button("应用",GUILayout.Width(70),GUILayout.Height(20)))
        {
            StartCoroutine(UP_Data());
        }
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
        GUI.DragWindow();
    }
    void PutDownMenu(ref string Str_SelectedList,ref bool DropdownVisible,Rect Rect_PutDownMenu,List<ListItem> SelectedList,ref int SelectedListItem,ref Vector2 DropDownPos)
    {
        if(DropdownVisible)
        {
            GUILayout.BeginArea(new Rect(Rect_PutDownMenu.x,45,Rect_PutDownMenu.width,70),"",Style_Bg);
            DropDownPos = GUILayout.BeginScrollView(DropDownPos);
            GUILayout.BeginVertical();
            for(int i = 0;i<SelectedList.Count; i++)
            {
                if(GUILayout.Button(SelectedList[i].Name))//设置背景图
                {
                    SelectedListItem = i;
                    SelectedList[SelectedListItem].enable();
                    DropdownVisible = false; //隐藏列表
                }
            }
            GUILayout.EndVertical();
            GUILayout.EndScrollView();
            GUILayout.EndArea();
        }
        GUILayout.BeginHorizontal("box",GUILayout.Width(Rect_PutDownMenu.width));
        Str_SelectedList = (SelectedListItem == -1) ? SelectedList[0].Name : SelectedList[SelectedListItem].Name;
        GUILayout.Label(Str_SelectedList);
        string Str_Arrow = (DropdownVisible)? "▲" : "▼"; //暂用字符代替
        DropdownVisible = GUILayout.Toggle(DropdownVisible, Str_Arrow, "button",GUILayout.Width(15),GUILayout.Height(15));
        GUILayout.EndHorizontal();
    }
    private string jsonURL1="http://localhost/ekarma/user_select.php";
    private string jsonURL2="http://localhost/ekarma/user_basic_info_select.php"; //查询多条记录
    private string jsonURL3="http://localhost/ekarma/user_update.php";
    private string jsonURL4="http://localhost/ekarma/user_basic_info_update.php";
    private string jsonURL5="http://localhost/ekarma/country_select.php";
    private string jsonURL6="http://localhost/ekarma/province_select.php";
    private string jsonURL7="http://localhost/ekarma/city_select.php";
    public IEnumerator Test(){
        WWW getwww = new WWW (jsonURL2);
        yield return getwww;
        print("dasdasdasd~~~");
    }
    //取数据
    public IEnumerator DoGetMultitermData() {
        WWW getwww = new WWW (jsonURL2);
        yield return getwww;
        string myHash = getwww.data;   //eval转换成一个object对象
        JObject o = JObject.Parse(myHash);
        print(o);
        WWW getwww1 = new WWW (jsonURL1);
        yield return getwww1;
        string myHash1 = getwww1.data;   //eval转换成一个object对象
        JObject o1 = JObject.Parse(myHash1);
        print("~~~~~~~~~~~!!!!!!!!!~~~~~~~~~~"+o1);
        Str_NickName=(string)o["real_name"] ;
        Str_SelfIntro=(string)o["self_introduction"];
        SelectedGenderItem=int.Parse((string)o["sex"]);
        SelectedCountryItem=int.Parse((string)o["country"]);
        SelectedProvinceItem=int.Parse((string)o["province"]);
        SelectedCityItem=int.Parse((string)o["city"]);
        string birthday=(string)o["birthday"];
        string[] date = birthday.Split('-');
        //~ Str_BirthYear=date[0];
        //~ Str_BirthMonth=date[1];
        //~ Str_BirthDay=date[2];
        System.DateTime currentTime=new System.DateTime();
        currentTime=System.DateTime.Now;
        int nian=currentTime.Year;
        SelectedBirthYearItem=nian-int.Parse(date[0]);
        SelectedBirthMonthItem=int.Parse(date[1])-1;
        SelectedBirthDayItem=int.Parse(date[2])-1;
        print("1:"+date[0]+"2:"+date[1]+"3:"+date[2]);
        //~ Age=nian-int.Parse(date[0]);
        Str_Phone=(string)o["telphone"];
        Str_Mobile=(string)o["cellphone"];
        Str_Email=(string)o1["user_name"];
        Str_QQ=(string)o["qq"];
        Str_MSN=(string)o["msn"];
        //取国家
        WWW getwww2 = new WWW (jsonURL5);
        yield return getwww2;
        string myHash2 = getwww2.data;   //eval转换成一个object对象
        JObject o2 = JObject.Parse(myHash2);
        JArray sizes2 = (JArray)o2["user"];
        print(sizes2);
        for(int i=0;i<sizes2.Count;i++)
        {
            string Country_id = (string)sizes2[i]["id"];
            string Country_country_name = (string)sizes2[i]["country_name"];
            CountryID.Add((string)Country_id);
            CountryList.Add(new ListItem(Country_country_name));
        }
        //取省
        WWW getwww3 = new WWW (jsonURL6);
        yield return getwww3;
        string myHash3 = getwww3.data;   //eval转换成一个object对象
        JObject o3 = JObject.Parse(myHash3);
        JArray sizes3 = (JArray)o3["user"];
        print(sizes3);
        for(int i=0;i<sizes3.Count;i++)
        {
            string Province_id = (string)sizes3[i]["id"];
            string JB_Province_country_id = (string)sizes3[i]["country_id"];
            string JB_Province_province_name = (string)sizes3[i]["province_name"];
            ProvinceID.Add((string)Province_id);
            CountryID_Province.Add((string)JB_Province_country_id);
            ProvinceList.Add(new ListItem(JB_Province_province_name));
        }
        //取城市
        WWW getwww4 = new WWW (jsonURL7);
        yield return getwww4;
        string myHash4 = getwww4.data;   //eval转换成一个object对象
        JObject o4 = JObject.Parse(myHash4);
        JArray sizes4 = (JArray)o4["user"];
        print(sizes4);
        for(int i=0;i<sizes4.Count;i++)
        {
            string City_id = (string)sizes4[i]["id"];
            string JB_City_country_id = (string)sizes4[i]["country_id"];
            string JB_City_province_id = (string)sizes4[i]["province_id"];
            string JB_City_city_name= (string)sizes4[i]["city_name"];
            CityID.Add((string)City_id);
            CityID_Province.Add((string)JB_City_province_id);
            CityID_Province_Country.Add((string)JB_City_country_id);
            CityList.Add(new ListItem(JB_City_city_name));
        }
    }
    //更新数据
    public IEnumerator UP_Data(){
        //提交JSON数据的方法
        Hashtable mydata=new Hashtable();
        Hashtable mydata1=new Hashtable();
        mydata.Add("real_name",Str_NickName);
        mydata.Add("self_introduction",Str_SelfIntro);
        mydata.Add("sex",SelectedGenderItem);
        System.DateTime currentTime=new System.DateTime();
        currentTime=System.DateTime.Now;
        int nian=currentTime.Year;
        //~ mydata.Add("birthday",(nian-SelectedBirthYearItem)+"-"+Str_BirthMonth+"-"+Str_BirthDay);
        mydata.Add("birthday",Str_BirthYear+"-"+Str_BirthMonth+"-"+Str_BirthDay);
        mydata.Add("country",CountryID[SelectedCountryItem]);
        mydata.Add("province",ProvinceID[SelectedProvinceItem]);
        mydata.Add("city",CityID[SelectedCityItem]);
        mydata.Add("telphone",Str_Phone);
        mydata.Add("cellphone",Str_Mobile);
        mydata.Add("qq",Str_QQ);
        mydata.Add("msn",Str_MSN);
        mydata1.Add("user_name",Str_Email);
        //~ print("selected"+SelectedCountryItem);
        //将数据转换为json字符串
        string jsonstring=HashTable2Json(mydata);
        print("!!!!!123"+jsonstring);
        WWWForm form = new WWWForm();
        form.AddField("jsonstring", jsonstring);
        WWW postwww = new WWW(jsonURL4, form);
        yield return postwww;
        print(":::"+postwww.data);
        string jsonstring1=HashTable2Json(mydata1);
        print("!!!!!"+jsonstring1);
        WWWForm form1 = new WWWForm();
        form1.AddField("jsonstring", jsonstring1);
        WWW postwww1 = new WWW(jsonURL3, form1);
        yield return postwww1;
        print(":::"+postwww1.data);
    }
    public static string HashTable2Json(Hashtable hashtable){
        if (hashtable.Count < 1 ){
            return "{}";
        }
        //初始化
        StringBuilder sb = new StringBuilder();
        sb.Append('{');
            foreach (System.Collections.DictionaryEntry objDE in hashtable)
            {
                if (sb.Length > 1 )
                sb.Append(",");
                sb.Append("\"" + objDE.Key.ToString() + "\":" + "\""+objDE.Value.ToString()+ "\"");
            }
            sb.Append('}');
        //~ print("!!!!!!!!!!!!!!!!!"+ sb.ToString());
        return sb.ToString();
    }
}
本条目发布于2010年5月10日。属于Unity脚本分类。