Пример #1
0
    public static void LoadScene(string name)
    {
#if UNITY_EDITOR && !USE_BUNDLE_IN_EDITOR
        SceneManager.LoadScene(name);
#else
        Debug.LogFormat("XLoader.LoadScene loading: {0}", name);
        AssetBundleManager.Instance.ReleaseSceneCachedBundleOnSceneSwitch();

        XManifest.Scene info = XManifest.Instance.GetScene(name);

        if (info != null)
        {
            XManifest.Pack packInfo = XManifest.Instance.GetPack(info.bundle);
            switch (packInfo.location)
            {
            case XManifest.Location.Resource:
                Debug.LogErrorFormat("XLoader.LoadScene can't load bundled scene {0} in resource!", name);
                break;

            default:
                Debug.LogErrorFormat("[XLoader] can't load scene in sync load {0}", name);
                break;
            }
        }
        else
        {
            SceneManager.LoadScene(name);
        }
#endif
    }
Пример #2
0
 private IEnumerator _LoadDependenciesAsync(XManifest.Pack pack)
 {
     for (int i = 0; i < pack.dependencies.Length; i++)
     {
         AssetBundleInfo bundleInfo = GetAssetBundleInfo(pack.dependencies[i]);
         if (bundleInfo != null)
         {
             if (!bundleInfo.isDone)
             {
                 CoroutineHelper.Run(_LoadAssetBundleAsync(bundleInfo, false));
             }
             bundleInfo.AddDepended(pack.name);
         }
     }
     for (int i = 0; i < pack.dependencies.Length; i++)
     {
         AssetBundleInfo bundleInfo = GetAssetBundleInfo(pack.dependencies[i]);
         if (bundleInfo != null)
         {
             while (bundleInfo.isLoading)
             {
                 yield return(null);
             }
         }
     }
 }
Пример #3
0
    private static IEnumerator _DoLoadSceneAsync(string name, System.Action <bool> callback)
    {
#if UNITY_EDITOR && !USE_BUNDLE_IN_EDITOR
        yield return(SceneManager.LoadSceneAsync(name));
#else
        Debug.LogFormat("XLoader.LoadScene loading: {0}", name);
        XManifest.Scene info = XManifest.Instance.GetScene(name);

        if (info != null)
        {
            Debug.LogFormat("XLoader.LoadScene bundle loading: {0}", name);
            XManifest.Pack packInfo = XManifest.Instance.GetPack(info.bundle);
            switch (packInfo.location)
            {
            case XManifest.Location.Resource:
                Debug.LogErrorFormat("XLoader.LoadScene can't load bundled scene {0} in resource!", name);
                break;

            default:
                yield return(AssetBundleManager.Instance.LoadScene(info));

                break;
            }
        }
        else
        {
            Debug.LogFormat("XLoader.LoadScene native loading: {0}", name);
            yield return(SceneManager.LoadSceneAsync(name));
        }
#endif
        string loadedName = SceneManager.GetActiveScene().name;
        bool   success    = loadedName == name ? true : false;
        Debug.LogFormat("XLoader.LoadScene {0} loaded", loadedName);
        callback(success);
    }
Пример #4
0
 public AssetBundleInfo(XManifest.Pack info)
 {
     pack         = info;
     bundle       = null;
     isDone       = false;
     isLoading    = false;
     refCount     = 0;
     lastReadTime = 0;
 }
Пример #5
0
 private void _LoadDependencies(XManifest.Pack pack)
 {
     for (int i = 0; i < pack.dependencies.Length; i++)
     {
         AssetBundleInfo bundleInfo = GetAssetBundleInfo(pack.dependencies[i]);
         if (bundleInfo != null)
         {
             if (!bundleInfo.isDone)
             {
                 _LoadAssetBundle(bundleInfo, false);
             }
             bundleInfo.AddDepended(pack.name);
         }
     }
 }
