这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌
Unity中加载时显示进度条的脚本
我们可以看见很多游戏在加载关卡或者进入下个游戏场景的时候都有进度条,来表明游戏加载完成率,而不是卡在当前场景或者是直接黑屏。Unity3D提供了一些函数供我们使用。我们可以查找API来看看 Application.LoadLevelAsync 这个函数
AsyncOperation async = Application.LoadLevelAsync("MyBigLevel");
yield return async;
Debug.Log("Loading complete");
}
这个是异步加载的函数,AsyncOperation的progress返回了0-1之间的值,它是只读的。又来这个就好办了。我们就可以根据AsyncOperation.progress这个值来和进度条进行关联了。因为它就是0-1.
更详细的如下:
//先声明进度条
private AsyncOperation async;
//判断是否要触发进度条
void OnTriggerEnter(Collider other)
{
print("打开");
//判断触发的物体,是预设的物体就跳转进度条
if (other.tag == "Player")
{
print("打开副本");
StartCoroutine(GetProgress());
}
}
//通过接口来实现进度条的加载
IEnumerator GetProgress() //进度条
{
async = Application.LoadLevelAsync(0);
yield return async;
}
//通过Update来做到实时更新进度条
void Update()
{
if (async != null)
{
if (!async.isDone)
{
float progress = async.progress;
print("加载进度 " + progress);
}
}
}
void OnTriggerExit(Collider other)
{
if (other.collider.gameObject.tag == "Player") //判断出触发器的物体是不是标签为Player的物体
{
print("关闭副本");
}
}
}