Skip to content

Commit 861ed51

Browse files
author
DESKTOP-UBV38B7\codingworks
committed
增加了WebRequest模块
1 parent 31cac46 commit 861ed51

19 files changed

+620
-63
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: 138 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,146 @@
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+
#endregion
29+
30+
#region 重写函数
31+
32+
public override void OnEnter(params object[] parameters)
33+
{
34+
base.OnEnter(parameters);
35+
GameMode.Event.AddListener<HttpReadTextSuccessEventArgs>(OnHttpReadTextSuccess);
36+
GameMode.Event.AddListener<HttpReadTextFaileEventArgs>(OnHttpReadTextFaile);
37+
GameMode.Event.AddListener<DownloadSuccessEventArgs>(OnDownloadSuccess);
38+
GameMode.Event.AddListener<DownloadFaileEventArgs>(OnDownloadFaile);
39+
40+
_localVersion=LoadLocalVersion();
41+
LoadRemoteVersion();
42+
}
43+
44+
45+
public override void OnExit()
46+
{
47+
GameMode.Event.RemoveListener<HttpReadTextSuccessEventArgs>(OnHttpReadTextSuccess);
48+
GameMode.Event.RemoveListener<HttpReadTextFaileEventArgs>(OnHttpReadTextFaile);
49+
GameMode.Event.RemoveListener<DownloadSuccessEventArgs>(OnDownloadSuccess);
50+
GameMode.Event.RemoveListener<DownloadFaileEventArgs>(OnDownloadFaile);
51+
base.OnExit();
52+
}
53+
54+
public override void OnFixedUpdate()
55+
{
56+
base.OnFixedUpdate();
57+
}
58+
59+
public override void OnInit()
60+
{
61+
base.OnInit();
62+
}
63+
64+
public override void OnUpdate()
65+
{
66+
base.OnUpdate();
67+
//更新完资源
68+
//...
69+
//切换到加载界面
70+
// ChangeState<LoadResourceState>();
71+
}
72+
73+
#endregion
74+
75+
76+
#region 事件回调
77+
//http文本读取成功
78+
private void OnHttpReadTextSuccess(object sender, IEventArgs e)
79+
{
80+
HttpReadTextSuccessEventArgs ne = (HttpReadTextSuccessEventArgs)e;
81+
if (ne != null)
82+
{
83+
_remoteVersion = JsonUtility.FromJson<AssetBundleVersionInfo>(ne.Content);
84+
//如果资源版本不一样 则更新资源
85+
if (!CompareVersion())
86+
UpdateResource();
87+
}
88+
}
89+
//http文件读取错误
90+
private void OnHttpReadTextFaile(object sender, IEventArgs e)
91+
{
92+
}
93+
//加载文件成功
94+
private void OnDownloadSuccess(object sender, IEventArgs e)
95+
{
96+
}
97+
//下载文件失败
98+
private void OnDownloadFaile(object sender, IEventArgs e)
99+
{
100+
101+
}
102+
103+
#endregion
104+
105+
#region 内部函数
106+
107+
//加载本地版本信息
108+
private AssetBundleVersionInfo LoadLocalVersion()
109+
{
110+
string localPath = GameMode.Resource.LocalPath + "/AssetVersion.txt";
111+
if(!File.Exists(localPath))
112+
return null;
113+
114+
string content= File.ReadAllText(localPath);
115+
return JsonUtility.FromJson<AssetBundleVersionInfo>(content);
116+
}
117+
118+
//加载远程版本信息
119+
private void LoadRemoteVersion()
120+
{
121+
string remotePath = GameMode.Resource.ResUpdatePath + "/AssetVersion.txt";
122+
GameMode.WebRequest.ReadHttpText(remotePath);
123+
}
124+
125+
//比较版本
126+
private bool CompareVersion()
127+
{
128+
return _remoteVersion.Version == _localVersion.Version;
129+
}
130+
131+
//更新资源
132+
private void UpdateResource()
133+
{
134+
foreach (var item in _remoteVersion.Resources)
135+
{
136+
//本地有响应文件则跳过
137+
if (_localVersion.Resources.Contains(item))
138+
continue;
139+
string remoteUrl = Path.Combine(GameMode.Resource.ResUpdatePath, item.Name);
140+
//获取本地文件的路径
141+
string localPath = Path.Combine(GameMode.Resource.LocalPath, item.Name);
142+
//创建文件夹
143+
int index = localPath.LastIndexOf("/", StringComparison.Ordinal);
144+
string localDir = localPath.Substring(0, index);
145+
if (!Directory.Exists(localDir))
146+
Directory.CreateDirectory(localDir);
147+
GameMode.WebRequest.StartDownload(remoteUrl, localPath);
148+
}
149+
}
150+
151+
#endregion
152+
153+
}
49154
}

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);
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.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #网页请求帮助类接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年7月12日 11点16分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
14+
namespace GameFramework.Taurus
15+
{
16+
public interface IWebRequestHelper
17+
{
18+
/// <summary>
19+
/// 读取http上的文件
20+
/// </summary>
21+
/// <param name="path"></param>
22+
/// <param name="result"></param>
23+
void ReadHttpText(string url, Action<string, bool,string> result);
24+
}
25+
}

Assets/GameFramework/WebRequest/IWebRequestHelper.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)