Skip to content

Commit 4385123

Browse files
committed
add:设置图片资源支持从指定资源包中加载
1 parent 1334dc3 commit 4385123

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public interface IResourceManager
8686
/// <summary>
8787
/// 初始化操作。
8888
/// </summary>
89-
/// <param name="packageName">指定资源包的名称。不传使用默认资源包</param>
89+
/// <param name="customPackageName">指定资源包的名称。不传使用默认资源包</param>
9090
/// <returns></returns>
91-
InitializationOperation InitPackage(string packageName = "");
91+
InitializationOperation InitPackage(string customPackageName = "");
9292

9393
/// <summary>
9494
/// 卸载资源。

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,12 @@ public void Initialize()
332332
/// 初始化资源包裹。
333333
/// </summary>
334334
/// <returns>初始化资源包裹操作句柄。</returns>
335-
public InitializationOperation InitPackage(string packageName = "")
335+
public InitializationOperation InitPackage(string customPackageName = "")
336336
{
337337
// 创建默认的资源包
338-
var targetPackageName = string.IsNullOrEmpty(packageName) || packageName.Equals(PackageName)
338+
var targetPackageName = string.IsNullOrEmpty(customPackageName) || customPackageName.Equals(PackageName)
339339
? PackageName
340-
: packageName;
340+
: customPackageName;
341341
var package = YooAssets.TryGetPackage(targetPackageName);
342342
if (package == null)
343343
{

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

Lines changed: 11 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>
@@ -528,7 +529,8 @@ public T LoadAsset<T>(string location, bool needInstance = true, bool needCache
528529
public T LoadAsset<T>(string location, Transform parent, bool needInstance = true, bool needCache = false,
529530
string customPackageName = "") where T : UnityEngine.Object
530531
{
531-
return m_ResourceManager.LoadAsset<T>(location, parent, needInstance, needCache, packageName: customPackageName);
532+
return m_ResourceManager.LoadAsset<T>(location, parent, needInstance, needCache,
533+
packageName: customPackageName);
532534
}
533535

534536
/// <summary>
@@ -559,7 +561,8 @@ public T LoadAsset<T>(string location, out AssetOperationHandle handle, bool nee
559561
public T LoadAsset<T>(string location, Transform parent, out AssetOperationHandle handle,
560562
bool needCache = false, string customPackageName = "") where T : UnityEngine.Object
561563
{
562-
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);
563566
}
564567

565568
/// <summary>
@@ -673,7 +676,8 @@ public async UniTask<List<T>> LoadAssetsByTagAsync<T>(string assetTag, string cu
673676
/// <typeparam name="T">要加载资源的类型。</typeparam>
674677
/// <returns>异步资源实例。</returns>
675678
public async UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default,
676-
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
677681
{
678682
return await m_ResourceManager.LoadAssetAsync<T>(location, cancellationToken, needInstance, needCache,
679683
packageName: customPackageName);
@@ -720,7 +724,8 @@ public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform
720724
public async UniTask<RawFileOperationHandle> LoadRawAssetAsync(string location,
721725
CancellationToken cancellationToken = default, string customPackageName = "")
722726
{
723-
return await m_ResourceManager.LoadRawAssetAsync(location, cancellationToken, packageName: customPackageName);
727+
return await m_ResourceManager.LoadRawAssetAsync(location, cancellationToken,
728+
packageName: customPackageName);
724729
}
725730

726731
/// <summary>

0 commit comments

Comments
 (0)