예제 #1
0
    public static void LoadResPackageAsyn(string PackName, IResourceLoad iLoad, bool PureAsyn = true)
    {
        if (!Application.isPlaying)
        {
            return;
        }

        if (PackName.Length <= 0)
        {
            ResLoadResult ret = new ResLoadResult();
            ret.fProgress = 1.0f;
            ret.resObj    = null;
            ret.sErrorMsg = "";
            ret.sPath     = PackName;
            Singleton.InvokePackageCallback(ret, PackName, iLoad);
            return;
        }

        if (Singleton.CachePackages.Contains(PackName))
        {
            ResLoadResult ret = new ResLoadResult();
            ret.fProgress = 1.0f;
            ret.resObj    = Singleton.CachePackages[PackName] as AssetBundle;
            ret.sErrorMsg = "";
            ret.sPath     = PackName;
            Singleton.InvokePackageCallback(ret, PackName, iLoad);
            return;
        }

        if (Singleton.RequestTable.Contains(PackName))
        {
            RequestPackageInfo info = Singleton.RequestTable[PackName] as RequestPackageInfo;
            if (info.bLoading == true)
            {
                return;
            }
        }

        if (PureAsyn)
        {
            Singleton.StartCoroutine(Singleton.LoadResPackageAsynEnumerator(PackName, iLoad));
        }
        else
        {
            ResLoadResult ret = new ResLoadResult();
            ret.fProgress = 1.0f;
            ret.resObj    = null;
            ret.sErrorMsg = "";
            ret.sPath     = PackName;

            LoadResPackageSync(PackName);

            Singleton.InvokePackageCallback(ret, PackName, iLoad);
        }
    }
예제 #2
0
    private void InvokePackageCallback(ResLoadResult ret, string PackName, IResourceLoad iLoad)
    {
        bool bSucc = CachePackages.Contains(PackName);

        if (bSucc)
        {
            if (iLoad != null)
            {
                iLoad.ResourceLoadSucc(ret);
            }
        }
        else
        {
            if (iLoad != null)
            {
                iLoad.ResourceLoadFail(ret);
            }
        }

        if (bSucc)
        {
            AssetBundle assetbundle = CachePackages[PackName] as AssetBundle;
            if (RequestTable.Contains(PackName))
            {
                RequestPackageInfo      packinfo    = RequestTable[PackName] as RequestPackageInfo;
                List <RequestAssetInfo> RequestList = packinfo.RequestAssets;
                for (int i = 0; i < RequestList.Count; i++)
                {
                    RequestAssetInfo info = RequestList[i];
                    info.obj = null;
                    if (assetbundle.Contains(info.resPath))
                    {
                        info.obj = assetbundle.LoadAsset(info.resPath);
                    }
                    if (info.obj == null)
                    {
                        string resShort = System.IO.Path.GetFileNameWithoutExtension(info.resPath);
                        if (assetbundle.Contains(resShort))
                        {
                            info.obj = assetbundle.LoadAsset(resShort);
                        }
                    }
                    RequestList[i] = info;
                }
                foreach (RequestAssetInfo info in RequestList)
                {
                    InvokeAssetCallback(info.obj, info.resPath, info.iLoad);
                }
                RequestTable.Remove(PackName);
            }
        }
    }
예제 #3
0
    private void LoadFromAssetBundleAsynImp(string resPath, IResourceLoad iLoad)
    {
        UnityEngine.Object ret = null;

        long t1 = Api.GetTickCount();

        //----------------------------------------------------
        //step1: try to load from packages
        //----------------------------------------------------
        //step1.1: unload packages
        int trynumber = CachePackages.Count - CachePackageNumber;

        for (int i = 0; i < trynumber; i++)
        {
            string selectkey = "";
            if (CacheOrders.Count > 1)
            {
                selectkey = CacheOrders[0];
                if (selectkey == "patch")
                {
                    selectkey = CacheOrders[1];
                }
            }
//				foreach(DictionaryEntry keyvalue in CachePackages)
//				{
//					string key=keyvalue.Key as string;
//					if (key!="patch" && RequestTable.Contains(key)==false) //unload where it is not requesting
//					{
//						selectkey=key;
//						break;
//					}
//				}
            if (selectkey.Length > 0)
            {
                UnLoadResPackage(selectkey);
            }
        }
        //System.GC.Collect ();

        //step1.2: load packages
        string PackName = "";

        if (CachePackages.Contains("patch"))
        {
            PackName = "patch";           //set default to patch if it is exists
        }
        if (ResourceList.Contains(resPath.ToLower()))
        {
            PackName = ResourceList [resPath.ToLower()] as string;
        }
        AssetBundle PackageAssetBundle = null;

        if (PackName.Length > 0)
        {
            if (!CachePackages.Contains(PackName))
            {
                if (!RequestTable.Contains(PackName))
                {
                    RequestPackageInfo packnewinfo = new RequestPackageInfo();
                    RequestTable[PackName] = packnewinfo;
                }
                RequestPackageInfo packinfo = RequestTable[PackName] as RequestPackageInfo;
                RequestAssetInfo   info     = new RequestAssetInfo();
                info.resPath = resPath;
                info.iLoad   = iLoad;
                packinfo.RequestAssets.Add(info);
                RequestTable[PackName] = packinfo;
                LoadResPackageAsyn(PackName, null, true);
                return;
            }
            PackageAssetBundle = CachePackages[PackName] as AssetBundle;
        }

        //step1.3: then try to load from patch package
        if (PackageAssetBundle != null)
        {
            if (PackageAssetBundle.Contains(resPath))
            {
                ret = PackageAssetBundle.LoadAsset(resPath);
            }
            if (ret == null)
            {
                string resShort = System.IO.Path.GetFileNameWithoutExtension(resPath);
                if (PackageAssetBundle.Contains(resShort))
                {
                    ret = PackageAssetBundle.LoadAsset(resShort);
                }
            }
        }

        long t2 = Api.GetTickCount();
        long dt = t2 - t1;

        if (ret != null)
        {
            Trace.Log("LoadFromAssetBundleAsynImp OK, respath = " + resPath + ", pack=" + PackName + ", time=" + dt.ToString() + "ms");
        }
        else
        {
            Trace.LogError("LoadFromAssetBundleAsynImp fail, respath = " + resPath + ", pack=" + PackName + ", time=" + dt.ToString() + "ms");
        }

        InvokeAssetCallback(ret, resPath, iLoad);
    }
