unity学习笔记08

一、Application类常用属性和方法

游戏数据文件夹路径 
Debug.Log(Application.dataPath); 

如何读取Assets文件夹下面的文件

Debug.Log(Application.dataPath +"/新建文本文档.txt");

这个是自读的文件路径不能写入。

拿到不同平台的可储存路径

Debug.Log(Application.persistentDataPath)

StreamingAssets文件夹路径(只读,配置文件),这个文件是不会被打包加密的。

Debug.Log(Application.streamingAssetsPath);


临时文件夹
Debug.Log(Application.temporaryCachePath);

控制是否在后台运行。
Debug.Log(Application.runInBackground);
打开url
Application.OpenURL(" ");

退出游戏
Application.Quit();

二、游戏场景

1.场景的跳转

现在有两个场景如何通过代码去切换场景。

首先点击文件→生成设置可以看到 Build中的场景

如果场景要在程序中使用,那么就要拖拽过来。右边的数值是索引可以用于加载场景的名称

通过索引的方式加载:SceneManager.LoadScene(1);

通过名称的方式:SceneManager.LoadScene("MyScene");

获取当前场景
Scene scene = SceneManager.GetActiveScene();

2.场景的常见属性

获取当前场景
Scene scene = SceneManager.GetActiveScene0;

获取当前场景名称
Debug.Log(scene.name);

场景是否已经加载
Debug.Log(scene.isLoaded);

获取场景路径
Debug.Log(scene.path);

获取场景索引
Debug.Log(scene.buildIndex);

获取场景中所有的游戏物体
GameObject[] gos = scene.GetRootGameObjects();

3.场景管理类常用属性和方法
创建新的场景
Scene newScene = SceneManager.CreateScene("newScene");

已加载的场景个数
Debug.Log(SceneManager.sceneCount);

卸载指定场景
SceneManager.UnloadSceneAsync(newScene);

一般加载场景
SceneManager.LoadScene("MyScene" ,LoadSceneMode.Additive);

4.异步加载场景

异步加载场景是一种优化手段,可以避免在切换场景时造成的界面卡顿。

声明返回值 operation

AsyncOperation operation;

void Start(){
StartCoroutine(loadScene0);

}

lEnumerator loadScene() {
operation = SceneManager.LoadSceneAsync(1);

yield return operation;
}

获取加载进度(0-0.9)
Debug.Log(operation.progress);

加载完场景是不要自动跳转
operation.allowSceneActivation = false;


 

猜你喜欢

转载自blog.csdn.net/2301_79022588/article/details/134618553
今日推荐