Пример #6
0
    private static IEnumerator _DoLoadBundleAsync(string bundleName, System.Action <Object> callback)
    {
#if UNITY_EDITOR && !USE_BUNDLE_IN_EDITOR
        callback(null);
        yield break;
#else
        //Debug.LogFormat("[XLoader] AsyncLoading: ({0} : {1})", path, type.ToString());
        if (string.IsNullOrEmpty(bundleName))
        {
            Debug.LogErrorFormat("[XLoader.LoadBundle] sent empty/null name!");
            yield break;
        }

        XManifest.Pack pack = XManifest.Instance.GetPack(bundleName);
        if (pack == null)
        {
            Debug.LogErrorFormat("[XLoader.LoadBundle] can not find bundle pack: [{0}]", bundleName);
            callback(null);
            yield break;
        }

        XManifest.Location local = pack.NoBundle() ? XManifest.Location.Resource : XManifest.Location.Bundle;
        switch (local)
        {
        case XManifest.Location.Bundle:
        {
            yield return(AssetBundleManager.Instance.LoadBundleAsync(bundleName, delegate(Object result) {  }));
        }
        break;

        default:
            break;
        }
        callback(null);
#endif
    }
Пример #7
0
    static void _PrepareResources(string outPath, bool forUpdate)
    {
        bool zipLua       = false;
        bool pertty       = true;
        bool copyManifest = false;

        if (forUpdate)
        {
            zipLua       = true;
            pertty       = false;
            copyManifest = true;
        }

        XManifest.Instance.Load(Path.Combine(XManifest.resOutputPath, XManifest.name).Replace("\\", "/"));

        int total = 0;

        total += (XManifest.Instance.GetPacks() as Dictionary <string, XManifest.Pack>).Count;
        total += (XManifest.Instance.GetFiles() as Dictionary <string, XManifest.File>).Count;
        int current = 0;

        //copy bundles
        string bundleSrcPath = XManifest.resOutputPath.Replace("\\", "/");
        string bundleAimPath = Path.Combine(outPath, AssetBundleManager.bundleFolder);

        if (Directory.Exists(bundleAimPath))
        {
            Directory.Delete(bundleAimPath, true);
        }
        Directory.CreateDirectory(bundleAimPath);

        foreach (KeyValuePair <string, XManifest.Pack> item in XManifest.Instance.GetPacks())
        {
            XManifest.Pack pack = item.Value;
            File.Copy(Path.Combine(bundleSrcPath, pack.name), Path.Combine(bundleAimPath, Path.ChangeExtension(pack.name, null) + "." + pack.checksum), true);

            EditorUtility.DisplayProgressBar("Prepare Bundle Resource", pack.name, current++ / (float)total);
        }

        // copy luas
        string luaScrPath = Defines.LuaByteCodeOutPath;
        string luaAimPath = Path.Combine(outPath, LuaFilePicker.fileFolder);

        if (Directory.Exists(luaAimPath))
        {
            Directory.Delete(luaAimPath, true);
        }
        Directory.CreateDirectory(luaAimPath);

        foreach (KeyValuePair <string, XManifest.File> item in XManifest.Instance.GetFiles())
        {
            XManifest.File file    = item.Value;
            var            srcName = Path.Combine(luaScrPath, file.name);
            var            srcPath = Path.Combine(luaScrPath, file.name);
            var            aimName = Path.ChangeExtension(file.name, null) + "." + file.checksum;
            var            aimPath = Path.Combine(luaAimPath, aimName);

            File.Copy(srcPath, aimPath, true);

            EditorUtility.DisplayProgressBar("Prepare Bundle Resource", file.name, current++ / (float)total);
        }

        // copy/save manifest
        if (copyManifest)
        {
            uint manifestChecksum = 0;
            uint manifestSize     = 0;
            var  manifestAimPath  = Path.Combine(outPath, XManifest.name);
            //XManifest.Instance.Save(manifestAimPath, pertty);
            XManifestSave(manifestAimPath, pertty);

            GetFileInfo(manifestAimPath, out manifestChecksum, out manifestSize);
            string destName = manifestAimPath + ("." + manifestChecksum);
            File.Copy(manifestAimPath, destName);
        }
        else
        {
            //XManifest.Instance.Save(Path.Combine(outPath, XManifest.name), pertty);
            XManifestSave(Path.Combine(outPath, XManifest.name), pertty);
        }

        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
    }
