Beispiel #1
0
        public static byte[] GetUrlByte(string url)
        {
            C_DebugHelper.Log("GetUrlByte url = " + url);

            byte[] bytes = null;

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                request.SendWebRequest();

                while (!request.isDone)
                {
                    if (request.isHttpError || request.isNetworkError)
                    {
                        C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                        return(bytes);
                    }
                }

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    bytes = request.downloadHandler.data;
                }
            }

            return(bytes);
        }
Beispiel #2
0
        public void AddShadowCaster(GameObject gOParam)
        {
            if (gOParam == null)
            {
                C_DebugHelper.LogError("C_ShadowProjector AddShadowCaster: gOParam is Null!");
                return;
            }

            gOParam.layer = LayerMask.NameToLayer("ShadowCaster");
            foreach (Transform child in gOParam.transform)
            {
                child.gameObject.layer = LayerMask.NameToLayer("ShadowCaster");
            }


            foreach (GameObject gO in m_ShadowCasterList)
            {
                if (gO == gOParam)
                {
                    return;
                }
            }

            m_ShadowCasterList.Add(gOParam);
        }
Beispiel #3
0
        // 将用完的GameObject放入m_DormantObjects中
        public void Despawn(Object objectParam, C_PoolChannel channel = C_PoolChannel.Global)
        {
            if (objectParam == null || channel == C_PoolChannel.None)
            {
                C_DebugHelper.LogError("C_PoolMgr Despawn: Object is Null!");
                return;
            }

            if (string.IsNullOrEmpty(objectParam.name))
            {
                C_DebugHelper.LogError("C_PoolMgr Despawn: Object name is Null or Empty!");
                return;
            }

            // 添加通道
            if (!m_PoolHandlerDict.ContainsKey(channel))
            {
                m_PoolHandlerDict[channel] = new List <Object>();
            }

            if (!m_PoolHandlerDict[channel].Contains(objectParam))
            {
                m_PoolHandlerDict[channel].Add(objectParam);
                //Debug.LogError("添加 对象池");
            }

            while (m_PoolHandlerDict[channel].Count > m_Capacity)
            {
                Object dob = m_PoolHandlerDict[channel][0];
                m_PoolHandlerDict[channel].RemoveAt(0);
                Destroy(dob);
            }
        }
Beispiel #4
0
        public static void SyncDownloadFile(string url, string savePath)
        {
            C_DebugHelper.Log("SyncDownloadFile 下载开始 url = " + url + ", savePath = " + savePath);

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                request.SendWebRequest();

                while (!request.isDone)
                {
                    if (request.isHttpError || request.isNetworkError)
                    {
                        C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                        return;
                    }
                }

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    C_DownloadMgr.CreatFile(url, savePath, request.downloadHandler.data);
                }
            }
        }
Beispiel #5
0
        private static Byte[] ToByteArray(UInt32[] data, Boolean includeLength)
        {
            if (data == null)
            {
                return(null);
            }

            try
            {
                Int32 n;
                if (includeLength)
                {
                    n = (Int32)data[data.Length - 1];
                }
                else
                {
                    n = data.Length << 2;
                }

                Byte[] result = new Byte[n];
                for (Int32 i = 0; i < n; i++)
                {
                    result[i] = (Byte)(data[i >> 2] >> ((i & 3) << 3));
                }

                return(result);
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("C_XXTea ToByteArray e.Message = " + e.Message);
            }

            return(null);
        }
Beispiel #6
0
        public static T GetAssetBundleFormCache <T>(string resName, string assetBundleFilePath, bool isInstantiate = false, bool isForever = false) where T : UnityEngine.Object
        {
            T result = null;

            try
            {
                C_AssetBundleRef bundle = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().GetAssetBundleRefList(assetBundleFilePath);

                if (bundle != null)
                {
                    result = bundle.LoadAsset <T>(resName);

                    if (isInstantiate && result != null)
                    {
                        result = GameObject.Instantiate(result);
                    }
                }
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("GetAssetBundleFormCache : " + e);
            }

            return(result);
        }
Beispiel #7
0
        private IEnumerator Execute(string url, string savePath, Action <byte[]> callback)
        {
            C_DebugHelper.Log("AsyncDownloadFile 下载开始 url = " + url + ", savePath = " + savePath);

            //System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            //stopWatch.Start();

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                yield return(request.SendWebRequest());

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    byte[] bytes = request.downloadHandler.data;

                    if (callback != null)
                    {
                        callback(bytes);
                    }

                    C_DownloadMgr.CreatFile(url, savePath, bytes);
                }

                C_DebugHelper.Log("下载完成");

                //stopWatch.Stop();
                // C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds);
            }
        }