예제 #4
0
    IEnumerator LoadResPackageAsynEnumerator(string PackName, IResourceLoad iLoad)
    {
        if (RequestTable.Contains(PackName))
        {
            RequestPackageInfo info = RequestTable[PackName] as RequestPackageInfo;
            if (info.bLoading == true)
            {
                yield break;
            }
        }

        Resources.UnloadUnusedAssets();
        Caching.CleanCache();

        ResLoadResult ret = new ResLoadResult();

        ret.fProgress = 1.0f;
        ret.resObj    = null;
        ret.sErrorMsg = "";
        ret.sPath     = PackName;

        if (RequestTable.Contains(PackName))
        {
            RequestPackageInfo info = RequestTable[PackName] as RequestPackageInfo;
            info.bLoading          = true;
            RequestTable[PackName] = info;
        }

        long   t1         = Api.GetTickCount();
        string ext        = bEncrypt ? ".zen" : ".unity3D";
        string outerpath  = ResDef.OuterPackageDirectory + PackName + ext;
        string innerpath1 = ResDef.InterPackageDirectoryEx + PackName + ext;
        string innerpath2 = ResDef.InterPackageDirectory + PackName + ext;

        string finalpath = "";
        WWW    www       = null;
        AssetBundleCreateRequest request = null;

        //外包优先
        if (System.IO.File.Exists(outerpath))
        {
            finalpath = outerpath;
            //if (!bEncrypt &&(Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)) //对于外包,如果没加密则直接采用读文件方式
            if (false)
            {
                byte[] data = System.IO.File.ReadAllBytes(outerpath);
                if (data != null)
                {
                    request = AssetBundle.LoadFromMemoryAsync(data);
                    while (!request.isDone)
                    {
                        yield return(new WaitForEndOfFrame());
                    }
                }
                data = null;
                System.GC.Collect();
                yield return(new WaitForEndOfFrame());
            }
            else
            {
                www = WWW.LoadFromCacheOrDownload("file:///" + outerpath, 5);
                //www = new WWW("file:///" + outerpath);
                yield return(www);
            }
        }
        //内包由于有可能在jar/zip等压缩包内,只能通过www组件去提取
        else if (System.IO.File.Exists(innerpath1))
        {
            finalpath = innerpath1;
            www       = new WWW("file:///" + innerpath1);
            yield return(www);
        }
        else
        {
            finalpath = innerpath2;
            yield return(true);
        }

        if (RequestTable.Contains(PackName))
        {
            RequestPackageInfo info = RequestTable[PackName] as RequestPackageInfo;
            info.bLoading          = false;
            RequestTable[PackName] = info;
        }

        AssetBundle pAssetBundle = null;

        if (www != null)
        {
            if (www.error == null)
            {
                if (!CachePackages.Contains(PackName))
                {
                    try
                    {
                        if (bEncrypt)
                        {
                            string key     = PackName + "*^$@&%#!";
                            byte[] outdata = DecryptDES(www.bytes, key);
                            if (outdata != null)
                            {
#if U462
                                pAssetBundle = AssetBundle.LoadFromMemory(outdata);
#else
                                string temppath = ResDef.UserPath + "temp.unity3d";
                                System.IO.File.WriteAllBytes(temppath, outdata);
                                pAssetBundle = AssetBundle.CreateFromFile(temppath);
#endif
                            }
                        }
                        else
                        {
                            pAssetBundle = www.assetBundle;
                        }
                    }
                    catch (UnityException e)
                    {
                        Trace.LogError(e.ToString());
                    }
                    if (pAssetBundle != null)
                    {
                        CachePackages[PackName] = pAssetBundle;
                        CacheOrders.Add(PackName);
                        long t2 = Api.GetTickCount();
                        long dt = t2 - t1;
                        Trace.Log("LoadResPackageAsynEnumerator OK, www.assetBundle OK:" + finalpath + ",time=" + dt.ToString() + "ms");
                    }
                    else
                    {
                        Trace.LogError("LoadResPackageAsynEnumerator fail, www.assetBundle fail:" + finalpath);
                    }
                }
            }
            else
            {
                Trace.LogError("LoadResPackageAsynEnumerator fail, www.errpr fail:" + www.error);
            }

            www.Dispose();
            www = null;
        }
        else if (request != null)
        {
            if (request.isDone)
            {
                if (!CachePackages.Contains(PackName))
                {
                    try
                    {
                        pAssetBundle = request.assetBundle;
                    }
                    catch (UnityException e)
                    {
                        Trace.LogError(e.ToString());
                    }
                    if (pAssetBundle != null)
                    {
                        CachePackages[PackName] = pAssetBundle;
                        CacheOrders.Add(PackName);
                        long t2 = Api.GetTickCount();
                        long dt = t2 - t1;
                        Trace.Log("LoadResPackageAsynEnumerator OK, www.assetBundle OK:" + finalpath + ",time=" + dt.ToString() + "ms");
                    }
                    else
                    {
                        Trace.LogError("LoadResPackageAsynEnumerator fail, www.assetBundle fail:" + finalpath);
                    }
                }
            }
            else
            {
                Trace.LogError("LoadResPackageAsynEnumerator fail, www.errpr fail:" + www.error);
            }
            request = null;
        }
        else
        {
            Trace.LogError("LoadResPackageAsynEnumerator fail, www null,package doesn't exists:" + finalpath);
        }

        System.GC.Collect();
        yield return(new WaitForEndOfFrame());

        InvokePackageCallback(ret, PackName, iLoad);
    }
