统一资产捆绑包3

时间:2019-02-28 05:02:38

标签: c# unity3d assetbundle

加载第二个捆绑软件时出现此错误。

  

ArgumentException:您要实例化的对象为null。   UnityEngine.Object.CheckNullArgument(System.Object arg,System.String消息)(位于C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239)   UnityEngine.Object.Instantiate [GameObject](UnityEngine.GameObject原始)(在C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:200)   AssetDownloader + c__Iterator0.MoveNext()(在Assets / Models / Scripts / AssetDownloader.cs:78处)   UnityEngine.SetupCoroutine.InvokeMoveNext(IEnumerator枚举器,IntPtr returnValueAddress)(位于C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

2 个答案:

答案 0 :(得分:0)

using System.Collections;
using UnityEngine;

public class ABDownloader : MonoBehaviour
{
    public static GameObject s_GearObject;
    private static bool s_CacheCleared = false;

    public string m_Url = "https://drive.google.com/uc?export=download&id=1j_iAg2ESHyNLL62CXYHb40p_X9C1oDM5";
    public string m_AssetName = "assets/prefab/h_gear.prefab";

    private void Awake()
    {
        if (!s_CacheCleared)
        {
            s_CacheCleared = true;
            Caching.ClearCache();
        }
        if (string.IsNullOrEmpty(m_Url))
        {
            Debug.LogError("[ABDownloader] Invalid URL!");
        }

        Load(m_AssetName);
    }

    private void Load(string gearName)
    {
        if (s_GearObject != null)
        {
            Destroy(s_GearObject);
        }

        StartCoroutine(LoadBundle(gearName));
    }

    private IEnumerator LoadBundle(string gearName)
    {
        while (!Caching.ready)
        {
            yield return null;
        }

        gearName = gearName.ToLowerInvariant();
        if (!gearName.StartsWith("assets/"))
        {
            Debug.LogError("[ABDownloader] Please give the asset's full path: " + gearName);
            yield break;
        }

        AssetBundle assetBundle = null;
        WWW www = WWW.LoadFromCacheOrDownload(m_Url, 0);
        yield return www;
        if (!www.isDone || !string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("[ABDownloader] Download assetBundle error: " + www.error + ", please check the URL or Network: " + m_Url);
            yield break;
        }
        assetBundle = www.assetBundle;
        AssetBundleRequest bundleRequest = assetBundle.LoadAssetAsync<GameObject>(gearName);
        yield return bundleRequest;
        if (!bundleRequest.isDone || bundleRequest.asset == null)
        {
            Debug.LogError("[ABDownloader] Load asset from AssetBundle error: " + gearName);
            yield break;
        }
        GameObject obj = bundleRequest.asset as GameObject;
        s_GearObject = Instantiate(obj) as GameObject;
        assetBundle.Unload(false);

        Debug.Log("[ABDownloader] Load asset OK!");
    }
}

答案 1 :(得分:-1)

公共类AssetDownloader:MonoBehaviour {

//https://drive.google.com/file/d//view?usp=sharing
//16NiBSZo7kuX6UqmOE1iJHUrmXJXDGFNC
//https://drive.google.com/uc?export=download&id=16NiBSZo7kuX6UqmOE1iJHUrmXJXDGFNC

public string url;
public static GameObject Gears;
public Text loadingText;

//AssetBundle MatBundle;

//public Transform spawnPos;
public bool mClearCache = false;

void Awake()
{
    Caching.ClearCache();

    if (mClearCache)
        Caching.ClearCache();
}

void Updat()

{



}

IEnumerator LoadBundle(string GearName) 
{
    while (!Caching.ready)
    {
        yield return null;
    }

    //MATERIAL
    //WWW ww = new WWW(Mat);
    //yield return ww;

    //if (ww.error != null)
    //{
    //    throw new System.Exception("Theres as error: " + ww.error);
    //}

    //MatBundle = ww.assetBundle;
    //MatBundle.LoadAllAssets();
    //MATERIAL


    //Begin download
    WWW www = WWW.LoadFromCacheOrDownload (url, 0);
    yield return www;

    //Load the downloaded bundle
    AssetBundle bundle = www.assetBundle;

    //Load an asset from the loaded bundle
    AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(GearName, typeof(GameObject));
    yield return bundleRequest;


    //AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(GearName, typeof(GameObject));

    //get object
    GameObject obj = bundleRequest.asset as GameObject;

    // Gear = Instantiate(obj,spawnPos.position, Quaternion.identity) as GameObject;

    Gears = Instantiate(obj) as GameObject;


    loadingText.text = "";

    bundle.Unload(false);
    //www.Dispose();
}

public void Load(string GearName)
{
    if (Gears)
    {
        Destroy(Gears);
    }

    loadingText.text = "Loading...";
    StartCoroutine(LoadBundle(GearName));


}

}