Unity-资源加载(Resources,AssetBundle)

Resources类

使用Resources加载资源,资源一定要放到Resources文件夹下,支持名字和路径加载。
1.可以有多个Resources文件夹同时存在。
2.Resources下的资源在打包后是加密压缩状态,运行时不可修改。
3.加载方法 : Load<类型>(名称或路径),名称不需要加后缀。

示例

public class TeachRes : MonoBehaviour {

    void Start () {
        //加载预制体,可以有路径
        GameObject prefab = Resources.Load("Prefabs/UI/Cube");
        GameObject.Instantiate(prefab);

        //加载精灵
        Sprite sprite = Resources.Load("SkyTileSprite");
        GameObject go = new GameObject("sp");
        go.AddComponent().sprite = sprite;

        //加载Texture
        Texture tex = Resources.Load("SkyTileSprite");
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.GetComponent().material.mainTexture = tex;

        //加载Texture2D -> sprite
        Texture2D tex2d = Resources.Load("SkyTileSprite");
        Sprite sp = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0.5f, 0.5f));

        GameObject spriteRender = new GameObject("go");
        go.AddComponent().sprite = spriteRender ;
        //go.AddComponent().sprite = spriteRender ;

        //加载声音类型
        AudioClip clip = Resources.Load("mp3");
        //加载视频类型
        MovieTexture movie = Resources.Load("movie");

        //Unity的GC
        Resources.UnloadAsset(tex);
        //释放unity认为不使用的资源
        Resources.UnloadUnusedAssets();
    }
}

WWW类

支持file://,http://,https://三种协议。
可以读取本地或者网络的资源。

示例

public class TeachWWW: MonoBehaviour {

    IEnumerator Start () {
        //本地路径
        //string url = @"file://D:\SkyTileSprite.png";
        //网络地址
        string url = @"http://192.168.11.3/SkyTileSprite.png";

        WWW www = new WWW(url);
        yield return www;

        Texture2D tex2d = www.texture;
        Sprite sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0.5f, 0.5f));

        GameObject go = new GameObject("sprite");
        go.AddComponent().sprite = sprite;
        //释放bundle
        www.assetBundle.Unload(false);
    }
}

如果资源包是在线的可以使用www下载或者直接加载
WWW加载AssetBundle

public class WWWLoadAB : MonoBehaviour {

    IEnumerator Start () {
        string url = "file://"+Application.streamingAssetsPath+"/+"cube.unity3d";

        WWW www = new WWW(url);
        yield return www;

        AssetBundle bundle= www.assetBundle;
        GameObject prefab = bundle.LoadAsset("Cube");
        Instantiate(prefab );
    }
}

如果是在热更新资源的项目中,一般是把资源先下载到本地的persistentDataPath,然后使用AssetBundle.LoadFromFile方法加载并处理依赖资源。

AssetBundle类 加载资源和依赖

public class LoadAssetbundle : MonoBehaviour
{
    void Start()
    {
        GameObject prefab = LoadAsset("cube.unity3d","cube");
        GameObject.Instantiate(prefab);
    }

    //加载bundle下的资源(包含依赖处理)
    GameObject LoadAsset(string abName,string assetName)
    {
        // 1.加载Manifest总配置文件  
        string path = Application.streamingAssetsPath + "/StreamingAssets";
        AssetBundle manifestBundle = AssetBundle.LoadFromFile(path);
        AssetBundleManifest manifest = manifestBundle.LoadAsset("AssetBundleManifest");

        // 2.获取ab包的依赖文件列表  
        string[] depends = manifest.GetAllDependencies(abName);

        // 3.加载所有的依赖ab包到内存
        foreach (var depName in depends)
        {
            string depPath = Application.streamingAssetsPath + "/" + depName;
            AssetBundle depBundle = AssetBundle.LoadFromFile(depPath);
        }

        // 4.加载资源ab包
        path = Application.streamingAssetsPath + "/"+ abName;
        AssetBundle bundle = AssetBundle.LoadFromFile(path);

        // 5.返回ab包中特定预制体
        return bundle.LoadAsset(assetName);
    }
}

你可能感兴趣的:(Unity-资源加载(Resources,AssetBundle))