Esempio n. 1
0
        /// <summary>
        /// Unload previously loaded shader
        /// </summary>
        /// <param name="asset">Shader asset</param>
        public void ShaderUnload(ShaderAsset asset)
        {
            if (asset == null)
            {
                return;
            }

            // Abort any existing async load for this asset
            AbortAsyncShaderLoad(asset);

            asset.InternalSetErrorStatus(RB.AssetStatus.Invalid, RB.Result.Undefined);

            if (asset.shader != null)
            {
                asset.shader = null;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Load a shader asset from given location
        /// </summary>
        /// <param name="fileName">Filename</param>
        /// <param name="asset">Asset object to load into</param>
        /// <param name="source">Source type</param>
        /// <returns>True if successful</returns>
        public bool ShaderLoad(string fileName, ShaderAsset asset, RB.AssetSource source)
        {
            if (asset == null)
            {
                return(false);
            }

            asset.progress = 0;

            if (asset.shader != null)
            {
                asset.shader = null;
            }

            // Abort any existing async load for this asset
            AbortAsyncShaderLoad(asset);

            if (fileName == null)
            {
                Debug.LogError("Shader filename is null!");
                asset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
                return(false);
            }

            fileName.Replace('\\', '/');

            var asyncResource = new RBShaderLoader();

            asyncResource.Load(fileName, asset, source);

            if (asset.status == RB.AssetStatus.Ready)
            {
                return(true);
            }

            // Always add to async queue, even if immediately failed. This gives out consistent async method of error checking
            mASyncShaders.Add(asyncResource);

            return(true);
        }
        /// <summary>
        /// Update asynchronous loading
        /// </summary>
        public void Update()
        {
            // If not loading then there is nothing to update
            if (shaderAsset == null || shaderAsset.status != RB.AssetStatus.Loading)
            {
                return;
            }

            if (mResourceRequest != null)
            {
                if (mResourceRequest.isDone)
                {
                    if (mResourceRequest.asset == null)
                    {
                        Debug.LogError("Could not load shader from " + path + ", make sure the resource is placed somehwere in Assets/Resources folder");
                        shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    }
                    else
                    {
                        var shader = (Shader)mResourceRequest.asset;
                        if (shader == null)
                        {
                            Debug.LogError("Could not load shader from " + path + ", make sure the resource is placed somehwere in Assets/Resources folder");
                            shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                            return;
                        }

                        FinalizeShader(shader);
                    }
                }
                else
                {
                    shaderAsset.progress = mResourceRequest.progress;
                }
            }
#if ADDRESSABLES_PACKAGE_AVAILABLE
            else if (mAddressableRequest.IsValid())
            {
                if (mAddressableRequest.Status == AsyncOperationStatus.Failed)
                {
                    // Can't really figure out failure reason
                    Addressables.Release(mAddressableRequest);
                    shaderAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return;
                }
                else if (mAddressableRequest.Status == AsyncOperationStatus.Succeeded)
                {
                    shaderAsset.progress = 1;
                    var shader = mAddressableRequest.Result;

                    if (shader == null)
                    {
                        Debug.Log("Could not load shader from " + path);
                        Addressables.Release(mAddressableRequest);
                        shaderAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                    }
                    else
                    {
                        FinalizeShader(shader);
                        shaderAsset.addressableHandle = mAddressableRequest;
                        shaderAsset.SetErrorStatus(RB.AssetStatus.Ready, RB.Result.Success);
                    }

                    return;
                }

                if (!mAddressableRequest.IsDone)
                {
                    shaderAsset.progress = mAddressableRequest.PercentComplete;
                    return;
                }
            }
#endif
            // WWW not supported for shaders
        }
        /// <summary>
        /// Load shader asset
        /// </summary>
        /// <param name="path">Path to load from</param>
        /// <param name="asset">ShaderAsset to load into</param>
        /// <param name="source">Source type</param>
        /// <returns>True if successful</returns>
        public bool Load(string path, ShaderAsset asset, RB.AssetSource source)
        {
            shaderAsset = asset;
            this.path   = path;

            if (path == null)
            {
                shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
            }

            if (asset == null)
            {
                Debug.LogError("ShaderAsset is null!");
                return(false);
            }

            if (source == RB.AssetSource.Resources)
            {
                // Synchronous load
                if (path == null)
                {
                    Debug.LogError("Shader filename is null!");
                    shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
                    return(false);
                }

                var shader = Resources.Load <Shader>(path);

                if (shader == null)
                {
                    Debug.LogError("Could not load shader from " + path + ", make sure the resource is placed somehwere in Assets/Resources folder");
                    shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                    return(false);
                }

                return(FinalizeShader(shader));
            }
            else if (source == RB.AssetSource.WWW)
            {
                // Not a supported source, should never get here
                shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotSupported);
                return(false);
            }
            else if (source == RB.AssetSource.ResourcesAsync)
            {
                // Finally attempt async resource load
                mResourceRequest = Resources.LoadAsync <Shader>(this.path);

                if (mResourceRequest == null)
                {
                    shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);
                    return(false);
                }

                shaderAsset.progress = 0;
                shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);
            }
#if ADDRESSABLES_PACKAGE_AVAILABLE
            else if (source == RB.AssetSource.AddressableAssets)
            {
                // Exceptions on LoadAssetAsync can't actually be caught... this might work in the future so leaving it here
                try
                {
                    mAddressableRequest = Addressables.LoadAssetAsync <Shader>(this.path);
                }
                catch (UnityEngine.AddressableAssets.InvalidKeyException e)
                {
                    RBUtil.Unused(e);
                    shaderAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                    return(false);
                }
                catch (Exception e)
                {
                    RBUtil.Unused(e);
                    shaderAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return(false);
                }

                // Check for an immediate failure
                if (mAddressableRequest.Status == AsyncOperationStatus.Failed)
                {
                    Addressables.Release(mAddressableRequest);
                    shaderAsset.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return(false);
                }

                shaderAsset.SetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);
                shaderAsset.progress = 0;

                return(true);
            }
#endif
            else
            {
                shaderAsset.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotSupported);
                return(false);
            }

            return(true);
        }