Skip to content

Commit 00c23cf

Browse files
author
DESKTOP-UBV38B7\codingworks
committed
Merge branch 'master' into ILRuntime
2 parents 1698659 + 80afcec commit 00c23cf

18 files changed

+729
-62
lines changed

Assets/Game/Scripts/Base/GameMode.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class GameMode : MonoBehaviour
2121
public static NodeManager Node;
2222
public static ResourceManager Resource;
2323
public static UIManager UI;
24+
public static WebRequestManager WebRequest;
2425

2526
/// <summary>
2627
/// 当前程序集
@@ -40,7 +41,7 @@ public class GameMode : MonoBehaviour
4041
/// <summary>
4142
/// 资源本地路径
4243
/// </summary>
43-
public PathType LocalPath = PathType.ReadOnly;
44+
public PathType LocalPathType = PathType.ReadOnly;
4445
/// <summary>
4546
/// ab资源默认包名称
4647
/// </summary>
@@ -62,29 +63,24 @@ IEnumerator Start()
6263
Node = GameFrameworkMode.GetModule<NodeManager>();
6364
Resource = GameFrameworkMode.GetModule<ResourceManager>();
6465
UI = GameFrameworkMode.GetModule<UIManager>();
65-
#endregion
66+
WebRequest = GameFrameworkMode.GetModule<WebRequestManager>();
67+
#endregion
6668

67-
#region resource
68-
Resource.ResUpdateType = ResUpdateType;
69+
#region resource
70+
Resource.ResUpdateType = ResUpdateType;
6971
Resource.ResUpdatePath = ResUpdatePath;
70-
Resource.LocalPath = LocalPath;
72+
Resource.LocalPathType = LocalPathType;
7173
Resource.RootAssetBundle = AssetBundleName;
72-
//#if UNITY_EDITOR
73-
// //设置资源的模式
74-
// if (ResUpdateType==ResourceUpdateType.Editor)
75-
// Resource.SetResourceHelper(new EditorResourceHelper());
76-
// else
77-
// {
78-
79-
// Resource.SetResourceHelper(new BundleResourceHelper());
80-
// Resource.SetResourcePath(AssetBundleName);
81-
// }
82-
//#else
83-
////非编辑器模式下 -- 只支持AssetBundle资源加载
84-
// IsEditorMode = false;
85-
// Resource.SetResourceHelper(new BundleResourceHelper());
86-
// Resource.SetResourcePath(DefaultPathType, AssetBundleName);
87-
//#endif
74+
#endregion
75+
76+
#region WebRequest
77+
//设置帮助类
78+
IWebRequestHelper webRequestHelper =
79+
new GameObject("IWebRequestHelper").AddComponent<WebRquestMonoHelper>();
80+
IWebDownloadHelper webDownloadHelper =
81+
new GameObject("IWebRequestHelper").AddComponent<WebDownloadMonoHelper>();
82+
WebRequest.SetWebRequestHelper(webRequestHelper);
83+
WebRequest.SetWebDownloadHelper(webDownloadHelper);
8884
#endregion
8985

9086
#region state

