Exemplo n.º 1
0
 public AssetBundleDownloadFromODROperation(string assetBundleName)
     : base(assetBundleName)
 {
     // Work around Xcode crash when opening Resources tab when a
     // resource name contains slash character
     request = OnDemandResources.PreloadAsync(new string[] { assetBundleName.Replace('/', '>') });
 }
Exemplo n.º 2
0
    private IEnumerator LoadAsset(string resourceName, Action <AssetBundle> onResourceLoadedCallback)
    {
        // Create the request
        OnDemandResourcesRequest request = OnDemandResources.PreloadAsync(new string[] { "resource_tag" });

        // Wait until request is completed
        yield return(request);

        // Check for errors
        if (request.error != null)
        {
            throw new Exception("ODR request failed: " + request.error);
        }

        // Get path to the resource and use it. Note that at the moment the only API
        // that can load ODR or sliced resources is AssetBundle.CreateFromFile()
        string path = "res://" + resourceName;

        AssetBundle bundle = AssetBundle.CreateFromFile(path);

        onResourceLoadedCallback(bundle);

        // Call Dispose() when resource is no longer needed. This will release a pin on ODR resource.
        request.Dispose();
    }
Exemplo n.º 3
0
            public IEnumerator LoadAsset(string resourceName, string odrTag)
            {
                Debug.Log("TTPOnDemandsResources::IosImpl:LoadAsset:resourceName=" + resourceName);
                // Create the request
                OnDemandResourcesRequest request = OnDemandResources.PreloadAsync(new string[] { odrTag });

                while (!request.isDone)
                {
                    Debug.Log("TTPOnDemandsResources::IosImpl:LoadAsset:Request:" + resourceName + " - " + (request.progress * 100) + "%");
                    yield return(new WaitForFixedUpdate());
                }

                // Wait until request is completed
                //yield return request;

                // Check for errors
                if (!string.IsNullOrEmpty(request.error))
                {
                    Debug.LogError("TTPOnDemandsResources::IosImpl:LoadAsset: " + request.error);
                    if (Delegate != null)
                    {
                        Delegate.OnODRFailed("{\"bundleName\":\"" + resourceName + "\", \"errorCode\":" + request.error + "}");
                    }
                    yield break;
                }

                var path = request.GetResourcePath(resourceName);

                Debug.Log("TTPOnDemandsResources::IosImpl:LoadAsset:path=" + path);
                bool        isBundleDownloaded = true;
                AssetBundle bundle             = AssetBundle.LoadFromFile("res://" + resourceName);

                if (bundle == null)
                {
                    Debug.LogError("TTPOnDemandsResources::IosImpl:LoadAsset: Bundle is null in res after request " + resourceName);
                    bundle = AssetBundle.LoadFromFile("odr://" + resourceName);
                    if (bundle == null)
                    {
                        Debug.LogError("TTPOnDemandsResources::IosImpl:LoadAsset: Bundle is null in odr after request " + resourceName);
                        isBundleDownloaded = false;
                        if (Delegate != null)
                        {
                            Delegate.OnODRFailed("{\"bundleName\":\"" + resourceName + "\", \"errorCode\":Bundle is null after request}");
                        }
                    }
                }
                if (isBundleDownloaded)
                {
                    if (Delegate != null)
                    {
                        Delegate.OnODRInstalled("{\"bundleName\":\"" + resourceName + "\"}");
                    }
                }

                request.Dispose();
            }
        public ResourceODRRequest(string[] tags, bool isVirtual)
        {
#if UNITY_IOS
            if (!isVirtual)
            {
                _odrRequest = OnDemandResources.PreloadAsync(tags);
                return;
            }

            _virtualStartTime    = Time.time;
            _virtualDownloadTime = CocoDebugSettingsData.Instance.VirtualODRDownloadTime * tags.Length;
#else
            var tag = tags.Length > 0 ? tags [0] : string.Empty;
            Error = "this platform NOT be supported.";
            Debug.LogErrorFormat("{0}->New: tags [{1}], isVirtual [{2}], error [{3}]", GetType().Name, tag, isVirtual, Error);
#endif
        }
