示例#1
0
        public static IEnumerator InitCoreBundle()
        {
            if (AppEnv.IsDevMode)
            {
                string coreBundlePath = GetCoreBundlePath();
                WWW    www            = new WWW(coreBundlePath);
                yield return(www);

                if (string.IsNullOrEmpty(www.error))
                {
                    // 解密代码
                    byte[] sourcedata    = www.bytes;
                    float  before_msTime = Time.realtimeSinceStartup * 1000;
                    byte[] result        = EnAndDecode.EncodeAndDecode.Decode(sourcedata);
                    float  after_msTime  = Time.realtimeSinceStartup * 1000;
                    Debug.Log("Decode cast time is : " + (after_msTime - before_msTime).ToString());
                    // 从内存中创建 AssetBundle
                    Debug.Log("start loadAssetBundle for Memory");
                    _CoreBundle = AssetBundle.LoadFromMemory(result);
                    Debug.LogFormat("InitCoreBundle Success Path={0} Count={1}", coreBundlePath, _CoreBundle.GetAllAssetNames().GetLength(0));
                }
                else
                {
                    Debug.LogErrorFormat("InitCoreBundle Error={0} Path={1}", www.error, coreBundlePath);
                }
            }
            else
            {
                string coreBundlePath = GetCoreBundlePath();
                Debug.LogFormat("Begin InitCoreBundle Path={0}", coreBundlePath);
                //AssetBundleCreateRequest bundleLoadRequest = null;
                //try
                //{
                //    ABFileStream fileStream = new ABFileStream(coreBundlePath, FileMode.Open, FileAccess.Read);
                //    fileStream.ReadTimeout = 100;
                //    if (fileStream.CanRead)
                //    {
                //        bundleLoadRequest = AssetBundle.LoadFromStreamAsync(fileStream);
                //    }
                //    else
                //    {
                //        Debug.LogFormat("core can not be read {0}", fileStream);
                //    }
                //}
                //catch(Exception _e)
                //{
                //    Debug.LogFormat("LoadFromStreamAsync exception {0}", _e.StackTrace);
                //}

                //while (bundleLoadRequest != null && !bundleLoadRequest.isDone)
                //{
                //    yield return null;    // 协程等待
                //}

                //if (bundleLoadRequest != null)
                //{
                //    _CoreBundle = bundleLoadRequest.assetBundle;
                //    if (_CoreBundle != null)
                //    {
                //        Debug.LogFormat("InitCoreBundle Success Path={0} Count={1}", coreBundlePath, _CoreBundle.GetAllAssetNames().GetLength(0));
                //    }
                //    else{
                //        Debug.LogError("!!!!!!!!!!!!!!!!!!!!!_CoreBundle is null!!!!!!!!!!!!!!!!!!!!!");
                //    }
                //}
                try
                {
                    ABFileStream fileStream = new ABFileStream(coreBundlePath, FileMode.Open, FileAccess.Read);
                    _CoreBundle = AssetBundle.LoadFromStream(fileStream);
                    if (_CoreBundle != null)
                    {
                        Debug.LogFormat("InitCoreBundle Success Path={0} Count={1}", coreBundlePath, _CoreBundle.GetAllAssetNames().GetLength(0));
                    }
                    else
                    {
                        Debug.LogError("!!!!!!!!!!!!!!!!!!!!!_CoreBundle is null!!!!!!!!!!!!!!!!!!!!!");
                    }
                }
                catch (Exception _e)
                {
                    Debug.LogFormat("LoadFromStream exception {0}", _e.StackTrace);
                }
            }
        }
示例#2
0
        public static IEnumerator InitGameBundle(string gameName)
        {
            if (AppEnv.IsDevMode)
            {
                string gameMainBundlePath = GetGameMainBundlePath(gameName);
                WWW    www = new WWW(gameMainBundlePath);
                yield return(www);

                if (string.IsNullOrEmpty(www.error))
                {
                    // 解密代码
                    byte[] sourcedata    = www.bytes;
                    float  beforGameTime = Time.realtimeSinceStartup * 1000;
                    byte[] result        = EnAndDecode.EncodeAndDecode.Decode(sourcedata);
                    float  afterGameTime = Time.realtimeSinceStartup * 1000;
                    Debug.Log("Decode the game assetBundle cast time " + (afterGameTime - beforGameTime).ToString());
                    _GameMainBundle = AssetBundle.LoadFromMemory(result);
                    Debug.LogFormat("InitGameMainBundle Success Path={0} Count={1}", gameMainBundlePath, _GameMainBundle.GetAllAssetNames().GetLength(0));
                }
                else
                {
                    Debug.LogErrorFormat("InitGameMainBundle Error={0} Path={1}", www.error, gameMainBundlePath);
                }

                string gameSceneBundlePath = GetGameSceneBundlePath(gameName);
                WWW    www2 = new WWW(gameSceneBundlePath);
                yield return(www2);

                if (string.IsNullOrEmpty(www2.error))
                {
                    // 解密 scene Assetbundle
                    byte[] sceneSourceData = www2.bytes;
                    float  bSceneTime      = Time.realtimeSinceStartup * 1000;
                    byte[] sceneResult     = EnAndDecode.EncodeAndDecode.Decode(sceneSourceData);
                    float  aSceneTime      = Time.realtimeSinceStartup * 1000;
                    Debug.Log("Decode the scene assetBundle cast time : " + (aSceneTime - bSceneTime).ToString());
                    _GameSceneBundle = AssetBundle.LoadFromMemory(sceneResult);
                    Debug.LogFormat("InitGameSceneBundle Success Path={0} Count={1}", gameSceneBundlePath, _GameSceneBundle.GetAllAssetNames().GetLength(0));
                }
                else
                {
                    Debug.LogErrorFormat("InitGameSceneBundle Error={0} Path={1}", www2.error, gameSceneBundlePath);
                }
            }
            else
            {
                string                   gameMainBundlePath    = GetGameMainBundlePath(gameName);
                ABFileStream             fileStreamMain        = new ABFileStream(gameMainBundlePath, FileMode.Open, FileAccess.Read);
                AssetBundleCreateRequest bundleLoadRequestMain = AssetBundle.LoadFromStreamAsync(fileStreamMain);
                while (!bundleLoadRequestMain.isDone)
                {
                    yield return(null);    // 协程等待
                }

                _GameMainBundle = bundleLoadRequestMain.assetBundle;
                Debug.LogFormat("InitCoreBundle Success Path={0} Count={1}", gameMainBundlePath, _GameMainBundle.GetAllAssetNames().GetLength(0));

                string                   gameSceneBundlePath    = GetGameSceneBundlePath(gameName);
                ABFileStream             fileStreamScene        = new ABFileStream(gameSceneBundlePath, FileMode.Open, FileAccess.Read);
                AssetBundleCreateRequest bundleLoadRequestScene = AssetBundle.LoadFromStreamAsync(fileStreamScene);
                while (!bundleLoadRequestScene.isDone)
                {
                    yield return(null);    // 协程等待
                }

                _GameSceneBundle = bundleLoadRequestScene.assetBundle;
                Debug.LogFormat("InitCoreBundle Success Path={0} Count={1}", gameSceneBundlePath, _GameSceneBundle.GetAllAssetNames().GetLength(0));
            }
        }