Example #1
0
        public FileHandle LoadBundle(string path, System.Action LoadEndAct = null)
        {
            FileHandle file = new FileHandle();

            file.m_LoadEndAct = LoadEndAct;
            file.m_LoadPath   = path;
            if (m_BundleDic.ContainsKey(path))
            {
                file.m_AssetBundle = m_BundleDic[path];
                file.LoadEnd();
                return(file);
            }

            StartCoroutine(WebRequestLoadBundle(path, file));
            return(file);
        }
Example #2
0
        private IEnumerator WebRequestLoadBundle(string path, FileHandle file)
        {
            Debug.LogWarning("WebRequestLoad:" + path);

            var www = UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(path);

            //var www = UnityEngine.Networking.UnityWebRequest.Get(path);
            file.m_UnityWebRequest = www;
            //DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
            //www.downloadHandler = handler;
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.LogError("LoadByWebRequest Error:" + path + ",Error:" + www.error);
                file.m_LoadError = true;
            }
            else
            {
                var bundle = (www.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
                Debug.LogWarning("bundle:" + bundle.name);
                file.m_AssetBundle = bundle;
                m_BundleDic.Add(path, bundle);
                #region Material & Shader
#if UNITY_EDITOR_OSX || UNITY_EDITOR
                {
                    var mats = bundle.LoadAllAssets(typeof(Material));
                    foreach (Material mat in mats)
                    {
                        var shaderName      = mat.shader.name;
                        var shaderInRuntime = Shader.Find(shaderName);
                        if (shaderInRuntime != null)
                        {
                            mat.shader = shaderInRuntime;
                            Debug.Log(string.Format("Found the shader: {0} used in mat: {1}", shaderName, mat.name));
                        }
                        else
                        {
                            Debug.Log(string.Format("Cant not find the shader: {0} used in mat: {1}", shaderName, mat.name));
                        }
                    }
                }
                {
                    var             objs = bundle.LoadAllAssets(typeof(GameObject));
                    List <Renderer> rs   = new List <Renderer>();
                    foreach (GameObject obj in objs)
                    {
                        var rends = obj.GetComponentsInChildren <Renderer>();
                        foreach (var r in rends)
                        {
                            rs.Add(r);
                        }
                    }
                    foreach (Renderer r in rs)
                    {
                        var mats = r.sharedMaterials;
                        foreach (Material mat in mats)
                        {
                            var shaderName      = mat.shader.name;
                            var shaderInRuntime = Shader.Find(shaderName);
                            if (shaderInRuntime != null)
                            {
                                mat.shader = shaderInRuntime;
                                Debug.Log(string.Format("Found the shader: {0} used in mat: {1}", shaderName, mat.name));
                            }
                            else
                            {
                                Debug.Log(string.Format("Cant not find the shader: {0} used in mat: {1}", shaderName, mat.name));
                            }
                        }
                    }
                }
#endif
                #endregion
            }
            file.LoadEnd();
        }