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 = "熊猫头";
    }
}
相关推荐
西岸行者11 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意11 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码11 天前
嵌入式学习路线
学习
毛小茛11 天前
计算机系统概论——校验码
学习
babe小鑫11 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms11 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下11 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。11 天前
2026.2.25监控学习
学习
im_AMBER11 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J11 天前
从“Hello World“ 开始 C++
c语言·c++·学习