Skip to content

Commit d148ca0

Browse files
committed
增加ResourceManager&UIManager
1 parent 3efc42a commit d148ca0

13 files changed

+960
-37
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #AssetBundle资源管理类# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年6月24日 17点00分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System.Collections.Generic;
11+
using UnityEngine;
12+
using UnityEngine.SceneManagement;
13+
14+
namespace GameFramework.Taurus
15+
{
16+
public sealed class BundleResourceHelper : IResourceHelper
17+
{
18+
//路径
19+
private string _readPath;
20+
//所有资源AssetBundle引用
21+
private readonly Dictionary<string, AssetBundle> _allAssets = new Dictionary<string, AssetBundle>();
22+
//资源引用
23+
private AssetBundleManifest _mainfest;
24+
25+
/// <summary>
26+
/// 设置资源的路径,默认是为只读路径:Application.streamingAssetsPath;
27+
/// </summary>
28+
/// <param name="path"></param>
29+
public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles")
30+
{
31+
switch (pathType)
32+
{
33+
case PathType.ReadOnly:
34+
_readPath = Application.streamingAssetsPath;
35+
break;
36+
case PathType.ReadWrite:
37+
_readPath = Application.persistentDataPath;
38+
break;
39+
case PathType.DataPath:
40+
_readPath = Application.dataPath;
41+
break;
42+
case PathType.TemporaryCache:
43+
_readPath = Application.temporaryCachePath;
44+
break;
45+
default:
46+
_readPath = Application.streamingAssetsPath;
47+
break;
48+
}
49+
50+
51+
string rootABPath = _readPath + "/" + rootAssetBundle;
52+
string directionPath = _readPath;
53+
54+
int index = rootAssetBundle.LastIndexOf("/");
55+
if (index > 0 && index < (rootAssetBundle.Length - 1))
56+
directionPath += "/" + rootAssetBundle.Substring(0, index);
57+
//加载mainfest文件
58+
LoadPlatformMainfest(rootABPath, directionPath);
59+
}
60+
61+
/// <summary>
62+
/// 加载资源
63+
/// </summary>
64+
/// <typeparam name="T"></typeparam>
65+
/// <param name="assetName"></param>
66+
/// <returns></returns>
67+
public T LoadAsset<T>(string assetName) where T : Object
68+
{
69+
assetName = assetName.ToLower();
70+
//if (_allObjects.ContainsKey(assetName))
71+
// return (T)_allObjects[assetName];
72+
73+
AssetBundle assetBundle;
74+
if (_allAssets.TryGetValue(assetName, out assetBundle))
75+
{
76+
//加载相关依赖
77+
string[] dependencies = _mainfest.GetAllDependencies(assetName);
78+
foreach (var item in dependencies)
79+
{
80+
AssetBundle.LoadFromFile(_readPath + "/" + item);
81+
}
82+
T asset = assetBundle.LoadAsset<T>(assetName);
83+
// _allObjects.Add(assetName, asset);
84+
return asset;
85+
}
86+
return null;
87+
}
88+
89+
/// <summary>
90+
/// 异步加载场景
91+
/// </summary>
92+
/// <param name="sceneName"></param>
93+
public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Additive)
94+
{
95+
AssetBundle assetBundle;
96+
if (_allAssets.TryGetValue(sceneName, out assetBundle))
97+
{
98+
//加载相关依赖
99+
string[] dependencies = _mainfest.GetAllDependencies(sceneName);
100+
foreach (var item in dependencies)
101+
{
102+
AssetBundle.LoadFromFile(_readPath + "/" + item);
103+
}
104+
return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, mode);
105+
}
106+
return null;
107+
}
108+
109+
/// <summary>
110+
/// 卸载场景
111+
/// </summary>
112+
/// <param name="sceneName"></param>
113+
public void UnloadScene(string sceneName)
114+
{
115+
UnityEngine.SceneManagement.SceneManager.UnloadScene(sceneName);
116+
}
117+
118+
119+
#region 事件回调
120+
/// <summary>
121+
/// 清理资源
122+
/// </summary>
123+
public void Clear()
124+
{
125+
foreach (var item in _allAssets.Values)
126+
item.Unload(true);
127+
_allAssets.Clear();
128+
129+
_mainfest = null;
130+
}
131+
132+
#endregion
133+
134+
135+
136+
#region 内部函数
137+
/// <summary>
138+
/// 加载mainfest
139+
/// </summary>
140+
private void LoadPlatformMainfest(string rootBundleaPath, string folderPath)
141+
{
142+
//string assetBundlePath = _readPath + "/AssetBundles";
143+
AssetBundle mainfestAssetBundle = AssetBundle.LoadFromFile(rootBundleaPath);
144+
_mainfest = mainfestAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//
145+
string[] assetBundleNames = _mainfest.GetAllAssetBundles();
146+
foreach (var item in assetBundleNames)
147+
{
148+
AssetBundle assetBundle = AssetBundle.LoadFromFile(folderPath + "/" + item);
149+
string[] assetNames = assetBundle.GetAllAssetNames();
150+
if (assetBundle.isStreamedSceneAssetBundle)
151+
assetNames = assetBundle.GetAllScenePaths();
152+
foreach (var name in assetNames)
153+
{
154+
if (!_allAssets.ContainsKey(name))
155+
_allAssets.Add(name, assetBundle);
156+
}
157+
}
158+
mainfestAssetBundle.Unload(false);
159+
}
160+
#endregion
161+
162+
}
163+
}