Пример #8
0
    public static void GenerateFileAllocationTable(BuildTarget buildTarget)
    {
        string targetName = XManifest.GetPlatformFolder(buildTarget);
        string bundlePath = assetBundleOutputPath;

        EditorUtility.DisplayProgressBar("Generate Manifest", "loading bundle manifest", 1 / 2);

        AssetBundle bundle = AssetBundle.LoadFromFile(Path.Combine(bundlePath, targetName));

        if (bundle == null)
        {
            Debug.LogError("Build Resrouce First.");
            EditorUtility.ClearProgressBar();
            return;
        }

        XManifest.Instance.Clear();

        try
        {
            string manifestSavePath = XManifest.manifestOutputPath;

            // gen packs
            AssetBundleManifest manifest = bundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
            string[]            bundles  = manifest.GetAllAssetBundles();
            EditorUtility.DisplayProgressBar("Generate Manifest", "compute bundle hash", 0);
            for (int i = 0; i < bundles.Length; i++)
            {
                string bundleName = bundles[i];

                XManifest.Pack pack = new XManifest.Pack();
                pack.name = bundleName;
                uint checksum = 0;
                uint size     = 0;
                GetFileInfo(Path.Combine(bundlePath, bundleName), out checksum, out size);
                pack.checksum = checksum;
                pack.size     = size;
                pack.location = XManifest.Location.Streaming;
                //pack.preloadType = XManifest.PreloadType.Loading;
                List <string> dependencies = new List <string>(manifest.GetAllDependencies(bundleName));
                dependencies.Remove(bundleName);
                pack.dependencies = dependencies.ToArray();

                XManifest.Instance.Add(pack);

                foreach (string file in AssetDatabase.GetAssetPathsFromAssetBundle(bundleName))
                {
                    if (Path.GetExtension(file) == ".unity")    // gen scenes
                    {
                        XManifest.Scene scene = new XManifest.Scene();
                        scene.name   = Path.GetFileNameWithoutExtension(file);
                        scene.bundle = bundleName;
                        XManifest.Instance.Add(scene);
                    }
                    else if (Path.GetExtension(file) == ".spriteatlas") // gen sporitepack
                    {
                        var name = Path.GetFileNameWithoutExtension(file);

                        if (XManifest.Instance.GetSpritePack(name) != null)
                        {
                            throw new System.Exception("can't have mutip spriteatlas named:" + file);
                        }
                        List <string> allPackable = new List <string>();
                        var           sa          = AssetDatabase.LoadAssetAtPath <UnityEngine.U2D.SpriteAtlas>(file);

                        var textureNameMap = new Dictionary <string, bool>();
                        foreach (var pa in UnityEditor.U2D.SpriteAtlasExtensions.GetPackables(sa))
                        {
                            var assetPath = AssetDatabase.GetAssetPath(pa.GetInstanceID());
                            assetPath = assetPath.Replace(Defines.AssetBundleSourcePath + "/", null);
                            assetPath = Path.ChangeExtension(assetPath, null);

                            allPackable.Add(assetPath);
                        }

                        XManifest.SpritePack spritePack = new XManifest.SpritePack();
                        spritePack.name     = name;
                        spritePack.packable = allPackable.ToArray();
                        var realName = file.Replace(Defines.AssetBundleSourcePath + "/", null).ToLower().Replace("\\", "/");
                        if (System.IO.Path.HasExtension(realName))
                        {
                            realName = System.IO.Path.ChangeExtension(realName, null);
                        }

                        spritePack.realName = realName;

                        XManifest.Instance.Add(spritePack);
                    }
                }

                EditorUtility.DisplayProgressBar("Generate Manifest", "compute bundle hash", i / (float)bundles.Length);
            }

            // record luas
            List <string> luaFiles = new List <string>();
            GetAllFile(new DirectoryInfo(Defines.LuaByteCodeOutPath), luaFiles);
            foreach (string fileName in luaFiles)
            {
                string name = Path.GetFileNameWithoutExtension(fileName);
                if (!XManifest.Instance.Exists(name))
                {
                    XManifest.File file = new XManifest.File();
                    file.name     = name;
                    file.location = XManifest.Location.Streaming;
                    uint checksum = 0;
                    uint size     = 0;
                    GetFileInfo(fileName, out checksum, out size);
                    file.checksum = checksum;
                    file.size     = size;
                    XManifest.Instance.Add(file);
                }
                else
                {
                    Debug.LogError("[GenerateFileAllocationTable] repeat name:" + name);
                }
            }

            // gen rules
            BundleNameHelper.initPackRules();
            foreach (BundlePackRule rule in BundleNameHelper.rules)
            {
                XManifest.Instance.AddBundleRule(rule);
            }

            GameConfig.VersionInfo version = new GameConfig.VersionInfo(VersionCode);

            XManifestSave(manifestSavePath, true);
            Debug.Log("[GenerateFileAllocationTable] Mainfest 生成成功:" + manifestSavePath);
        }
        catch (System.Exception e)
        {
            Debug.LogException(e);
        }

        EditorUtility.DisplayProgressBar("Generate Manifest", "success!", 1.0f);

        bundle.Unload(true);
        EditorUtility.ClearProgressBar();

        AssetDatabase.Refresh();
    }