Exemplo n.º 5
0
        public override bool Update()
        {
            if (done)
            {
                return(false);
            }

            if (request == null || request.isDone)
            {
                if (request == null || string.IsNullOrEmpty(request.error))
                {
                    FinishDownload();
                    done = true;
                    return(false);
                }

                request.Dispose();
                request = OnDemandResources.PreloadAsync(new string[] { assetBundleName.Replace('/', '>') });
            }

            return(true);
        }
        /// <summary>
        /// Loads the asset bundle.
        /// </summary>
        private IEnumerator OnDemandResourcesAdaptorImpl(string assetBundleName, System.Func <string, string> getAssetTagFunc)
        {
            if (string.IsNullOrEmpty(assetBundleName))
            {
                throw new System.ArgumentNullException("assetBundleName");
            }

            m_AssetBundle     = null;
            m_AssetBundleName = assetBundleName;
            m_Error           = null;
            m_KeepWaiting     = true;

                        #if UNITY_IOS || UNITY_TVOS
            string assetTag;
            if (getAssetTagFunc != null)
            {
                assetTag = getAssetTagFunc(assetBundleName);
            }
            else
            {
                assetTag = assetBundleName;
            }

            Debug.LogFormat("[OnDemandResourcesAdaptor] Requesting ODR Tag : {0}", assetTag);

            m_OnDemandResourcesRequest = OnDemandResources.PreloadAsync(new string[] { assetTag });
            while (!m_OnDemandResourcesRequest.isDone)
            {
                m_Progress = m_OnDemandResourcesRequest.progress;
                if (!string.IsNullOrEmpty(m_Error))
                {
                    m_AssetBundle = null;
                    m_KeepWaiting = false;
                    m_OnDemandResourcesRequest.Dispose();
                    m_OnDemandResourcesRequest = null;
                    m_Progress = -1;
                    yield break;
                }
                yield return(null);
            }

            if (!string.IsNullOrEmpty(m_OnDemandResourcesRequest.error))
            {
                m_AssetBundle = null;
                m_Error       = m_OnDemandResourcesRequest.error;
                m_KeepWaiting = false;
                m_OnDemandResourcesRequest.Dispose();
                m_OnDemandResourcesRequest = null;
                m_Progress = -1;
                yield break;
            }

            // TODO:
            // The documentation isn't very detailed, but it seems like GetResourcePath
            // should return the path to the asset bundle. As far as I can tell, it
            // always returns null. In Unity's demo project, they just prefix the asset
            // bundle name with "res://" (and don't use GetResourcePath).

            string assetBundlePath;
            // assetBundlePath = m_OnDemandResourcesRequest.GetResourcePath(assetBundleName);
            assetBundlePath = "res://" + assetBundleName;

            if (string.IsNullOrEmpty(assetBundlePath))
            {
                m_AssetBundle = null;
                m_Error       = string.Format("Could not get resource path for asset bundle: {0}", assetBundleName);
                m_KeepWaiting = false;
                m_OnDemandResourcesRequest.Dispose();
                m_OnDemandResourcesRequest = null;
                m_Progress = -1;
                yield break;
            }

            // Skip any loading from the adaptor.
            if (m_DownloadOnly)
            {
                Debug.LogFormat("[OnDemandResourcesAdaptor] Bundle loading skipped : {0}", assetBundleName);
                m_Error       = null;
                m_KeepWaiting = false;
                m_Progress    = 1;
                yield break;
            }

            Debug.LogFormat("[OnDemandResourcesAdaptor] Load asset bundle : {0}", assetBundleName);
            AssetBundleCreateRequest assetBundleCreateRequest;
            assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(assetBundlePath);
            yield return(assetBundleCreateRequest);

            if (assetBundleCreateRequest.assetBundle == null)
            {
                m_AssetBundle = null;
                m_Error       = string.Format("Could not load asset bundle: {0}", assetBundleName);
                m_KeepWaiting = false;
                m_OnDemandResourcesRequest.Dispose();
                m_OnDemandResourcesRequest = null;
                m_Progress = -1;
                yield break;
            }

            m_AssetBundle = assetBundleCreateRequest.assetBundle;
            m_Error       = null;
            m_KeepWaiting = false;
            m_Progress    = 1;
            yield break;
                        #else
            if (m_DownloadOnly)
            {
                // suppress unused variable warning on non-iOS platforms
            }

            m_Error       = string.Format("OnDemandResources is not available on the current platform: {0}", Application.platform);
            m_KeepWaiting = false;
            m_Progress    = -1;
            yield break;
                        #endif
        }
Exemplo n.º 7
0
 public AssetBundleDownloadFromODROperation(string assetBundleName)
     : base(assetBundleName)
 {
     request = OnDemandResources.PreloadAsync(new string[] { assetBundleName });
 }