예제 #5
0
    /// <summary>
    /// 同步加载资源包
    /// </summary>
    /// <param name="packID">资源包名</param>
    public static AssetBundle LoadResPackageSync(string PackName)
    {
        if (!Application.isPlaying)
        {
            return(null);
        }

        if (PackName.Length <= 0)
        {
            return(null);
        }

        if (Singleton.CachePackages.Contains(PackName))
        {
            return(Singleton.CachePackages[PackName] as AssetBundle);
        }

        if (Singleton.RequestTable.Contains(PackName))
        {
            RequestPackageInfo info = Singleton.RequestTable[PackName] as RequestPackageInfo;
            if (info.bLoading == true)
            {
                return(null);
            }
        }

        string finalpath  = "";
        string ext        = bEncrypt ? ".zen" : ".unity3D";
        string outerpath  = ResDef.OuterPackageDirectory + PackName + ext;
        string innerpath1 = ResDef.InterPackageDirectoryEx + PackName + ext;
        string innerpath2 = ResDef.InterPackageDirectory + PackName + ext;

        byte[] data = null;
        long   t1   = Api.GetTickCount();

        //先读外包
        if (System.IO.File.Exists(outerpath))
        {
            data      = System.IO.File.ReadAllBytes(outerpath);
            finalpath = outerpath;
        }
        //没有的话先以文件系统方式再读内包
        else if (System.IO.File.Exists(innerpath1))
        {
            data      = System.IO.File.ReadAllBytes(innerpath1);
            finalpath = innerpath1;
        }
        //再没有再以包系统方式读内包(安卓下内包可能在jar包里需要多次解压)
                #if SupportPackageIO
        else if (PackageIO.Exists(innerpath2))
        {
            data = PackageIO.ReadAllBytes(innerpath2);
        }
                #endif
        else
        {
            Trace.LogError("LoadResPackageSync fail, the file doesn't exists in paths below:" + outerpath + "," + innerpath1 + "," + innerpath2);
        }
        if (bEncrypt)
        {
            string key     = PackName + "*^$@&%#!";
            byte[] outdata = DecryptDES(data, key);
            if (outdata != null)
            {
                data = outdata;
            }
        }

        if (data != null)
        {
            AssetBundle pAssetBundle = null;
            try
            {
#if U462
                pAssetBundle = AssetBundle.LoadFromMemory(data);
#else
                string temppath = ResDef.UserPath + "temp.unity3d";
                System.IO.File.WriteAllBytes(temppath, data);
                pAssetBundle = AssetBundle.CreateFromFile(temppath);
#endif
            }
            catch (UnityException e)
            {
                Trace.LogError(e.ToString());
            }
            if (pAssetBundle != null)
            {
                Singleton.CachePackages[PackName] = pAssetBundle;
                Singleton.CacheOrders.Add(PackName);
                long t2 = Api.GetTickCount();
                long dt = t2 - t1;
                Trace.Log("LoadResPackageSync OK:" + finalpath + ",time=" + dt.ToString() + "ms");
                return(pAssetBundle);
            }
            else
            {
                Trace.LogError("LoadResPackageSync fail, CreateFromMemoryImmediate fail:" + finalpath);
            }
        }
        else
        {
            Trace.LogError("LoadResPackageSync fail, ReadAllBytes fail:" + finalpath);
        }

        return(null);
    }