行业知识
unity学习(55)——选择角色界面--解析赋值服务器返回的信息2
2026-12-02 20:49:43
来源:互联网转载
���备工作已经完成,下面开始对message的内容进行操作:
1.在SelectMenu.cs中添加一个全局静态变量allPlayers:
2.然后在MessageManager.cs的case UserProtocol.LIST_SRES:部分进行赋值
执行结果符合预期:
3.在selectMenu中增加一个选择角色按钮,这个是最简单的做法:
4.在SelectMenu中增加选择功能的脚本内容:
using System;using System.Collections;using System.Collections.Generic;using UnityEngine;using minna;//protocol的命名空间都来自这里using TMPro;public class SelectMenu : MonoBehaviour{ // Start is called before the first frame update //public GameObject c = GameObject.FindWithTag("selectMenuCanvas"); public GameObject quan;//start中就保存好了 public int flag = 0; public static PlayerModel[] allPlayers; public static PlayerModel nowPlayer; public static int posPlayer=0;//现在已经随心所欲了 void Start() { //gameObject.SetActive(false); //GameObject m = GameObject.FindWithTag("selectMenuCanvas");//现在我挂在camera上就可以这么用了 this.quan = GameObject.FindWithTag("selectMenuCanvas");//同一命名空间即可,canvas在最外边是有道理的 this.quan.SetActive(false); //gameObject.GetComponent.enabled = false; //我估计这个放不到这里,应该放到如果登录成功的那里 //Debug.Log("我估计这个放不到这里,应该放到如果登录成功的那里");//不出所料 //NetWorkScript.getInstance().sendMessage(Protocol.USER,0,UserProtocol.LIST_CREQ,null); } // Update is called once per frame void Update()//角色的选择和创建我做在一起就可以了 { //Debug.Log(GameInfo.GAME_STATE);//初始状态开始为0 //Debug.Log(GameState.PLAYER_CREATE); //这个是4 if (GameInfo.GAME_STATE == GameState.PLAYER_CREATE && this.flag == 0) { //Debug.Log(GameInfo.GAME_STATE);//初始状态开始为0 //Debug.Log(GameState.PLAYER_CREATE); this.flag = 1; Debug.Log("这里计划是只执行一次"); this.quan.SetActive(true); } } public void GoToCreate()//unity那边想要加载必须public---其实就是createClick { Debug.Log("我确实进入onclick函数了"); //这种藏起来的找不着!第二次犯这个错误了 //下面这两句再次说明了之前false的找不到 //GameObject panel = GameObject.FindWithTag("createPanel");//同一命名空间即可,canvas在最外边是有道理的 //panel.SetActive(true); Debug.Log(CreatePlayerPanel.panel); CreatePlayerPanel.panel.SetActive(true);//用的static实例类--这个卡住了 Debug.Log("给我弹出创造页面"); } public void changePlayer() { // if (allPlayers != null)//新账号第一次没角色 { TMP_Text a = GameObject.FindWithTag("playerName").GetComponent();//得到两个Text对象 int yushu = allPlayers.Length; Debug.Log(yushu); if (yushu > 0) { nowPlayer = allPlayers[posPlayer % yushu];//z这样就实现循环了 Debug.Log(nowPlayer.name); //text显示当前角色的姓名和等级 a.text = nowPlayer.name +" "+ nowPlayer.level+"级"; posPlayer = (posPlayer + 1);//静态static直接拿来用就行,不用this } } else { //text显示没有角色 TMP_Text a = GameObject.FindWithTag("playerName").GetComponent(); a.text = "当前账号中没有角色"; } }} 5.执行结果也满足要求,做一个求余操作就可以快速解决问题,下图切换了三遍。
6,最后在text中显示角色名称和等级,所有的细节都才4的代码中,经过测试确实没有问题。
text挂上一个tag,名字是playerName: