根据年龄、性别匹配用户化身

unity中用户登陆性别单选框设置https://mp.csdn.net/postedit/82495885

1.性别输入设置如上一篇所述,姓名输入设置:鼠标右击->UI->Input Field,将Inspector页面中的Input Field中Content Type改成Name类型,这样运行输入时只能输入文字或者字母,不能输入数字

同样的,创建年龄的Input Field,将Content Type改成Integer Number,这样运行输入时只能输入整型数字

注意,在创建多个UI对象时,排列整齐很重要,不需要叠加的时候尽量规划好大小,特别是Text框,容易被忽视掉未输入文字的地方,反而遮挡了别的对象,如图“性别”所处的Text遮住了选项“男”,运行时会造成选不中“男”的情况,也可以通过调整对象层次来避免这个问题。

2.创建选择用户化身代码

public class select : MonoBehaviour {

  
    public int nianling;
    public GameObject age;
    public GameObject girl;
    public GameObject boy;
    public GameObject woman;
    public GameObject man;
   
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
       
	}
    
    public void _select()
    {
        int.TryParse(age.GetComponent<Text>().text, out nianling);//将Text中的数字转化成整型
        if (nianling > 20 || nianling == 20)//判断年龄
        {
            Destroy(girl);
            Destroy(boy);
           
            if (SettingPopup.m == true)//判断性别
            {

                Destroy(woman);
            }
            else if (SettingPopup.w == true)
            {
               
                Destroy(man);
            }

        }
        else {
            if (SettingPopup.m == true)
            {

                Destroy(girl);
                
            }
            else if (SettingPopup.w == true)
            {

                Destroy(boy);
               
            }
        }
    }
}

然后将代码挂到一个空物体上,然后设置一个按钮触发获取信息事件,我的空物体名为“script”,将挂有代码的“script”拖到按钮的On Click的空白框中,在No Fuction中选择函数“_select”

猜你喜欢

转载自blog.csdn.net/xiaoyuchixiaomao/article/details/82497989