Skip to content

Commit f186d6b

Browse files
authored
Merge pull request #58 from AlanWeekend/main
增加初始化指定资源包操作,单独下载指定地址的资源文件
2 parents 623d301 + 4385123 commit f186d6b

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed

UnityProject/Assets/TEngine/Runtime/Extension/UnityExtension.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ public static void SetActive(this GameObject go, bool value, ref bool cacheValue
132132
/// <param name="spriteName">图片名称。</param>
133133
/// <param name="isSetNativeSize">是否使用原生分辨率。</param>
134134
/// <param name="isAsync">是否使用异步加载。</param>
135-
public static void SetSprite(this UnityEngine.UI.Image image, string spriteName, bool isSetNativeSize = false, bool isAsync = false)
135+
/// <param name="customPackageName">指定资源包的名称。不传使用默认资源包</param>
136+
public static void SetSprite(this UnityEngine.UI.Image image, string spriteName, bool isSetNativeSize = false,
137+
bool isAsync = false, string customPackageName = "")
136138
{
137139
if (image == null)
138140
{
@@ -147,7 +149,8 @@ public static void SetSprite(this UnityEngine.UI.Image image, string spriteName,
147149
{
148150
if (!isAsync)
149151
{
150-
image.sprite = GameModule.Resource.LoadAsset<Sprite>(spriteName);
152+
image.sprite =
153+
GameModule.Resource.LoadAsset<Sprite>(spriteName, customPackageName: customPackageName);
151154
if (isSetNativeSize)
152155
{
153156
image.SetNativeSize();
@@ -167,7 +170,7 @@ public static void SetSprite(this UnityEngine.UI.Image image, string spriteName,
167170
{
168171
image.SetNativeSize();
169172
}
170-
});
173+
}, customPackageName: customPackageName);
171174
}
172175
}
173176
}
@@ -178,7 +181,9 @@ public static void SetSprite(this UnityEngine.UI.Image image, string spriteName,
178181
/// <param name="spriteRenderer">Image组件。</param>
179182
/// <param name="spriteName">图片名称。</param>
180183
/// <param name="isAsync">是否使用异步加载。</param>
181-
public static void SetSprite(this SpriteRenderer spriteRenderer, string spriteName, bool isAsync = false)
184+
/// <param name="customPackageName">指定资源包的名称。不传使用默认资源包</param>
185+
public static void SetSprite(this SpriteRenderer spriteRenderer, string spriteName, bool isAsync = false,
186+
string customPackageName = "")
182187
{
183188
if (spriteRenderer == null)
184189
{
@@ -193,7 +198,8 @@ public static void SetSprite(this SpriteRenderer spriteRenderer, string spriteNa
193198
{
194199
if (!isAsync)
195200
{
196-
spriteRenderer.sprite = GameModule.Resource.LoadAsset<Sprite>(spriteName);
201+
spriteRenderer.sprite =
202+
GameModule.Resource.LoadAsset<Sprite>(spriteName, customPackageName: customPackageName);
197203
}
198204
else
199205
{
@@ -205,11 +211,11 @@ public static void SetSprite(this SpriteRenderer spriteRenderer, string spriteNa
205211
}
206212

207213
spriteRenderer.sprite = operation.AssetObject as Sprite;
208-
});
214+
}, customPackageName: customPackageName);
209215
}
210216
}
211217
}
212-
218+
213219
/// <summary>
214220
/// 查找子节点。
215221
/// </summary>
@@ -221,7 +227,7 @@ public static Transform FindChild(this Transform transform, string path)
221227
var findTrans = transform.Find(path);
222228
return findTrans != null ? findTrans : null;
223229
}
224-
230+
225231
/// <summary>
226232
/// 根据名字找到子节点,主要用于dummy接口。
227233
/// </summary>
@@ -252,7 +258,7 @@ public static Transform FindChildByName(this Transform transform, string name)
252258

