Skip to content

Commit e6d8c1b

Browse files
author
DESKTOP-UBV38B7\codingworks
committed
Merge branch 'master' into ILRuntime
2 parents 0b17ff8 + 1d94243 commit e6d8c1b

File tree

6 files changed

+172
-18
lines changed

6 files changed

+172
-18
lines changed

Assets/Game/Scripts/Resource/EditorResourceHelper.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ public T LoadAsset<T>(string assetName) where T : Object
3535
return AssetDatabase.LoadAssetAtPath<T>(assetName);
3636
}
3737

38+
39+
/// <summary>
40+
/// 异步加载资源
41+
/// </summary>
42+
/// <param name="assetName">资源名称</param>
43+
public AssetBundleRequest LoadAssetAsync<T>(string assetName) where T : Object
44+
{
45+
//assetName = assetName.ToLower();
46+
//AssetBundle assetBundle;
47+
//if (_allAssets.TryGetValue(assetName, out assetBundle))
48+
//{
49+
// //加载相关依赖
50+
// string[] dependencies = _mainfest.GetAllDependencies(assetName);
51+
// foreach (var item in dependencies)
52+
// {
53+
// AssetBundle.LoadFromFile(_readPath + "/" + item);
54+
// }
55+
// AssetBundleRequest requetAsset = assetBundle.LoadAssetAsync<T>(assetName);
56+
// // _allObjects.Add(assetName, asset);
57+
// return requetAsset;
58+
//}
59+
return null;
60+
}
61+
3862
/// <summary>
3963
/// 异步加载场景
4064
/// </summary>
@@ -48,9 +72,9 @@ public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = Load
4872
/// 卸载场景
4973
/// </summary>
5074
/// <param name="sceneName"></param>
51-
public void UnloadScene(string sceneName)
75+
public AsyncOperation UnloadSceneAsync(string sceneName)
5276
{
53-
UnitySceneManager.UnloadScene(sceneName);
77+
return UnitySceneManager.UnloadSceneAsync(sceneName);
5478
}
5579

5680
/// <summary>

Assets/GameFramework/Resource/BundleResourceHelper .cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ public T LoadAsset<T>(string assetName) where T : Object
8686
return null;
8787
}
8888

89+
/// <summary>
90+
/// 异步加载资源
91+
/// </summary>
92+
/// <param name="assetName">资源名称</param>
93+
public AssetBundleRequest LoadAssetAsync<T>(string assetName) where T : Object
94+
{
95+
assetName = assetName.ToLower();
96+
AssetBundle assetBundle;
97+
if (_allAssets.TryGetValue(assetName, out assetBundle))
98+
{
99+
//加载相关依赖
100+
string[] dependencies = _mainfest.GetAllDependencies(assetName);
101+
foreach (var item in dependencies)
102+
{
103+
AssetBundle.LoadFromFile(_readPath + "/" + item);
104+
}
105+
AssetBundleRequest requetAsset = assetBundle.LoadAssetAsync<T>(assetName);
106+
// _allObjects.Add(assetName, asset);
107+
return requetAsset;
108+
}
109+
return null;
110+
}
111+
112+
89113
/// <summary>
90114
/// 异步加载场景
91115
/// </summary>
@@ -110,9 +134,9 @@ public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = Load
110134
/// 卸载场景
111135
/// </summary>
112136
/// <param name="sceneName"></param>
113-
public void UnloadScene(string sceneName)
137+
public AsyncOperation UnloadSceneAsync(string sceneName)
114138
{
115-
UnityEngine.SceneManagement.SceneManager.UnloadScene(sceneName);
139+
return UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(sceneName);
116140
}
117141

118142

@@ -130,9 +154,7 @@ public void Clear()
130154
}
131155

132156
#endregion
133-
134-
135-
157+
136158
#region 内部函数
137159
/// <summary>
138160
/// 加载mainfest

Assets/GameFramework/Resource/IResourceHelper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public interface IResourceHelper
2828
/// <returns></returns>
2929
T LoadAsset<T>(string assetName) where T : UnityEngine.Object;
3030

31+
/// <summary>
32+
/// 异步加载资源
33+
/// </summary>
34+
/// <param name="assetName"></param>
35+
AssetBundleRequest LoadAssetAsync<T>(string assetName) where T : UnityEngine.Object;
36+
3137
/// <summary>
3238
/// 异步加载场景
3339
/// </summary>
@@ -38,7 +44,7 @@ public interface IResourceHelper
3844
/// 卸载场景
3945
/// </summary>
4046
/// <param name="sceneName"></param>
41-
void UnloadScene(string sceneName);
47+
AsyncOperation UnloadSceneAsync(string sceneName);
4248

4349
/// <summary>
4450
/// 清理资源

Assets/GameFramework/Resource/ResourceManager.cs

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,47 @@
77
// <time> #2018年6月22日 17点11分# </time>
88
//-----------------------------------------------------------------------
99

10+
using System.Collections.Generic;
1011
using UnityEngine;
1112
using UnityEngine.SceneManagement;
1213
using UnitySceneManager = UnityEngine.SceneManagement.SceneManager;
1314
using LoadSceneMode = UnityEngine.SceneManagement.LoadSceneMode;
1415

