参考: http://www.xuanyusong.com/archives/1427
异步加载 有进度
private float fps = 10.0f;
private float time;
//一组动画的贴图,在编辑器中赋值。
public Texture2D[] animations;
private int nowFram;
//异步对象
AsyncOperation async;
//读取场景的进度,它的取值范围在0 - 1 之间。
int progress = 0;
void Start()
{
//在这里开启协程
StartCoroutine(loadScene());
}
IEnumerator loadScene()
{
//异步读取场景。
//Globe.loadName 就是A场景中需要读取的C场景名称。
async = Application.LoadLevelAsync(Globe.loadName);
//读取完毕后返回, 系统会自动进入C场景
yield return async;
}
void OnGUI()
{
//因为在异步读取场景,
//所以这里我们可以刷新UI
DrawAnimation(animations);
}
void Update()
{
//在这里计算读取的进度,
//progress 的取值范围在0.1 - 1之间, 但是它不会等于1
//也就是说progress可能是0.9的时候就直接进入新场景了
//所以在写进度条的时候需要注意一下。
//为了计算百分比 所以直接乘以100即可
progress = (int)(async.progress *100);
//有了读取进度的数值
Debug.Log("xuanyusong" +progress);
}
//这是一个简单绘制2D动画的方法
void DrawAnimation(Texture2D[] tex)
{
time += Time.deltaTime;
if(time >= 1.0 / fps){
nowFram++;
time = 0;
if(nowFram >= tex.Length)
{
nowFram = 0;
}
}
GUI.DrawTexture(new Rect( 100,100,40,60) ,tex[nowFram] );
//在这里显示读取的进度。
GUI.Label(new Rect( 100,180,300,60), "lOADING!!!!!" + progress);
}
2.在加载大型场景的时候,如果将其中的一部分 Gameobject 不显示 即将其之前的√ 去掉 场景加载时就不会进行渲染
之后在脚本中的Awake中进行加载 找到对应的GameObject 的异步显示 可使用协程来实现 这样就可以每帧进行加载
不会在一瞬间占用大量的内存
3。也可以使用Instantiate 进行动态创建添加
异步加载 有进度
private float fps = 10.0f;
private float time;
//一组动画的贴图,在编辑器中赋值。
public Texture2D[] animations;
private int nowFram;
//异步对象
AsyncOperation async;
//读取场景的进度,它的取值范围在0 - 1 之间。
int progress = 0;
void Start()
{
//在这里开启协程
StartCoroutine(loadScene());
}
IEnumerator loadScene()
{
//异步读取场景。
//Globe.loadName 就是A场景中需要读取的C场景名称。
async = Application.LoadLevelAsync(Globe.loadName);
//读取完毕后返回, 系统会自动进入C场景
yield return async;
}
void OnGUI()
{
//因为在异步读取场景,
//所以这里我们可以刷新UI
DrawAnimation(animations);
}
void Update()
{
//在这里计算读取的进度,
//progress 的取值范围在0.1 - 1之间, 但是它不会等于1
//也就是说progress可能是0.9的时候就直接进入新场景了
//所以在写进度条的时候需要注意一下。
//为了计算百分比 所以直接乘以100即可
progress = (int)(async.progress *100);
//有了读取进度的数值
Debug.Log("xuanyusong" +progress);
}
//这是一个简单绘制2D动画的方法
void DrawAnimation(Texture2D[] tex)
{
time += Time.deltaTime;
if(time >= 1.0 / fps){
nowFram++;
time = 0;
if(nowFram >= tex.Length)
{
nowFram = 0;
}
}
GUI.DrawTexture(new Rect( 100,100,40,60) ,tex[nowFram] );
//在这里显示读取的进度。
GUI.Label(new Rect( 100,180,300,60), "lOADING!!!!!" + progress);
}
2.在加载大型场景的时候,如果将其中的一部分 Gameobject 不显示 即将其之前的√ 去掉 场景加载时就不会进行渲染
之后在脚本中的Awake中进行加载 找到对应的GameObject 的异步显示 可使用协程来实现 这样就可以每帧进行加载
不会在一瞬间占用大量的内存
3。也可以使用Instantiate 进行动态创建添加