253259
return null;
254260
}
255-
261+
256262
[TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
257263
public static Component FindChildComponent(this Type type, Transform transform, string path)
258264
{
@@ -264,7 +270,7 @@ public static Component FindChildComponent(this Type type, Transform transform,
264270

265271
return null;
266272
}
267-
273+
268274
public static T FindChildComponent<T>(this Transform transform, string path) where T : Component
269275
{
270276
var findTrans = transform.Find(path);

UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ public interface IResourceManager
8686
/// <summary>
8787
/// 初始化操作。
8888
/// </summary>
89+
/// <param name="customPackageName">指定资源包的名称。不传使用默认资源包</param>
8990
/// <returns></returns>
90-
InitializationOperation InitPackage();
91+
InitializationOperation InitPackage(string customPackageName = "");
9192

9293
/// <summary>
9394
/// 卸载资源。

UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal partial class ResourceManager : ModuleImp, IResourceManager
1515
#region Propreties
1616

1717
/// <summary>
18-
/// 资源包名称
18+
/// 默认资源包名称
1919
/// </summary>
2020
public string PackageName { get; set; } = "DefaultPackage";
2121

@@ -332,18 +332,24 @@ public void Initialize()
332332
/// 初始化资源包裹。
333333
/// </summary>
334334
/// <returns>初始化资源包裹操作句柄。</returns>
335-
public InitializationOperation InitPackage()
335+
public InitializationOperation InitPackage(string customPackageName = "")
336336
{
337337
// 创建默认的资源包
338-
string packageName = PackageName;
339-
var package = YooAssets.TryGetPackage(packageName);
338+
var targetPackageName = string.IsNullOrEmpty(customPackageName) || customPackageName.Equals(PackageName)
339+
? PackageName
340+
: customPackageName;
341+
var package = YooAssets.TryGetPackage(targetPackageName);
340342
if (package == null)
341343
{
342-
package = YooAssets.CreatePackage(packageName);
343-
YooAssets.SetDefaultPackage(package);
344+
package = YooAssets.CreatePackage(targetPackageName);
344345
}
345346

346-
DefaultPackage = package;
347+
// 设置默认资源包
348+
if (targetPackageName.Equals(PackageName))
349+
{
350+
YooAssets.SetDefaultPackage(package);
351+
DefaultPackage = package;
352+
}
347353

348354
#if UNITY_EDITOR
349355
//编辑器模式使用。
@@ -359,7 +365,7 @@ public InitializationOperation InitPackage()
359365
if (playMode == EPlayMode.EditorSimulateMode)
360366
{
361367
var createParameters = new EditorSimulateModeParameters();
362-
createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(packageName);
368+
createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(targetPackageName);
363369
initializationOperation = package.InitializeAsync(createParameters);
364370
}
365371

UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ private void Start()
230230
/// <summary>
231231
/// 初始化操作。
232232
/// </summary>
233+
/// <param name="customPackageName">指定资源包的名称。不传使用默认资源包</param>
233234
/// <returns></returns>
234-
public InitializationOperation InitPackage()
235+
public InitializationOperation InitPackage(string customPackageName = "")
235236
{
236-
return m_ResourceManager.InitPackage();
237+
return m_ResourceManager.InitPackage(customPackageName);
237238
}
238239

239240
/// <summary>
@@ -306,6 +307,27 @@ public ResourceDownloaderOperation CreateResourceDownloader(string customPackage
306307
}
307308
}
308309

310+
/// <summary>
311+
/// 创建资源下载器,用于下载当前资源版本指定地址的资源文件。
312+
/// </summary>
313+
/// <param name="location">资源地址</param>
314+
/// <param name="packageName">指定资源包的名称。不传使用默认资源包</param>
315+
public ResourceDownloaderOperation CreateResourceDownloader(string location, string packageName = "")
316+
{
317+
if (string.IsNullOrEmpty(packageName))
318+
{
319+
var package = YooAssets.GetPackage(this.packageName);
320+
Downloader = package.CreateResourceDownloader(location, downloadingMaxNum, failedTryAgain);
321+
return Downloader;
322+
}
323+
else
324+
{
325+
var package = YooAssets.GetPackage(packageName);
326+
Downloader = package.CreateResourceDownloader(location, downloadingMaxNum, failedTryAgain);
327+
return Downloader;
328+
}
329+
}
330+
309331
/// <summary>
310332
/// 清理包裹未使用的缓存文件。
311333
/// </summary>
@@ -507,7 +529,8 @@ public T LoadAsset<T>(string location, bool needInstance = true, bool needCache
507529
public T LoadAsset<T>(string location, Transform parent, bool needInstance = true, bool needCache = false,
508530
string customPackageName = "") where T : UnityEngine.Object
509531
{
510-
return m_ResourceManager.LoadAsset<T>(location, parent, needInstance, needCache, packageName: customPackageName);
532+
return m_ResourceManager.LoadAsset<T>(location, parent, needInstance, needCache,
533+
packageName: customPackageName);
511534
}
512535

513536
/// <summary>
@@ -538,7 +561,8 @@ public T LoadAsset<T>(string location, out AssetOperationHandle handle, bool nee
538561
public T LoadAsset<T>(string location, Transform parent, out AssetOperationHandle handle,
539562
bool needCache = false, string customPackageName = "") where T : UnityEngine.Object
540563
{
541-
return m_ResourceManager.LoadAsset<T>(location, parent, out handle, needCache, packageName: customPackageName);
564+
return m_ResourceManager.LoadAsset<T>(location, parent, out handle, needCache,
565+
packageName: customPackageName);
542566
}
543567

544568
/// <summary>
@@ -652,7 +676,8 @@ public async UniTask<List<T>> LoadAssetsByTagAsync<T>(string assetTag, string cu
652676
/// <typeparam name="T">要加载资源的类型。</typeparam>
653677
/// <returns>异步资源实例。</returns>
654678
public async UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default,
655-
bool needInstance = true, bool needCache = false, string customPackageName = "") where T : UnityEngine.Object
679+
bool needInstance = true, bool needCache = false, string customPackageName = "")
680+
where T : UnityEngine.Object
656681
{
657682
return await m_ResourceManager.LoadAssetAsync<T>(location, cancellationToken, needInstance, needCache,
658683
packageName: customPackageName);
@@ -699,7 +724,8 @@ public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform
699724
public async UniTask<RawFileOperationHandle> LoadRawAssetAsync(string location,
700725
CancellationToken cancellationToken = default, string customPackageName = "")
701726
{
702-
return await m_ResourceManager.LoadRawAssetAsync(location, cancellationToken, packageName: customPackageName);
727+
return await m_ResourceManager.LoadRawAssetAsync(location, cancellationToken,
728+
packageName: customPackageName);
703729
}
704730

705731
/// <summary>

0 commit comments

Comments
 (0)