Ejemplo n.º 1
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);
            }
        }
    }
Ejemplo n.º 2
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);
    }