Пример #9
0
    private static IEnumerator _DoLoadAsync(string path, System.Type type, System.Action <Object> callback)
    {
#if UNITY_EDITOR && !USE_BUNDLE_IN_EDITOR
        ResourceRequest request = Resources.LoadAsync(Path.ChangeExtension(path, null), type);
        yield return(request);

        Object obj = request.asset;
        if (obj == null)
        {
            obj = _LoadObjInEditorAt(path, type);
        }
        callback(obj);
#else
        //Debug.LogFormat("[XLoader] AsyncLoading: ({0} : {1})", path, type.ToString());
        if (string.IsNullOrEmpty(path))
        {
            Debug.LogErrorFormat("[XLoader.Load] sent empty/null path!");
            yield break;
        }


        string name = Path.ChangeExtension(path, null);
        XManifest.AssetInfo info = XManifest.Instance.Find(name);
        UnityEngine.Object  obj  = null;
        if (info != null)
        {
            switch (info.location)
            {
            case XManifest.Location.Resource:
            {
                ResourceRequest request = Resources.LoadAsync(name, type);
                yield return(request);

                obj = request.asset;
            }
            break;

            case XManifest.Location.Bundle:
            {
                yield return(AssetBundleManager.Instance.LoadAssetAsync(info, type, delegate(Object result) { obj = result; }));
            }
            break;

            default:
                break;
            }
        }
        else
        {
            string bundleName = XManifest.Instance.GetABNameByPath(name);
            if (!string.IsNullOrEmpty(bundleName))
            {
                XManifest.Pack pack = XManifest.Instance.GetPack(bundleName);
                if (pack == null)
                {
                    Debug.LogErrorFormat("[XLoader.LoadAsync] can not find bundle pack: [{0}] for res [{1}]", bundleName, name);
                    callback(null);
                    yield break;
                }

                XManifest.Location  local     = pack.NoBundle() ? XManifest.Location.Resource : XManifest.Location.Bundle;
                XManifest.AssetInfo assetInfo = new XManifest.AssetInfo()
                {
                    location = local, bundle = pack.name, fullName = path
                };

                switch (local)
                {
                case XManifest.Location.Resource:
                {
                    ResourceRequest request = Resources.LoadAsync(name, type);
                    yield return(request);

                    obj = request.asset;
                }
                break;

                case XManifest.Location.Bundle:
                {
                    yield return(AssetBundleManager.Instance.LoadAssetAsync(assetInfo, type, delegate(Object result) { obj = result; }));
                }
                break;

                default:
                    break;
                }

                if (obj != null)
                {
                    XManifest.Instance.AddFastIndex(path, assetInfo);
                }
            }
            else
            {
                ResourceRequest request = Resources.LoadAsync(name, type);
                yield return(request);

                obj = request.asset;
            }
        }


        if (obj == null)
        {
            if (info != null)
            {
                Debug.LogErrorFormat("[XLoader] Can't find {0} in Location({1})", name, info.location);
            }
            else
            {
                Debug.LogErrorFormat("[XLoader] Can't find {0} in Resources", name);
            }
        }

        callback(obj);
#endif
    }