Assets/GameFramework/Resource/BundleResourceHelper .cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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月22日 17点05分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using UnityEngine;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
14+
namespace GameFramework.Taurus
15+
{
16+
internal class GameObjectPoolHelper : MonoBehaviour, IGameObjectPoolHelper
17+
{
18+
/// <summary>
19+
/// 对象池名称
20+
/// </summary>
21+
public string PoolName { get; set; }
22+
23+
/// <summary>
24+
/// 对象池所有的预设
25+
/// </summary>
26+
private readonly Dictionary<string, PoolPrefabInfo> _prefabs = new Dictionary<string, PoolPrefabInfo>();
27+
28+
/// <summary>
29+
/// 已经生成并显示物体
30+
/// </summary>
31+
private readonly Dictionary<string, List<GameObject>> _spawneds = new Dictionary<string, List<GameObject>>();
32+
33+
/// <summary>
34+
/// 已经生成未显示物体
35+
/// </summary>
36+
private readonly Dictionary<string, Queue<GameObject>> _despawneds = new Dictionary<string, Queue<GameObject>>();
37+
38+
public void AddPrefab(string assetName, PoolPrefabInfo prefabInfo)
39+
{
40+
if (_prefabs.ContainsKey(assetName))
41+
{
42+
Debug.Log("已经存在资源:" + assetName);
43+
return;
44+
}
45+
if (prefabInfo.Prefab == null)
46+
{
47+
//根据assetName,直接从ResourceManager里面加载
48+
prefabInfo.Prefab = GameFrameworkMode.GetModule<ResourceManager>().LoadAsset<GameObject>(assetName);
49+
if (prefabInfo.Prefab == null)
50+
{
51+
Debug.Log("预设资源为null:" + assetName);
52+
return;
53+
}
54+
}
55+
_prefabs[assetName] = prefabInfo;
56+
_spawneds[assetName] = new List<GameObject>();
57+
58+
Initialization(assetName, prefabInfo);
59+
}
60+
61+
public bool HasPrefab(string assetName)
62+
{
63+
return _prefabs.ContainsKey(assetName);
64+
}
65+
66+
private void Initialization(string assetName, PoolPrefabInfo prefabInfo)
67+
{
68+
var objects = new Queue<GameObject>();
69+
for (var i = 0; i < prefabInfo.PreloadAmount; i++)
70+
{
71+
var obj = GameObject.Instantiate(prefabInfo.Prefab);
72+
obj.transform.SetParent(transform);
73+
obj.SetActive(false);
74+
objects.Enqueue(obj);
75+
}
76+
_despawneds[assetName] = objects;
77+
}
78+
79+
public GameObject Spawn(string assetName)
80+
{
81+
if (!_despawneds.ContainsKey(assetName))
82+
{
83+
//在没有添加预设的时候 默认添加一个预设
84+
AddPrefab(assetName, new PoolPrefabInfo());
85+
}
86+
87+
GameObject gameObject;
88+
Queue<GameObject> queueGos = _despawneds[assetName];
89+
if (queueGos.Count > 0)
90+
{
91+
gameObject = queueGos.Dequeue();
92+
gameObject.SetActive(true);
93+
}
94+
else
95+
{
96+
97+
gameObject = GameObject.Instantiate((GameObject)_prefabs[assetName].Prefab);
98+
gameObject.transform.SetParent(transform);
99+
}
100+
_spawneds[assetName].Add(gameObject);
101+
102+
return gameObject;
103+
}
104+
105+
public void Despawn(GameObject go, bool isDestroy = false)
106+
{
107+
foreach (var item in _spawneds)
108+
{
109+
if (item.Value.Contains(go))
110+
{
111+
if (isDestroy)
112+
{
113+
item.Value.Remove(go);
114+
MonoBehaviour.Destroy(go);
115+
}
116+
else
117+
{
118+
Queue<GameObject> _queueObjs = _despawneds[item.Key];
119+
if ((_prefabs[item.Key].MaxAmount >= 0)
120+
&& (item.Value.Count + _queueObjs.Count) > _prefabs[item.Key].MaxAmount)
121+
{
122+
item.Value.Remove(go);
123+
MonoBehaviour.Destroy(go);
124+
}
125+
else
126+
{
127+
item.Value.Remove(go);
128+
go.SetActive(false);
129+
_despawneds[item.Key].Enqueue(go);
130+
}
131+
}
132+
return;
133+
}
134+
}
135+
}
136+
137+
public void DespawnAll()
138+
{
139+
foreach (var item in _spawneds)
140+
foreach (var go in item.Value)
141+
{
142+
item.Value.Remove(go);
143+
go.SetActive(false);
144+
_despawneds[item.Key].Enqueue(go);
145+
}
146+
}
147+
148+
public void DestroyAll()
149+
{
150+
foreach (var item in _spawneds)
151+
foreach (var go in item.Value)
152+
{
153+
item.Value.Remove(go);
154+
MonoBehaviour.Destroy(go);
155+
}
156+
157+
foreach (var item in _despawneds.Values)
158+
MonoBehaviour.Destroy(item.Dequeue());
159+
}
160+
161+
public void DespawnPrefab(string assetName)
162+
{
163+
if (_spawneds.ContainsKey(assetName))
164+
{
165+
var objs = _spawneds[assetName];
166+
foreach (var go in objs)
167+
{
168+
objs.Remove(go);
169+
go.SetActive(false);
170+
_despawneds[assetName].Enqueue(go);
171+
}
172+
}
173+
}
174+
}
175+
}

Assets/GameFramework/Resource/GameObjectPoolHelper .cs.meta

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