1.发现一个非常实用的功能,点击unity中console的输出项,可以直接跳转到vs的代码页!
2.static类(变量)有三个特点:
(1)独一份(2)无法实例化。(3)全局。类似于c语言中的全局变量。
3.应用:因为create的panel在初始化时是false的,无法通过select脚本来find,这个问题之前出过。这次在create初始化时,直接用static变量来记录panel的指针。再在create点击“创建角色”按钮时将其弹出。
create部分的代码如下:
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 int job;//当前所选角色// Start is called before the first frame updatevoid Start(){Debug.Log("CreatePlayerPanel获取object测试--这个在游戏加载的时候就执行了,最最最开始大的时候");panel = GameObject.FindWithTag("createPanel");//同一命名空间即可,canvas在最外边是有道理的panel.SetActive(false);}// Update is called once per framevoid Update(){}public void finish(){panel.SetActive(false);//点击}public void selectJob(int job)//职业-我就纳闷昵称在哪里{//选择职业}
}
select部分的代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;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;void Start(){//gameObject.SetActive(false);//GameObject m = GameObject.FindWithTag("selectMenuCanvas");//现在我挂在camera上就可以这么用了this.quan = GameObject.FindWithTag("selectMenuCanvas");//同一命名空间即可,canvas在最外边是有道理的this.quan.SetActive(false);//gameObject.GetComponent<Renderer>.enabled = false;}// Update is called once per framevoid Update()//角色的选择和创建我做在一起就可以了{//Debug.Log(GameInfo.GAME_STATE);//初始状态开始为0//Debug.Log(GameState.PLAYER_CREATE); //这个是4if (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{Debug.Log("我确实进入onclick函数了");//这种藏起来的找不着!第二次犯这个错误了//下面这两句再次说明了之前false的找不到//GameObject panel = GameObject.FindWithTag("createPanel");//同一命名空间即可,canvas在最外边是有道理的//panel.SetActive(true);Debug.Log(CreatePlayerPanel.panel);CreatePlayerPanel.panel.SetActive(true);//用的static实例类--这个卡住了Debug.Log("给我弹出创造页面");}
}
这东西难在对static的理解和应用。