예제 #1
0
    public T LoadAsset <T>(string fileName) where T : Object
    {
        //首先 判断 依赖中有没有该资源
        //1.没有则LoadLoad
        //2.有的话,则判断本地是否加载过
        //2.1.加载过则用本地
        //2.2 没有加载过则进行LoadBundle在添加到本地LoadedBundle
        string newName = fileName.ToLower() + ".ab";

        if (IsManifestContains(newName)) //判断Manifest中是否存在 bundleName
        {
            //判断是否已经加载过
            AssetBundle ab = null;
            if (IsLoadedBundle(newName))
            {
                ab = GetBundleByName(newName);
            }
            else
            {
                string path = DataUrl.GetFilePersistentUrl(newName);
                ab = AssetBundle.LoadFromFile(path); //注意这边名称是否正确,注意测试
                //加载依赖
                LoadDesBundle(newName);
                AddLoadedBundle(newName, ab);
            }
            return(ab.LoadAsset <T>(string.Format("Assets/Resources/{0}.{1}", fileName, GetSufixx(newName))));
        }
        else
        {
            return(null);
        }
    }
예제 #2
0
    public TextAsset LoadLuaAB(string fileName)
    {
        string ABName = "luascripts.ab";

        if (IsManifestContains(ABName)) //判断Manifest中是否存在 bundleName
        {
            //判断是否已经加载过
            AssetBundle ab = null;
            if (IsLoadedBundle(ABName))
            {
                ab = GetBundleByName(ABName);
            }
            else
            {
                string path = DataUrl.GetFilePersistentUrl(ABName);
                ab = AssetBundle.LoadFromFile(path); //注意这边名称是否正确,注意测试
                //加载依赖
                AddLoadedBundle(ABName, ab);
            }
            return(ab.LoadAsset <TextAsset>(string.Format("Assets/Resources/{0}.{1}", fileName, "txt")));
        }
        else
        {
            return(null);
        }
    }
예제 #3
0
    IEnumerator Load(string[] bundle_names, UnityAction callFunc, UnityAction <int> progressFunc = null)
    {
        int len = bundle_names.Length;

        for (int i = 0; i < len; i++)
        {
            string name = bundle_names[i];
            if (!loadedBundles.ContainsKey(name))
            {
                AssetBundle ab = AssetBundle.LoadFromFile(DataUrl.GetFilePersistentUrl(name));
                loadedBundles.Add(name, ab);
                yield return(null);

                if (progressFunc != null)
                {
                    progressFunc.Invoke(i + 1);
                }
            }

            if (i == len - 1)
            {
                callFunc();
            }
        }
    }
예제 #4
0
    public override T LoadAsset <T>(string fileName)
    {
        string[] files = fileName.Split('/');
        fileName = files[files.Length - 1];

        BundleInfo info = null;

        fileMap.TryGetValue(fileName, out info);
        if (null == info)
        {
            return(null);
        }

        string path = DataUrl.GetFilePersistentUrl(info.bundleName);
        bool   isPersistentDataPath = System.IO.File.Exists(path);

        if (!isPersistentDataPath)
        {
            path = DataUrl.GetFileStreamingUrl(info.bundleName);
            if (!System.IO.File.Exists(path))
            {
                return(null);
            }
        }

        AssetBundle asset = null;

        loadedBundleDic.TryGetValue(path, out asset);
        if (asset == null)
        {
            asset = AssetBundle.LoadFromFile(path);
            loadedBundleDic.Add(path, asset);
        }

        if (asset == null)
        {
            return(null);
        }

        if (null != info.dependencies)
        {
            foreach (string dep in info.dependencies)
            {
                AssetBundle _asset     = null;
                string      dependPath = isPersistentDataPath ? DataUrl.GetFilePersistentUrl(dep) : DataUrl.GetFileStreamingUrl(dep);
                if (IsHadLoadDeps(dependPath))
                {
                    continue;
                }
                _asset = AssetBundle.LoadFromFile(dependPath);
                depsDic.Add(dependPath, _asset);
            }
        }

        return(asset.LoadAsset <T>(info.assetName));
    }
예제 #5
0
    //暂时不采用异步加载,
    public override T LoadAssetAsync <T>(string fileName)
    {
        //这边不知道,在路径错误的情况下isDone是啥样的,
        //可以判断request.assetBundle为空的情况下,就继续异步加载.(暂时先不弄,没需求)
        string path = DataUrl.GetFilePersistentUrl(fileName);
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

        if (request.isDone)
        {
            //这边异步加载Streaming目录即可。
            return(request.assetBundle.LoadAsset <T>(fileName));
        }
        return(null);
    }
