赞
踩
但凡学习unity的小伙伴,都难免要跟unity的资源打交道,但是过段时间不用就容易忘记,我也是,因此特地整理一下经常用的资源加载的方式,已供大家跟自己在以后查阅。
这也是很常用的一种方式,不管是在运行期,还是在编辑器下,都是可以使用的。unity官方给了下面几种示例:
- // Load assets from the Resources folder. Ignore other named and typed assets.
- using UnityEngine;
-
- public class ExampleClass : MonoBehaviour
- {
- void Start()
- {
- //Load a text file (Assets/Resources/Text/textFile01.txt)
- var textFile = Resources.Load<TextAsset>("Text/textFile01");
-
- //Load text from a JSON file (Assets/Resources/Text/jsonFile01.json)
- var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
- //Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
-
- //Load a Texture (Assets/Resources/Textures/texture01.png)
- var texture = Resources.Load<Texture2D>("Textures/texture01");
-
- //Load a Sprite (Assets/Resources/Sprites/sprite01.png)
- var sprite = Resources.Load<Sprite>("Sprites/sprite01");
-
- //Load an AudioClip (Assets/Resources/Audio/audioClip01.mp3)
- var audioClip = Resources.Load<AudioClip>("Audio/audioClip01");
- }
- }
特点:
示例:
先创建一个预设,步骤如下:
1.先随意创建一个spere,然后创建一个预设,放到Resources/Prefab下:
2.然后创建一个material,并给material一个材质,并且赋给上面的prefab:
3.然后创建一个脚本,叫ResourcesManager,并挂在Main Camera下:
4.在脚本中加入测试代码:
- using UnityEngine;
-
- namespace Game.Framework
- {
- public class ResourcesManager : MonoBehaviour
- {
- private GameObject gameObject;
- private void Awake()
- {
- GameObject prefab = Resources.Load<GameObject>("Prefab/Sphere");
- gameObject = Instantiate(prefab);
- }
-
- private void Update()
- {
- gameObject.transform.Translate(Vector3.right * Time.deltaTime);
- UnityEngine.Debug.LogFormat("gameObject pos : {0}", gameObject.transform.position);
- }
- }
- }
5.运行,发现出现了一个新创建的球,在场景中运动。
有时候我们需要不打包资源,运行个各个平台时,有时候需要访问原文件,unity会把我们Assets/StreamingAssets 下的文件原模原样的复制到目标机器。如常见的mp4文件。
特点:
- string path =
- #if UNITY_ANDROID && !UNITY_EDITOR
- Application.streamingAssetsPath + "/Json/GameConfig.json";
- #elif UNITY_IPHONE && !UNITY_EDITOR
- "file://" + Application.streamingAssetsPath + "/Json/GameConfig.json";
- #elif UNITY_STANDLONE_WIN || UNITY_EDITOR
- "file://" + Application.streamingAssetsPath + "/Json/GameConfig.json";
- #else
- string.Empty;
- #endif
示例:
1.新建一个json文件如下,内容随便加点什么:
2.在脚本中加入以下代码:
- using UnityEngine;
-
- namespace Game.Framework
- {
- public class ResourcesManager : MonoBehaviour
- {
- private void Awake()
- {
- string path =
- #if UNITY_ANDROID && !UNITY_EDITOR
- Application.streamingAssetsPath + "/Json/GameConfig.json";
- #elif UNITY_IPHONE && !UNITY_EDITOR
- "file://" + Application.streamingAssetsPath + "/Json/GameConfig.json";
- #elif UNITY_STANDLONE_WIN || UNITY_EDITOR
- "file://" + Application.streamingAssetsPath + "/Json/GameConfig.json";
- #else
- string.Empty;
- #endif
- StartCoroutine(ReadData(path));
- }
-
- IEnumerator ReadData(string path)
- {
- WWW www = new WWW(path);
- yield return www;
- while (www.isDone == false)
- {
- yield return new WaitForEndOfFrame();
- }
- yield return new WaitForSeconds(0.5f);
- string data = www.text;
- UnityEngine.Debug.Log(data);
- yield return new WaitForEndOfFrame();
- }
- }
- }
3.运行时,查看console输出data的数据:
现在基本每个游戏都有用到这种方式,原因主要是要热更新,主要包括一些图片,预设,模型,场景等。
特点:
示例:
1.先将方式一种的prefab进行ab标记
2.新建一个编辑器功能脚本AssetBundleBuilder,放到Editor目录下:
3.AssetBundleBuilder中写入如下代码:
- using UnityEditor;
- using UnityEngine;
-
- public class AssetBundleBuilder : Editor
- {
- [MenuItem("Custom Editor/Build AssetBundles")]
- static void BuildAsset()
- {
- BuildPipeline.BuildAssetBundles("Assets/StreamingAssets", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
- }
- }
4.然后在unity中点击如下按钮:
5.等打包完,然后会在我们制定的目录Assets/StreamingAssets下生成对应的ab文件:
6.最后在代码中写入如下代码,测试是否能够正确加载:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
-
- namespace Game.Framework
- {
- public class ResourcesManager : MonoBehaviour
- {
- private void Awake()
- {
- string path = Application.streamingAssetsPath + "/";
- AssetBundle assetbundle = AssetBundle.LoadFromFile(path + "prefab.sphere");
- GameObject Prefab = assetbundle.LoadAsset<GameObject>("Sphere");
- if (Prefab != null)
- {
- UnityEngine.Debug.Log("success");
- }
- else
- {
- UnityEngine.Debug.Log("fail");
- }
- }
- }
- }
7.当我们运行的时候发现:
说明我们已经正确的读取了ab的资源了。
如果对任意目录希望可以访问资源,我们可以使用AssetDatabase
特点:
示例:
- using UnityEngine;
- using UnityEditor;
-
- public class MyPlayer : MonoBehaviour
- {
- [MenuItem("AssetDatabase/LoadAssetExample")]
- static void ImportExample()
- {
- Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D));
- }
- }
加载方法:AssetDatabase.LoadAssetAtPath(string assetPath, Type type);
怎么测试,上面已经讲了很多了,有兴趣的读者试一下就知道了。
码字完毕,哈哈,很简单的东西,但是经常用到,这里简单的记录一下,以供自己以后使用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。