Unity 加载资源与存储数据
DontDestroyOnLoad
参考资料 Unity中DontDestroyOnLoad在切换场景时的坑点
DontDestroyOnLoad 可以保证 Gameobject 以及上面绑定的组件不会销毁,在处理全局控制的时候有用。
public class ExampleClass :MonoBehaviour{
void Awake() {
DontDestroyOnLoad(gameObject);
}
}
运行时就会把这个对象丢到 DontDestroyOnLoad 这里

但是要注意使用 DontDestroyOnLoad 的时候需要注意的坑:
由于使用 DontDestroyOnLoad 的物体不会被释放掉,假设我们写了上面的代码,而物体所在的游戏场景又可以重复进入的时候,游戏物体就会在每次进入场景的时候创建一次,甚至可以无限创建下去,这样的处理明显不妥。
针对性的解决方案:(注意不可以在 Unity 里面使用静态构造方法的方式实例对象)
判断一下引用是否为空,如果为空再设置为 DontDestroyOnLoad,如果不为空则删除此游戏物体
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; set; }//单例
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
else
{
Instance = this;
DontDestroyOnLoad(this);
}
}
}
Unity 加载资源
参考资料 unity3d Resources.Load动态加载资源
Unity3d 常用两种加载资源方案:Resources.Load 和 AssetBundle
Resources.Load 加载资源
官方文档使用 Resources.Load
使用这种方式加载资源,首先需要下 Asset 目录下创建一个名为 Resources 的文件夹,这个命名是 Unity 规定的方式,然后把资源文件放进去,
当然也可以在 Resources 中再创建子文件夹,当然在代码加载时需要添加相应的资源路径
using UnityEngine;
using System.Collections;
public class LoadResDemo : MonoBehaviour {
// 这里的根路径就是 Resources 目录
private string cubePath = "Prebs/MyCubePreb"; // Asset/Resources/Prebs/MyCubePreb
private string spherePath = "MySpherePreb"; // Asset/Resources/MySpherePreb
void Start () {
//把资源加载到内存中
Object cubePreb = Resources.Load(cubePath, typeof(GameObject));
//用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject cube = Instantiate(cubePreb) as GameObject;
//以下同理实现Sphere的动态实例化
//把资源加载到内存中
Object spherePreb = Resources.Load(spherePath, typeof(GameObject));
//用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject sphere = Instantiate(spherePreb) as GameObject;
}
void Update () {
}
}
注意!! Resources.Load 加载资源不用加文件后缀
// 加载 Assets/Resources/TileSprite/TileSpriteAtlas.asset
// 不用加文件后缀
SpriteLibraryAsset sa = Resources.Load<SpriteLibraryAsset>("TileSprite/TileSpriteAtlas");
加载 JSON 文件
public string JsonRead(string name)
{
string json = "";
TextAsset text = Resources.Load<TextAsset>("Jsons/" + name);
json = text.text;
if (string.IsNullOrEmpty(json)) return null;
return json;
}
获取文件路径
一般存档之类的文件丢到这个 Application.persistentDataPath 目录里面来
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class GameSaveManager : MonoBehaviour
{
public Inventory myInventory;
void Awake()
{
SaveGame();
}
public void SaveGame()
{
Debug.Log(Application.persistentDataPath);
// 如果不存在则 创建目录
if (!Directory.Exists(Application.persistentDataPath + "/game_SaveData"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/game_SaveData");
}
}
}
创建资源路径
AssetDatabase.GetAssetPath 获取资源路径
@MenuItem("GameObject/Create Material")
static function CreateMaterial () {
//新建一个简单的材质资源
var material = new Material (Shader.Find("Specular"));
// 创建一个资源到 Assets 目录下面
AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");
//打印新建资源的路径
Debug.Log(AssetDatabase.GetAssetPath(material));
}
Unity 场景管理
参考资料 Unity 通用场景管理器分享 参考资料 【Unity】场景管理/ 切换:SceneManager、GetScene、LoadScene、MoveGameObjectToScene等
首先别忘了把当前场景添加到 Build Settings 里面

重新加载场景
GetActiveScene 是取得当前已经激活的场景
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);