From 89a61a678da41b7e256c6dcb84f141a8935a8130 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Tue, 31 May 2022 20:23:37 +0800 Subject: [PATCH] Update Utils.cs --- Assets/TEngine/Runtime/Unitity/Utils.cs | 67 +++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/Assets/TEngine/Runtime/Unitity/Utils.cs b/Assets/TEngine/Runtime/Unitity/Utils.cs index 9488e009..33b78890 100644 --- a/Assets/TEngine/Runtime/Unitity/Utils.cs +++ b/Assets/TEngine/Runtime/Unitity/Utils.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.UI; @@ -125,7 +126,7 @@ namespace TEngine /// 置空时传入path为null public static void SetSprite(this UnityEngine.UI.Image image, string path, bool bAsync = false) { - LoadAsset(image, path, 0, bAsync); + LoadAsset(image, path, 0); } /// @@ -135,14 +136,72 @@ namespace TEngine /// Sprite路径,通过右键菜单Get Asset Path获取的路径 /// 是否异步加载 /// 置空时传入path为null - public static void SetSprite(this SpriteRenderer spriteRenderer, string path, bool bAsync = false) + public static void SetSprite(this SpriteRenderer spriteRenderer, string path) { - LoadAsset(spriteRenderer, path, 0, bAsync); + LoadAsset(spriteRenderer, path, 0); } - static void LoadAsset(Component component, string path, int index, bool bAsync) where T : UnityEngine.Object + static void LoadAsset(Component component, string path, int index) where T : UnityEngine.Object { //TODO + + if (string.IsNullOrEmpty(path)) + { + if (component as Image != null) + { + var image = (Image)component; + image.sprite = null; + } + else if(component as SpriteRenderer != null) + { + var image = (SpriteRenderer)component; + image.sprite = null; + } + } + else + { + int splitIndex = path.LastIndexOf('#'); + string resPath; + bool bWithSubAssets; + string subAssetName; + if (splitIndex > 0) + { + resPath = path.Substring(0, splitIndex); + subAssetName = path.Substring(splitIndex + 1); + bWithSubAssets = true; + } + else + { + resPath = path; + if (component.GetType() == typeof(Sprite)) + { + bWithSubAssets = true; + subAssetName = Path.GetFileNameWithoutExtension(path); + } + else + { + bWithSubAssets = false; + } + } + + var asset = ResMgr.Instance.GetAsset(resPath, bWithSubAssets); + + if (asset == null) + { + return; + } + + if (component.GetType() == typeof(Image)) + { + var image = (Image)component; + image.sprite = asset.AssetObject as Sprite; + } + else if (component as SpriteRenderer != null) + { + var image = (SpriteRenderer)component; + image.sprite = asset.AssetObject as Sprite; + } + } } #endregion