unity学习(40)——创建(create)角色脚本(panel)——UI

1.点击不同的头像按钮,分别选择职业1和职业2,create脚本中对应的函数。

2.调取inputfield中所输入的角色名(限制用户名长度为7字符),但愿逆向的服务器可以查重名:

3.点击头衔,显示选择的职业:(视频审核之后补上)

创建角色

函数代码如下(就是简单的设置static变量):

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class CreatePlayerPanel : MonoBehaviour
{
    //TMP_Text a = GameObject.FindWithTag("username").GetComponent<TMP_Text>();--不能写这里
    public static GameObject panel;//这是个全局变量--实例类
    private static int job=0;//当前所选角色
    //public static GameObject jobShow;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("CreatePlayerPanel获取object测试--这个在游戏加载的时候就执行了,最最最开始大的时候");
        panel = GameObject.FindWithTag("createPanel");//同一命名空间即可,canvas在最外边是有道理的
        panel.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void finish()
    {
        //panel.SetActive(false);//点击
        //收取角色名
        TMP_Text nicknameCreate = GameObject.FindWithTag("nicknameCreate").GetComponent<TMP_Text>();
        Debug.Log(nicknameCreate.text);
        Debug.Log(job);
        panel.SetActive(false);//都接收完再隐藏,要不找不到--panel是个实例类,不用急
        if (nicknameCreate.text.Length < 16 && job != 0)
        {
            //跳转到游戏场景
            Debug.Log("跳转到游戏场景");
        }
        else 
        {
            Debug.Log("请按规定输入");
        }
        //最后跳转到游戏场景中

    }
    public void selectJob(int job)//职业-我就纳闷昵称在哪里
    {
        //选择职业
    }
    //点击按钮选择不同的角色--
    public void j1()
    {
        job = 1;//static 1号职业
        TMP_Text c = GameObject.FindWithTag("jobShow").GetComponent<TMP_Text>(); ;//得到两个Text对象
        c.text = "纸片人";
    }
    public void j2()
    {
        job = 2;//static 1号职业
        TMP_Text c = GameObject.FindWithTag("jobShow").GetComponent<TMP_Text>(); ;//得到两个Text对象
        c.text = "熊猫头";
    }
}
相关推荐
我是小哪吒2.01 小时前
书籍推荐-《对抗机器学习:攻击面、防御机制与人工智能中的学习理论》
人工智能·深度学习·学习·机器学习·ai·语言模型·大模型
✎ ﹏梦醒͜ღ҉繁华落℘1 小时前
WPF学习(四)
学习·wpf
✎ ﹏梦醒͜ღ҉繁华落℘2 小时前
WPF学习(动画)
学习·wpf
循环过三天2 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
生如夏花℡3 小时前
HarmonyOS学习记录3
学习·ubuntu·harmonyos
之歆3 小时前
Python-封装和解构-set及操作-字典及操作-解析式生成器-内建函数迭代器-学习笔记
笔记·python·学习
幽络源小助理3 小时前
SpringBoot基于JavaWeb的城乡居民基本医疗信息管理系统
java·spring boot·学习
虾球xz4 小时前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习
Chef_Chen4 小时前
从0开始学习R语言--Day38--辛普森多样性指数
学习
Allen_LVyingbo4 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