一,打包资源

Editor文件夹下

Packing.cs

using UnityEngine;
using System.Collections;
//
using UnityEditor;
using System.Collections.Generic;   // 加了一个容器 List

public class Packing
{  // 编辑器扩展不继承于MonoBehaviour

    [MenuItem("Packing/Make AssetsBundle")]
    static void PackAsset()
    {
        Debug.Log("Make AssetsBundle");
        //
        List<AssetBundleBuild> builds = new List<AssetBundleBuild>();
        AssetBundleBuild build = new AssetBundleBuild();

        build.assetBundleName = "first.unity3d";
        // build.assetNames[0] = "Assets/Resources/mascot.prefab";
        build.assetNames = new string[] { "Assets/Resources/mascot.prefab" };
        builds.Add(build);
        // 第一个参数为打包文件的输出路径,第二个参数为打包资源的列表,第三个参数为打包需要的操作,第四个为打包的输出的环境
        BuildPipeline.BuildAssetBundles(@"Assets/Bundle", builds.ToArray(),
            BuildAssetBundleOptions.None, BuildTarget.WebPlayerStreamed);
        Debug.Log("11111" + builds.ToArray() + "222222");
    }

    [MenuItem("Packing/Pack2")]
    static void Pack2()
    {
        Debug.Log("Pack2");
    }
}
二,加载资源

using UnityEngine;
using System.Collections;

public class LoadAssetBundle : MonoBehaviour {

	// Use this for initialization
	void Start () {
        StartCoroutine(LoadAssetBundle1());
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    IEnumerator LoadAssetBundle1()
    {
        WWW www = new WWW("file:///F:/Unity3d_program/LoadResource/Assets/Bundle/first.unity3d");   //从网上下载

        print(1);
        yield return www;
        print(2);
        AssetBundle bundle = www.assetBundle;
        /*
        //1,异步加载
        AssetBundleRequest request = bundle.LoadAssetAsync("Assets/Resources/mascot.prefab", typeof(GameObject));
        yield return request;
        print(3);
        GameObject go = request.asset as GameObject;
        Instantiate(go);
        */
        //2,同步加载
        //GameObject go1 = bundle.Load<GameObject>("Assets/Resources/mascot.prefab");
        GameObject go1 = bundle.LoadAsset<GameObject>("Assets/Resources/mascot.prefab");
        Instantiate(go1);

        www.Dispose();
    }
}



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