예제 #6
0
    string GetScenePath(string fileName)
    {
        string path = DataUrl.GetFilePersistentUrl(fileName) + ".unity3d";
        // string path= DataUrl.LOCAL_URL_PREFIX + Application.dataPath + "/StreamingAssets/" + "Scene_Main" + ".unity3d";
        //读取Per目录的时候不需要加prefix,但是读取Streaming目录时候需加上prefix
        bool isPersistentDataPath = System.IO.File.Exists(path);

        if (!isPersistentDataPath)
        {
            path = DataUrl.GetFileStreamingUrl(fileName) + ".unity3d";
            return(path);
        }

        return(DataUrl.LOCAL_URL_PREFIX + path);
    }
예제 #7
0
    //void LoadAtlas()
    //{
    //    string[] atlsPaths = { "MainAtlas.unity3d", "PlayingAtlas.unity3d", "cardAtlas.unity3d" };
    //    for (int i = 0; i < atlsPaths.Length; i++)
    //    {
    //        AssetBundle asset = null;
    //        string path = atlsPaths[i];
    //        //bool isPersistentDataPath = System.IO.File.Exists(path);
    //        //if (!isPersistentDataPath)
    //        //{
    //        //    path = DataUrl.GetFileStreamingUrl(info.bundleName);
    //        //    if (!System.IO.File.Exists(path))
    //        //        AssetBundle asset = null;
    //        loadedBundleDic.TryGetValue(path, out asset);
    //        if (asset == null)
    //        {
    //            path =DataUrl.GetFilePersistentUrl(atlsPaths[i]);
    //            asset = AssetBundle.LoadFromFile(path);
    //            if(asset == null)
    //            {
    //                path = DataUrl.GetFileStreamingUrl(atlsPaths[i]);
    //                asset = AssetBundle.LoadFromFile(path);
    //                if(asset == null)
    //                {

    //                }
    //                else
    //                {
    //                    loadedBundleDic.Add(atlsPaths[i], asset);
    //                }
    //            }
    //            else
    //            {
    //                loadedBundleDic.Add(atlsPaths[i], asset);
    //            }
    //        }
    //        else
    //        {
    //            loadedBundleDic.Add(atlsPaths[i], asset);
    //        }
    //    }
    //}
    /// <summary>
    /// StreamingAssets.xml 初始化
    /// </summary>
    void InitFileMap()
    {
        string fileName         = GlobalData.mStreamingAssetsXml;
        string path             = DataUrl.GetFilePersistentUrl(fileName);
        bool   isPersistentPath = System.IO.File.Exists(path);

        Debug.Log("isPersistentPath:" + isPersistentPath.ToString());
        if (!isPersistentPath)
        {
            path = DataUrl.GetFileStreamingUrl(fileName);
            //安卓读取该目录有错误,所以,只能读取Per目录中的数据。
            if (!System.IO.File.Exists(path))
            {
                //Debug.LogError(fileName + "文件加载失败...");
                return;
            }
        }

        XmlDocument doc = new XmlDocument();

        doc.Load(path);
        Debug.Log(fileName + doc.Name);
        XmlNodeList nodeList = doc.SelectSingleNode("files").ChildNodes;

        foreach (XmlElement xe in nodeList)
        {
            BundleInfo info      = new BundleInfo();
            string     _fileName = xe.SelectSingleNode("fileName").InnerText.Replace("\\", "/");
            info.assetName  = xe.SelectSingleNode("assetName").InnerText.Replace("\\", "/");
            info.bundleName = xe.SelectSingleNode("bundleName").InnerText.Replace("\\", "/");

            XmlNode deps = xe.SelectSingleNode("deps");
            if (null != deps)
            {
                XmlNodeList depList = deps.ChildNodes;
                foreach (XmlElement _xe in depList)
                {
                    info.AddDependence(_xe.InnerText.Replace("\\", "/"));
                }
            }
            string[] strs     = _fileName.Substring("Assets/Resources/".Length).Split('.');
            string   strValue = strs[0];
            fileMap.Add(strValue, info);
        }
    }
예제 #8
0
    IEnumerator ReadBundleManifest()
    {
        var abAsy = AssetBundle.LoadFromFileAsync(DataUrl.GetFilePersistentUrl("StreamingAssets"));

        yield return(abAsy);

        if (abAsy.assetBundle != null)
        {
            mainfest    = abAsy.assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
            bundleNames = mainfest.GetAllAssetBundles();
            SplitInitOrMainBundle();
            LoadInitBundles(initBundleNames.ToArray(), () =>
            {
                Debug.Log("Bundle加载完成...bundles Count:" + loadedBundles.Count);
                XLuaInstance.Instance.HotStartFixing();
                GameManager.Instance.SetRenderTexture();
                GameManager.Instance.SwitchGameStatus(EGameStatus.ELogin);
            }, (a) => Debug.Log("进度:" + a.ToString()));
        }
    }
예제 #9
0
    void LoadFromFileAsyncAB(string name)
    {
        AssetBundle ab = AssetBundle.LoadFromFile(DataUrl.GetFilePersistentUrl(name));

        AddLoadedBundle(name, ab);
    }