1516
namespace GameFramework.Taurus
1617
{
17-
public sealed class ResourceManager:GameFrameworkModule
18+
public sealed class ResourceManager:GameFrameworkModule,IUpdate
1819
{
19-
#region 属性
20+
#region 属性
21+
22+
//事件触发类
23+
private EventManager _event;
2024
//资源管理器 帮助类
2125
private IResourceHelper _resourceHelper;
22-
2326
//GameObject对象池管理器
2427
private IGameObjectPoolHelper _gameObjectPoolHelper;
2528

29+
30+
//场景加载中事件
31+
private SceneLoadingEventArgs _sceneLoadingEventArgs;
32+
//场景加载完毕事件
33+
private SceneLoadedEventArgs _sceneLoadedEventArgs;
34+
//场景异步加载
35+
private Dictionary<string, AsyncOperation> _sceneAsyncOperations;
36+
2637
#endregion
2738

2839
#region 构造函数
2940
public ResourceManager()
3041
{
31-
//添加对象池管理器
32-
_gameObjectPoolHelper = new GameObject("GameObject_Pool").AddComponent<GameObjectPoolHelper>();
42+
//获取事件管理器
43+
_event = GameFrameworkMode.GetModule<EventManager>();
44+
//添加对象池管理器
45+
_gameObjectPoolHelper = new GameObject("GameObject_Pool").AddComponent<GameObjectPoolHelper>();
46+
47+
//场景事件
48+
_sceneLoadingEventArgs = new SceneLoadingEventArgs();
49+
_sceneLoadedEventArgs = new SceneLoadedEventArgs();
50+
_sceneAsyncOperations = new Dictionary<string, AsyncOperation>();
3351
}
3452
#endregion
3553

@@ -70,6 +88,8 @@ public T LoadAsset<T>(string assetName) where T : UnityEngine.Object
7088
return _resourceHelper.LoadAsset<T>(assetName);
7189
}
7290

91+
92+
7393
/// <summary>
7494
/// 异步加载场景
7595
/// </summary>
@@ -78,17 +98,23 @@ public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = Load
7898
{
7999
if (_resourceHelper == null)
80100
return null;
101+
AsyncOperation asyncOperation= _resourceHelper.LoadSceneAsync(sceneName, mode);
102+
_sceneAsyncOperations.Add(sceneName, asyncOperation);
103+
return asyncOperation;
81104

82-
return _resourceHelper.LoadSceneAsync(sceneName, mode);
83105
}
84106

85107
/// <summary>
86108
/// 卸载场景
87109
/// </summary>
88110
/// <param name="sceneName"></param>
89-
public void UnloadScene(string sceneName)
111+
public void UnloadSceneAsync(string sceneName)
90112
{
91-
UnitySceneManager.UnloadScene(sceneName);
113+
if (_resourceHelper == null)
114+
return;
115+
116+
_resourceHelper.UnloadSceneAsync(sceneName);
117+
return;
92118
}
93119

94120
/// <summary>
@@ -166,11 +192,36 @@ public void DespawnPrefab(string assetName)
166192
}
167193

168194
#endregion
195+
169196

197+
#region 重写函数
170198

199+
public void OnUpdate()
200+
{
201+
if (_sceneAsyncOperations.Count > 0)
202+
{
203+
foreach (var item in _sceneAsyncOperations)
204+
{
205+
//触发加载完毕事件
206+
if (item.Value.isDone)
207+
{
208+
_sceneLoadedEventArgs.SceneName = item.Key;
209+
_event.Trigger(this, _sceneLoadedEventArgs);
210+
_sceneAsyncOperations.Remove(item.Key);
211+
break;
212+
}
213+
//触发正在加载事件
214+
else
215+
{
216+
_sceneLoadingEventArgs.SceneName = item.Key;
217+
_sceneLoadingEventArgs.Progress = item.Value.progress;
218+
_event.Trigger(this, _sceneLoadingEventArgs);
219+
}
220+
}
221+
}
222+
}
171223

172-
#region 重写函数
173-
public override void OnClose()
224+
public override void OnClose()
174225
{
175226
if (_resourceHelper != null)
176227
_resourceHelper.Clear();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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年6月25日 16点43分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
namespace GameFramework.Taurus
11+
{
12+
/// <summary>
13+
/// 场景加载中事件
14+
/// </summary>
15+
public class SceneLoadingEventArgs : GameEventArgs<SceneLoadingEventArgs>
16+
{
17+
/// <summary>
18+
/// 场景名称
19+
/// </summary>
20+
public string SceneName;
21+
/// <summary>
22+
/// 场景加载进度
23+
/// </summary>
24+
public float Progress;
25+
}
26+
27+
/// <summary>
28+
/// 场景加载完成事件
29+
/// </summary>
30+
public class SceneLoadedEventArgs : GameEventArgs<SceneLoadedEventArgs>
31+
{
32+
/// <summary>
33+
/// 场景名称
34+
/// </summary>
35+
public string SceneName;
36+
}
37+
38+
}

Assets/GameFramework/Resource/SceneEventArgs.cs.meta

Lines changed: 13 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)