Пример #10
0
    public static Object Load(string path, System.Type type)
    {
        Debug.LogFormat("[XLoader] Load: ({0} : {1})", path, type.ToString());

        if (string.IsNullOrEmpty(path))
        {
            Debug.LogErrorFormat("[XLoader.Load] sent empty/null path!");
            return(null);
        }
        Object obj = null;

#if UNITY_EDITOR
        obj = Resources.Load(Path.ChangeExtension(path, null), type);

        if (obj == null)
        {
            obj = _LoadObjInEditorAt(path, type);
        }

        if (obj != null)
        {
            return(obj);
        }

        //return obj;
#endif
        //#else
        string name = Path.ChangeExtension(path, null);
        //UnityEngine.Object obj = null;

        //Debug.LogFormat("[XLoader] can't find Asset({0} : {1}) in cache: ", name, type.ToString());
        XManifest.AssetInfo info = XManifest.Instance.Find(name);
        if (info != null)
        {
            switch (info.location)
            {
            case XManifest.Location.Resource:
            {
                obj = Resources.Load(name, type);
            }
            break;

            case XManifest.Location.Bundle:
            {
                obj = AssetBundleManager.Instance.LoadAsset(info, type);
            }
            break;

            default:
                break;
            }
        }
        else
        {
            string bundleName = XManifest.Instance.GetABNameByPath(name);
            if (!string.IsNullOrEmpty(bundleName))
            {
                XManifest.Pack pack = XManifest.Instance.GetPack(bundleName);
                if (pack == null)
                {
                    Debug.LogErrorFormat("[XLoader.Load] can not find bundle pack: [{0}] for res [{1}]", bundleName, name);
                    return(null);
                }
                XManifest.Location  local     = pack.NoBundle() ? XManifest.Location.Resource : XManifest.Location.Bundle;
                XManifest.AssetInfo assetInfo = new XManifest.AssetInfo()
                {
                    location = local, bundle = pack.name, fullName = path
                };

                switch (local)
                {
                case XManifest.Location.Resource:
                {
                    obj = Resources.Load(name, type);
                }
                break;

                case XManifest.Location.Bundle:
                {
                    obj = AssetBundleManager.Instance.LoadAsset(assetInfo, type);
                }
                break;

                default:
                    break;
                }

                if (obj != null)
                {
                    XManifest.Instance.AddFastIndex(path, assetInfo);
                }
            }
            else
            {
                obj = Resources.Load(name, type);
            }
        }

        if (obj == null)
        {
            if (info != null)
            {
                Debug.LogErrorFormat("[XLoader] Can't find {0} in Location({1})", name, info.location);
            }
            else
            {
                Debug.LogErrorFormat("[XLoader] Can't find {0} in Resources", name);
            }
        }
        else
        {
            Debug.LogFormat("[XLoader] ({0} : {1}) Loaded.", name, type.ToString());
        }

        return(obj);
//#endif
    }