Beispiel #8
0
 /// <summary>
 /// 添加延迟事件方法
 /// </summary>
 /// <param name="time">延迟时间</param>
 /// <param name="timeUpAction">方法</param>
 /// <param name="name">方法名</param>
 /// <param name="loop">是否循环?</param>
 public void AddTimer(float time, Action timeUpAction, string name, int loop = 1)
 {
     if (timeUpAction == null)
     {
         C_DebugHelper.LogError("C_TimerMgr AddTimer timeUpAction is null!");
     }
     m_TimerList.Add(new C_Timer(time, timeUpAction, name, loop));
 }
Beispiel #9
0
 private void DeleteCacheFiles()
 {
     for (int i = 0; i < ThreadNum; i++)
     {
         FileInfo info = new FileInfo(FileNames[i]);
         C_DebugHelper.LogFormat("Delete File {0} OK!", FileNames[i]);
         info.Delete();
     }
 }
Beispiel #10
0
        IEnumerator MergeFile(string url, string savePath, Action callback)
        {
            while (true)
            {
                IsMerge = true;

                for (int i = 0; i < ThreadNum; i++)
                {
                    if (ThreadStatus[i] == false)
                    {
                        IsMerge = false;

                        yield return(0);

                        System.Threading.Thread.Sleep(100);
                        break;
                    }
                }

                if (IsMerge)
                {
                    break;
                }
            }
            byte[] bytes = new byte[bufferSize];

            FileStream fs     = new FileStream(C_DownloadMgr.StandardDownloadSavePath(url, savePath), FileMode.OpenOrCreate);
            FileStream fsTemp = null;

            for (int i = 0; i < ThreadNum; i++)
            {
                fsTemp = new FileStream(FileNames[i], FileMode.OpenOrCreate);

                int readBytes;
                while ((readBytes = fsTemp.Read(bytes, 0, bytes.Length)) > 0)
                {
                    fs.Write(bytes, 0, readBytes);
                }

                fsTemp.Close();
            }

            fs.Close();

            if (callback != null)
            {
                callback();
            }

            m_StopWatch.Stop();
            C_DebugHelper.Log("下载完成,耗时: " + m_StopWatch.ElapsedMilliseconds);

            yield return(null);

            DeleteCacheFiles();
        }
Beispiel #11
0
        public int AddTimer(float time, Action timeUpAction, int loop = 1)
        {
            if (timeUpAction == null)
            {
                C_DebugHelper.LogError("C_TimerMgr AddTimer timeUpAction is null!");
                return(-1);
            }

            m_nTimerSequence++;

            m_TimerList.Add(new C_Timer(time, timeUpAction, m_nTimerSequence, loop));

            return(m_nTimerSequence);
        }
Beispiel #12
0
        public static T LoadAssetBundle <T>(string resName, string assetBundleFilePath, List <string> dpsList, bool isInstantiate = false, bool isForever = false) where T : UnityEngine.Object
        {
            if (string.IsNullOrEmpty(resName) || string.IsNullOrEmpty(assetBundleFilePath))
            {
                return(null);
            }

            T result = null;

            try
            {
                C_AssetBundleRef[] abs = new C_AssetBundleRef[dpsList.Count];
                for (int i = 0; i < abs.Length; i++)
                {
                    C_DebugHelper.LogFormat("LoadResource abs[{0}]: {1}", i, dpsList[i]);

                    abs[i] = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().LoadFromFile(dpsList[i], isForever);
                }

                C_AssetBundleRef bundle = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().LoadFromFile(assetBundleFilePath, isForever);

                if (bundle != null)
                {
                    result = bundle.LoadAsset <T>(resName);

                    if (isInstantiate && result != null)
                    {
                        result = GameObject.Instantiate(result);
                    }

                    bundle.AutoUnload();
                }

                for (int i = 0; i < abs.Length; i++)
                {
                    if (abs[i] != null)
                    {
                        abs[i].AutoUnload();
                    }
                }
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("LoadAssetBundle : " + e);
            }

            return(result);
        }
