您当前的位置: 首页 >  unity

鱼儿-1226

暂无认证

  • 0浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

unity5.x-2019导入老项目包含GUI对象的处理方式

鱼儿-1226 发布时间:2020-09-05 10:25:36 ,浏览量:0

新同学可能经常会遇到开打或者导入一些老项目,其中包含过去的GUI对象,出现报错的情况。例如:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

	// Use this for initialization
	public static GameManager _instance;

	public int score=0;

	private GUIText uiText;

	void Awake(){

		_instance = this;
		uiText = GameObject.FindGameObjectWithTag ("scoreGUI").guiText;
	}

	
	// Update is called once per frame
	void Update () {
		uiText.text = "Score:" + score;	
	}
}

2019的unity版本

这些地方要修改​

另外,Unity2019的 UI是通过 Package Manager 添加的,如果你的项目Package中没有 Unity UI,需要到  Package Manager添加

5.x-2018的unity版本

修改代码是一样的。如果你的项目属性中没有包含对UI的引用,可以自己修改一下你的项目属性文件,添加对UI的引用,是一个xxx.csproj的文件

你可以在vs 菜单 【项目】 里找到并编辑你的项目属性文件

最终代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{

	// Use this for initialization
	public static GameManager _instance;

	public int score = 0;

	private Text uiText;

	void Awake()
	{

		_instance = this;
		uiText = GameObject.Find("scoreGUI").GetComponent();
	}


	// Update is called once per frame
	void Update()
	{
		uiText.text = "Score:" + score;
	}
}
关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0480s