Assets/Game/Scripts/Editor/GameModeEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,16 @@ void DrawEditorModeGUI()
230230
{
231231
_gameMode.ResUpdatePath =
232232
EditorGUILayout.TextField("Resource Update Path", _gameMode.ResUpdatePath);
233-
_gameMode.LocalPath =
233+
_gameMode.LocalPathType =
234234
(PathType)EditorGUILayout.EnumPopup("Local Path Type", PathType.ReadWrite);
235235
}
236236
else
237237
{
238-
_gameMode.LocalPath =
239-
(PathType)EditorGUILayout.EnumPopup("Local Path Type", _gameMode.LocalPath);
238+
_gameMode.LocalPathType =
239+
(PathType)EditorGUILayout.EnumPopup("Local Path Type", _gameMode.LocalPathType);
240240
}
241241
string path = "";
242-
switch (_gameMode.LocalPath)
242+
switch (_gameMode.LocalPathType)
243243
{
244244
case PathType.DataPath:
245245
path = Application.dataPath;

Assets/Game/Scripts/State/CheckResourceState.cs

Lines changed: 182 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,190 @@
99
using System;
1010
using System.Collections;
1111
using System.Collections.Generic;
12+
using System.IO;
1213
using UnityEngine;
1314

1415
namespace GameFramework.Taurus
1516
{
16-
[GameState]
17-
public class CheckResourceState : GameState
18-
{
19-
public override void OnEnter(params object[] parameters)
20-
{
21-
base.OnEnter(parameters);
22-
}
23-
24-
25-
public override void OnExit()
26-
{
27-
base.OnExit();
28-
}
29-
30-
public override void OnFixedUpdate()
31-
{
32-
base.OnFixedUpdate();
33-
}
34-
35-
public override void OnInit()
36-
{
37-
base.OnInit();
38-
}
39-
40-
public override void OnUpdate()
41-
{
42-
base.OnUpdate();
43-
//更新完资源
44-
//...
45-
//切换到加载界面
46-
ChangeState<LoadResourceState>();
47-
}
48-
}
17+
[GameState]
18+
public class CheckResourceState : GameState
19+
{
20+
#region 属性
21+
22+
//本地版本信息
23+
private AssetBundleVersionInfo _localVersion;
24+
25+
//远程版本信息
26+
private AssetBundleVersionInfo _remoteVersion;
27+
28+
//资源更新完成
29+
private bool _resourceUpdateDone;
30+
//需要更新的资源
31+
private Dictionary<string, string> _downloadResouces;
32+
#endregion
33+
34+
#region 重写函数
35+
36+
public override void OnInit()
37+
{
38+
base.OnInit();
39+
40+
_downloadResouces = new Dictionary<string, string>();
41+
}
42+
43+
public override void OnEnter(params object[] parameters)
44+
{
45+
base.OnEnter(parameters);
46+
GameMode.Event.AddListener<HttpReadTextSuccessEventArgs>(OnHttpReadTextSuccess);
47+
GameMode.Event.AddListener<HttpReadTextFaileEventArgs>(OnHttpReadTextFaile);
48+
GameMode.Event.AddListener<DownloadSuccessEventArgs>(OnDownloadSuccess);
49+
GameMode.Event.AddListener<DownloadFaileEventArgs>(OnDownloadFaile);
50+
GameMode.Event.AddListener<DownloadProgressEventArgs>(OnDownloadProgress);
51+
52+
_localVersion=LoadLocalVersion();
53+
LoadRemoteVersion();
54+
}
55+
56+
57+
public override void OnExit()
58+
{
59+
GameMode.Event.RemoveListener<HttpReadTextSuccessEventArgs>(OnHttpReadTextSuccess);
60+
GameMode.Event.RemoveListener<HttpReadTextFaileEventArgs>(OnHttpReadTextFaile);
61+
GameMode.Event.RemoveListener<DownloadSuccessEventArgs>(OnDownloadSuccess);
62+
GameMode.Event.RemoveListener<DownloadFaileEventArgs>(OnDownloadFaile);
63+
GameMode.Event.RemoveListener<DownloadProgressEventArgs>(OnDownloadProgress);
64+
65+
base.OnExit();
66+
}
67+
68+
public override void OnFixedUpdate()
69+
{
70+
base.OnFixedUpdate();
71+
}
72+
73+
public override void OnUpdate()
74+
{
75+
base.OnUpdate();
76+
77+
//更新资源--切换到加载界面
78+
if (_resourceUpdateDone&& _downloadResouces.Count==0)
79+
ChangeState<LoadResourceState>();
80+
}
81+
82+
#endregion
83+
84+
85+
#region 事件回调
86+
//http文本读取成功
87+
private void OnHttpReadTextSuccess(object sender, IEventArgs e)
88+
{
89+
HttpReadTextSuccessEventArgs ne = (HttpReadTextSuccessEventArgs)e;
90+
if (ne != null)
91+
{
92+
_remoteVersion = JsonUtility.FromJson<AssetBundleVersionInfo>(ne.Content);
93+
//如果资源版本不一样 则更新资源
94+
if (!CompareVersion())
95+
{
96+
//更新资源
97+
UpdateResource();
98+
//下载资源
99+
DownloadResource();
100+
}
101+
102+
//资源更新完成
103+
_resourceUpdateDone = true;
104+
}
105+
}
106+
//http文件读取错误
107+
private void OnHttpReadTextFaile(object sender, IEventArgs e)
108+
{
109+
HttpReadTextFaileEventArgs ne = (HttpReadTextFaileEventArgs) e;
110+
if (ne != null)
111+
Debug.LogError(ne.Error);
112+
}
113+
//加载文件成功
114+
private void OnDownloadSuccess(object sender, IEventArgs e)
115+
{
116+
DownloadSuccessEventArgs ne = (DownloadSuccessEventArgs) e;
117+
if (_downloadResouces.ContainsKey(ne.RemoteUrl))
118+
_downloadResouces.Remove(ne.RemoteUrl);
119+
}
120+
//下载文件失败
121+
private void OnDownloadFaile(object sender, IEventArgs e)
122+
{
123+
DownloadFaileEventArgs ne = (DownloadFaileEventArgs)e;
124+
if (ne != null)
125+
Debug.LogError(ne.Error);
126+
}
127+
//下载进度
128+
private void OnDownloadProgress(object sender, IEventArgs e)
129+
{
130+
DownloadProgressEventArgs ne = (DownloadProgressEventArgs) e;
131+
Debug.Log(
132+
$"path:{ne.LocalPath} progress:{ne.DownloadProgress} bytes:{ne.DownloadBytes} speed:{ne.DownloadSpeed}");
133+
}
134+
135+
#endregion
136+
137+
#region 内部函数
138+
139+
//加载本地版本信息
140+
private AssetBundleVersionInfo LoadLocalVersion()
141+
{
142+
string localPath = GameMode.Resource.LocalPath + "/AssetVersion.txt";
143+
if(!File.Exists(localPath))
144+
return null;
145+
146+
string content= File.ReadAllText(localPath);
147+
return JsonUtility.FromJson<AssetBundleVersionInfo>(content);
148+
}
149+
150+
//加载远程版本信息
151+
private void LoadRemoteVersion()
152+
{
153+
string remotePath = GameMode.Resource.ResUpdatePath + "/AssetVersion.txt";
154+
GameMode.WebRequest.ReadHttpText(remotePath);
155+
}
156+
157+
//比较版本
158+
private bool CompareVersion()
159+
{
160+
return _remoteVersion.Version == _localVersion.Version;
161+
}
162+
163+
//更新资源
164+
private void UpdateResource()
165+
{
166+
foreach (var item in _remoteVersion.Resources)
167+
{
168+
//本地有响应文件则跳过
169+
if (_localVersion.Resources.Contains(item))
170+
continue;
171+
string remoteUrl = Path.Combine(GameMode.Resource.ResUpdatePath, item.Name);
172+
//获取本地文件的路径
173+
string localPath = Path.Combine(GameMode.Resource.LocalPath, item.Name);
174+
//创建文件夹
175+
int index = localPath.LastIndexOf("/", StringComparison.Ordinal);
176+
string localDir = localPath.Substring(0, index);
177+
if (!Directory.Exists(localDir))
178+
Directory.CreateDirectory(localDir);
179+
180+
//添加需要下载的资源
181+
_downloadResouces.Add(remoteUrl, localPath);
182+
}
183+
184+
}
185+
186+
//下载资源
187+
private void DownloadResource()
188+
{
189+
foreach (var item in _downloadResouces)
190+
{
191+
GameMode.WebRequest.StartDownload(item.Key, item.Value);
192+
}
193+
}
194+
195+
#endregion
196+
197+
}
49198
}

Assets/GameFramework/Resource/ResourceManager.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,32 @@ public sealed class ResourceManager:GameFrameworkModule,IUpdate
4343
/// </summary>
4444
public ResourceUpdateType ResUpdateType= ResourceUpdateType.Local;
4545
/// <summary>
46-
/// 资源本地路径
46+
/// 资源本地路径 类型
4747
/// </summary>
48-
public PathType LocalPath= PathType.ReadOnly;
49-
/// <summary>
48+
public PathType LocalPathType= PathType.ReadOnly;
49+
/// <summary>
50+
/// 资源本地路径
51+
/// </summary>
52+
public string LocalPath
53+
{
54+
get
55+
{
56+
switch (LocalPathType)
57+
{
58+
case PathType.DataPath:
59+
return Application.dataPath;
60+
case PathType.ReadOnly:
61+
return Application.streamingAssetsPath;
62+
case PathType.ReadWrite:
63+
return Application.persistentDataPath;
64+
case PathType.TemporaryCache:
65+
return Application.temporaryCachePath;
66+
}
67+
return "";
68+
}
69+
}
70+
71+
/// <summary>
5072
/// ab资源默认包名称
5173
/// </summary>
5274
public string RootAssetBundle = "AssetBundles/AssetBundles";
@@ -86,7 +108,7 @@ public void SetResourceHelper(IResourceHelper resourceHelper)
86108

87109
//设置资源的路径,默认是为只读路径: Application.streamingAssetsPath;
88110
if (resourceHelper is BundleResourceHelper)
89-
_resourceHelper.SetResourcePath(LocalPath, RootAssetBundle);
111+
_resourceHelper.SetResourcePath(LocalPathType, RootAssetBundle);
90112
}
91113

92114

Assets/GameFramework/WebRequest.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #http下载帮助接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年7月12日 11点36分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
14+
namespace GameFramework.Taurus
15+
{
16+
public interface IWebDownloadHelper
17+
{
18+
void StartDownload(string remoteUrl, string localPath,Action<string,string,bool,string> result,Action<string,string,ulong,float,float> progress);
19+
}
20+
}

Assets/GameFramework/WebRequest/IWebDownloadHelper.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)