Beispiel #13
0
        public static UInt32[] Decrypt(UInt32[] v, UInt32[] k)
        {
            if (v == null || k == null)
            {
                return(null);
            }

            try
            {
                Int32 n = v.Length - 1;
                if (n < 1)
                {
                    return(v);
                }

                if (k.Length < 4)
                {
                    UInt32[] key = new UInt32[4];
                    k.CopyTo(key, 0);
                    k = key;
                }

                UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum, e;
                Int32  p, q = 6 + 52 / (n + 1);
                sum = unchecked ((UInt32)(q * delta));
                while (sum != 0)
                {
                    e = sum >> 2 & 3;
                    for (p = n; p > 0; p--)
                    {
                        z = v[p - 1];
                        y = unchecked (v[p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                    }
                    z   = v[n];
                    y   = unchecked (v[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                    sum = unchecked (sum - delta);
                }

                return(v);
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("C_XXTea Decrypt e.Message = " + e.Message);
            }

            return(null);
        }
Beispiel #14
0
        public static UInt32[] Encrypt(UInt32[] v, UInt32[] k)
        {
            if (v == null || k == null)
            {
                return(null);
            }

            try
            {
                Int32 n = v.Length - 1;
                if (n < 1)
                {
                    return(v);
                }

                if (k.Length < 4)
                {
                    UInt32[] key = new UInt32[4];
                    k.CopyTo(key, 0);
                    k = key;
                }

                UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum = 0, e;
                Int32  p, q = 6 + 52 / (n + 1);
                while (q-- > 0)
                {
                    sum = unchecked (sum + delta);
                    e   = sum >> 2 & 3;
                    for (p = 0; p < n; p++)
                    {
                        y = v[p + 1];
                        z = unchecked (v[p] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                    }
                    y = v[0];
                    z = unchecked (v[n] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
                }

                return(v);
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("C_XXTea Encrypt e.Message = " + e.Message);
            }

            return(null);
        }
Beispiel #15
0
        void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
        {
#if !C_Framework
            if (string.IsNullOrEmpty(_MainActiveSceneName))
            {
                SceneManager.sceneLoaded -= OnSceneLoaded;
                _LoadOver = true;
            }

            if (!string.IsNullOrEmpty(_MainActiveSceneName) && scene != null && scene.name.Equals(_MainActiveSceneName))
            {
                Utility.SetMainScene(_MainActiveSceneName);
                _LoadOver = true;
                SceneManager.sceneLoaded -= OnSceneLoaded;
            }

            C_DebugHelper.Log(" OnSceneLoaded" + scene.name);
#endif
        }
Beispiel #16
0
        private GameObject ReallyOpenUI(string uiName, string uiPath, bool isCloseOthers, params object[] uiParams)
        {
            if (string.IsNullOrEmpty(uiName))
            {
                C_DebugHelper.LogError("C_UIMgr ReallyOpenUI uiName is null or empty!");
                return(null);
            }

            if (isCloseOthers)
            {
                CloseUIAll();
            }

            GameObject UIObject = GetUI(uiName);

            if (UIObject != null)
            {
                C_BaseUI listBaseUI = UIObject.gameObject.GetComponent <C_BaseUI>();
                if (listBaseUI != null)
                {
                    listBaseUI.OpenUI(uiParams);
                }

                return(UIObject);
            }

            UIObject = C_Singleton <GameResMgr> .GetInstance().LoadResource_UI(uiName, uiPath);

            if (UIObject != null)
            {
                UIObject.name = uiName;
                m_OpenedUIsList.Add(UIObject);

                C_BaseUI baseUI = UIObject.GetComponent <C_BaseUI>();
                if (baseUI != null)
                {
                    baseUI.OpenUI(uiParams);
                }
            }

            return(UIObject);
        }
Beispiel #17
0
        public void RegisterState(string name, C_IState state)
        {
            if (string.IsNullOrEmpty(name))
            {
                C_DebugHelper.LogError("C_StateMachine RegisterState name is null or empty!");
                return;
            }

            if (state == null)
            {
                C_DebugHelper.LogError("C_StateMachine RegisterState state is null!");
                return;
            }

            if (m_RegistedState.ContainsKey(name))
            {
                C_DebugHelper.LogError("C_StateMachine RegisterState name = " + name + " is covered!");
            }

            m_RegistedState[name] = state;
        }
Beispiel #18
0
        public void RemoveShadowCaster(GameObject gOParam)
        {
            if (gOParam == null)
            {
                C_DebugHelper.LogError("C_ShadowProjector AddShadowCaster: gOParam is Null!");
                return;
            }

            if (gOParam.layer == LayerMask.NameToLayer("ShadowCaster"))
            {
                C_DebugHelper.LogError("C_ShadowProjector AddShadowCaster: gOParam layer != ShadowCaster");
                return;
            }

            for (int i = m_ShadowCasterList.Count - 1; i >= 0; i--)
            {
                if (m_ShadowCasterList[i] == gOParam)
                {
                    m_ShadowCasterList.RemoveAt(i);
                }
            }
        }
Beispiel #19
0
        //销毁同样名字的对象
        public void Destory(string resName, C_PoolChannel channel = C_PoolChannel.Global)
        {
            if (string.IsNullOrEmpty(resName) || channel == C_PoolChannel.None)
            {
                C_DebugHelper.LogError("C_PoolMgr Destory: resName is Null or Empty!");
                return;
            }

            resName = StandardResName(resName);

            if (m_PoolHandlerDict.ContainsKey(channel))
            {
                for (int i = m_PoolHandlerDict[channel].Count - 1; i >= 0; i++)
                {
                    if (m_PoolHandlerDict[channel][i] != null && m_PoolHandlerDict[channel][i].name == resName)
                    {
                        Object go = m_PoolHandlerDict[channel][i];
                        m_PoolHandlerDict[channel].RemoveAt(i);
                        Destroy(go);
                    }
                }
            }
        }
Beispiel #20
0
        public JsonData this[string prop_name] {
            get {
                EnsureDictionary();
                if (inst_object[prop_name] == null)
                {
                    C_DebugHelper.LogError(" assert ");
                }
                return(inst_object[prop_name]);
            }

            set {
                EnsureDictionary();

                KeyValuePair <string, JsonData> entry =
                    new KeyValuePair <string, JsonData> (prop_name, value);

                if (inst_object.ContainsKey(prop_name))
                {
                    for (int i = 0; i < object_list.Count; i++)
                    {
                        if (object_list[i].Key == prop_name)
                        {
                            object_list[i] = entry;
                            break;
                        }
                    }
                }
                else
                {
                    object_list.Add(entry);
                }

                inst_object[prop_name] = value;

                json = null;
            }
        }
Beispiel #21
0
        private static UInt32[] ToUInt32Array(Byte[] data, Boolean includeLength)
        {
            if (data == null)
            {
                return(null);
            }

            try
            {
                Int32    n = (((data.Length & 3) == 0) ? (data.Length >> 2) : ((data.Length >> 2) + 1));
                UInt32[] result;

                if (includeLength)
                {
                    result    = new UInt32[n + 1];
                    result[n] = (UInt32)data.Length;
                }
                else
                {
                    result = new UInt32[n];
                }

                n = data.Length;
                for (Int32 i = 0; i < n; i++)
                {
                    result[i >> 2] |= (UInt32)data[i] << ((i & 3) << 3);
                }

                return(result);
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("C_XXTea ToUInt32Array e.Message = " + e.Message);
            }

            return(null);
        }
Beispiel #22
0
        void Update()
        {
            try
            {
                //强制卸载
                if (m_ForceDirtyAssetBundleRefList.Count > 0)
                {
                    for (int i = 0; i < m_ForceDirtyAssetBundleRefList.Count; i++)
                    {
                        // Debug.Log("--ab name:" + m_ForceDirtyAssetBundleRefList[i].Bundle.name + "start unload.........");
                        if (m_ForceDirtyAssetBundleRefList[i].Bundle != null)
                        {
                            m_ForceDirtyAssetBundleRefList[i].Bundle.Unload(true);
                        }
                    }

                    m_ForceDirtyAssetBundleRefList.Clear();
                }

                if (m_UnloadAssetBundleRefList.Count > 0)
                {
                    for (int i = 0; i < m_UnloadAssetBundleRefList.Count; i++)
                    {
                        if (m_UnloadAssetBundleRefList[i].Bundle != null)
                        {
                            m_UnloadAssetBundleRefList[i].Bundle.Unload(false);
                        }
                    }

                    m_UnloadAssetBundleRefList.Clear();

                    //黄志龙,后续修改到loading界面去释放
                    Resources.UnloadUnusedAssets();
                }

                for (int i = m_DirtyAssetBundleRefList.Count - 1; i >= 0; i--)
                {
                    if (m_DirtyAssetBundleRefList[i] == null || m_DirtyAssetBundleRefList[i].Bundle == null)
                    {
                        m_DirtyAssetBundleRefList.RemoveAt(i);
                    }
                    else
                    {
                        m_UnloadAssetBundleRefList.Add(m_DirtyAssetBundleRefList[i]);

                        m_DirtyAssetBundleRefList.RemoveAt(i);
                    }
                }
                if (m_NeedLoadCacheDict.Count > 0)
                {
                    List <string> keyList = new List <string>(m_NeedLoadCacheDict.Keys);

                    for (int i = m_NeedLoadCacheDict.Count - 1; i >= 0; i--)
                    {
                        List <string> valueList = m_NeedLoadCacheDict[keyList[i]];

                        if (valueList.Count > 4)
                        {
                            LoadFromFile(valueList[valueList.Count - 1], true);
                            LoadFromFile(valueList[valueList.Count - 2], true);
                            LoadFromFile(valueList[valueList.Count - 3], true);
                            LoadFromFile(valueList[valueList.Count - 4], true);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else if (valueList.Count > 0)
                        {
                            C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + valueList[valueList.Count - 1] + "start");

                            LoadFromFile(valueList[valueList.Count - 1], true);
                            C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + valueList[valueList.Count - 1] + "end");

                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else
                        {
                            m_NeedLoadCacheDict.Remove(keyList[i]);
                        }
                    }
                }



                if (m_UnLoadCacheDict.Count > 0)
                {
                    List <string> keyList = new List <string>(m_UnLoadCacheDict.Keys);

                    for (int i = m_UnLoadCacheDict.Count - 1; i >= 0; i--)
                    {
                        List <string> valueList = m_UnLoadCacheDict[keyList[i]];

                        /* if (valueList.Count > 4)
                         * {
                         *  C_AssetBundleRef abr = GetAssetBundleRefList(valueList[valueList.Count - 1]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 2]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 3]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 4]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 2);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 3);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 4);
                         *
                         * }  else*/if (valueList.Count > 0)
                        {
                            C_AssetBundleRef abr = GetAssetBundleRefList(valueList[valueList.Count - 1]);
                            if (abr != null)
                            {
                                abr.CacheUnload();
                                C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + abr.Bundle.name);
                            }

                            m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else
                        {
                            m_UnLoadCacheDict.Remove(keyList[i]);
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                C_DebugHelper.LogError("NeedLoadCache error is :" + e);
            }
        }
Beispiel #23
0
        private const int m_nTimeOutWait      = 5 * 1000; //超时等待时间

        public void DownloadFile(string url, string savePath, Action callback, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal)
        {
            C_DebugHelper.Log("C_HttpDownloader DownloadFile url = " + url);

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

            Progress           = 0;
            DownloadFileLength = 0;
            m_bIsStop          = false;

            m_Thread = new Thread(delegate()
            {
                stopWatch.Start();

                //判断保存路径是否存在
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }

                string filePath = C_DownloadMgr.StandardDownloadSavePath(url, savePath);
                //string fileName = C_DownloadMgr.StandardDownloadName(url);

                //使用流操作文件
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);

                //获取文件现在的长度
                DownloadFileLength = fs.Length;

                //获取下载文件的总长度
                long totalLength = C_DownloadMgr.GetLength(url);
                //Debug.LogFormat("<color=red>文件:{0} 已下载{1}M,剩余{2}M</color>", fileName, fileLength / 1024 / 1024, (totalLength - fileLength) / 1024 / 1024);

                //如果没下载完
                if (DownloadFileLength < totalLength)
                {
                    //断点续传核心,设置本地文件流的起始位置
                    fs.Seek(DownloadFileLength, SeekOrigin.Begin);

                    HttpWebRequest request = C_DownloadMgr.GetWebRequest(url);

                    request.ReadWriteTimeout = m_nReadWriteTimeOut;
                    request.Timeout          = m_nTimeOutWait;

                    //断点续传核心,设置远程访问文件流的起始位置
                    request.AddRange((int)DownloadFileLength);

                    Stream stream = request.GetResponse().GetResponseStream();
                    byte[] buffer = new byte[4096];

                    //使用流读取内容到buffer中
                    //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少
                    int length = stream.Read(buffer, 0, buffer.Length);
                    while (length > 0)
                    {
                        //如果Unity客户端关闭,停止下载
                        if (m_bIsStop)
                        {
                            break;
                        }

                        //将内容再写入本地文件中
                        fs.Write(buffer, 0, length);

                        //计算进度
                        DownloadFileLength += length;
                        Progress            = (float)DownloadFileLength / (float)totalLength;

                        //类似尾递归
                        length = stream.Read(buffer, 0, buffer.Length);
                    }

                    stream.Close();
                    stream.Dispose();
                }
                else
                {
                    Progress = 1;
                }

                fs.Close();

                stopWatch.Stop();
                C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds);

                //如果下载完毕,执行回调
                if (Progress == 1)
                {
                    if (callback != null)
                    {
                        callback();
                    }

                    m_Thread.Abort();
                }
            });

            //开启子线程
            m_Thread.IsBackground = true;
            m_Thread.Priority     = threadPriority;
            m_Thread.Start();
        }
Beispiel #24
0
 public void GotoState(string name)
 {
     C_DebugHelper.LogFormat("C_GameStateCtrl Goto State {0}", name);
     m_GameState.ChangeState(name);
 }