统一资产包3

问题描述 投票:0回答:2

我加载第二个包时收到此错误。

ArgumentException:要实例化的Object为null。 UnityEngine.Object.CheckNullArgument(System.Object arg,System.String message)(at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239)UnityEngine.Object.Instantiate [GameObject](UnityEngine.GameObject original) )(在C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:200)AssetDownloader + c__Iterator0.MoveNext()(在Assets / Models / Scripts / AssetDownloader.cs:78)UnityEngine.SetupCoroutine.InvokeMoveNext(IEnumerator) enumerator,IntPtr returnValueAddress)(在C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

c# unity3d assetbundle
2个回答
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
投票

公共类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));


}

}

© www.soinside.com 2019 - 2024. All rights reserved.