拉取资源
This commit is contained in:
187
Assets/TcgEngine/Scripts/Tools/ResourcePathHelper.cs
Normal file
187
Assets/TcgEngine/Scripts/Tools/ResourcePathHelper.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using System.IO;
|
||||
#endif
|
||||
|
||||
namespace TcgEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 自动设置卡牌、头像、卡背的资源路径
|
||||
/// </summary>
|
||||
public class ResourcePathHelper : MonoBehaviour
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[MenuItem("TcgEngine/Tools/Setup Cards Data Paths")]
|
||||
public static void SetupCardsDataPaths()
|
||||
{
|
||||
string[] cardGuids = AssetDatabase.FindAssets("t:CardData");
|
||||
int updated = 0;
|
||||
|
||||
foreach (string guid in cardGuids)
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
CardData card = AssetDatabase.LoadAssetAtPath<CardData>(assetPath);
|
||||
|
||||
if (card != null)
|
||||
{
|
||||
bool changed = false;
|
||||
|
||||
card.art_full_path = $"Cards/{card.id}.png";
|
||||
Debug.Log($"Card {card.id}: set art_full_path = {card.art_full_path}");
|
||||
changed = true;
|
||||
|
||||
card.art_board_path = $"CardsBoard/{card.id}_board.png";
|
||||
Debug.Log($"Card {card.id}: set art_board_path = {card.art_board_path}");
|
||||
changed = true;
|
||||
|
||||
if (changed)
|
||||
{
|
||||
EditorUtility.SetDirty(card);
|
||||
updated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
[MenuItem("TcgEngine/Tools/Setup Avatar Data Paths")]
|
||||
public static void SetupAvatarDataPaths()
|
||||
{
|
||||
string[] avatarGuids = AssetDatabase.FindAssets("t:AvatarData");
|
||||
int updated = 0;
|
||||
|
||||
foreach (string guid in avatarGuids)
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
AvatarData avatar = AssetDatabase.LoadAssetAtPath<AvatarData>(assetPath);
|
||||
|
||||
if (avatar != null)
|
||||
{
|
||||
avatar.avatar_path = $"Avatar/{avatar.id}.png";
|
||||
Debug.Log($"Avatar {avatar.id}: set avatar_path = {avatar.avatar_path}");
|
||||
|
||||
EditorUtility.SetDirty(avatar);
|
||||
updated++;
|
||||
}
|
||||
}
|
||||
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
[MenuItem("TcgEngine/Tools/Setup Cardback Data Paths")]
|
||||
public static void SetupCardbackDataPaths()
|
||||
{
|
||||
string[] cardbackGuids = AssetDatabase.FindAssets("t:CardbackData");
|
||||
int updated = 0;
|
||||
|
||||
foreach (string guid in cardbackGuids)
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
CardbackData cardback = AssetDatabase.LoadAssetAtPath<CardbackData>(assetPath);
|
||||
|
||||
if (cardback != null)
|
||||
{
|
||||
bool changed = false;
|
||||
|
||||
int lastIndex = cardback.id.LastIndexOf('_');
|
||||
string cardbackId = "";
|
||||
|
||||
if (lastIndex != -1)
|
||||
{
|
||||
cardbackId = cardback.id.Substring(lastIndex + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
cardbackId = cardback.id;
|
||||
}
|
||||
|
||||
cardback.cardback_path = $"Cardbacks/cardback_{cardbackId}.png";
|
||||
Debug.Log($"Cardback {cardback.id} set cardback_path = {cardback.cardback_path}");
|
||||
changed = true;
|
||||
|
||||
cardback.deck_path = $"Cardbacks/deck_{cardbackId}.png";
|
||||
Debug.Log($"Cardback {cardback.id} set deck_path = {cardback.deck_path}");
|
||||
if (changed)
|
||||
{
|
||||
EditorUtility.SetDirty(cardback);
|
||||
updated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Unity资源路径转换为Sprites相对路径
|
||||
/// </summary>
|
||||
private static string ConvertUnityPathToSpritePath(string unityPath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(unityPath))
|
||||
return "";
|
||||
|
||||
// 查找Sprites文件夹在路径中的位置
|
||||
int spritesIndex = unityPath.IndexOf("Sprites/");
|
||||
if (spritesIndex >= 0)
|
||||
{
|
||||
// 提取Sprites/之后的部分
|
||||
string relativePath = unityPath.Substring(spritesIndex + "Sprites/".Length);
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
return Path.GetFileName(unityPath);
|
||||
}
|
||||
|
||||
[MenuItem("TcgEngine/Tools/Clear Dynamic Sprite Paths")]
|
||||
public static void ClearDynamicSpritePaths()
|
||||
{
|
||||
// 清除卡牌数据路径
|
||||
string[] cardGuids = AssetDatabase.FindAssets("t:CardData");
|
||||
foreach (string guid in cardGuids)
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
CardData card = AssetDatabase.LoadAssetAtPath<CardData>(assetPath);
|
||||
if (card != null)
|
||||
{
|
||||
card.art_full_path = "";
|
||||
card.art_board_path = "";
|
||||
EditorUtility.SetDirty(card);
|
||||
}
|
||||
}
|
||||
|
||||
// 清除头像数据路径
|
||||
string[] avatarGuids = AssetDatabase.FindAssets("t:AvatarData");
|
||||
foreach (string guid in avatarGuids)
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
AvatarData avatar = AssetDatabase.LoadAssetAtPath<AvatarData>(assetPath);
|
||||
if (avatar != null)
|
||||
{
|
||||
avatar.avatar_path = "";
|
||||
EditorUtility.SetDirty(avatar);
|
||||
}
|
||||
}
|
||||
|
||||
// 清除卡背数据路径
|
||||
string[] cardbackGuids = AssetDatabase.FindAssets("t:CardbackData");
|
||||
foreach (string guid in cardbackGuids)
|
||||
{
|
||||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
CardbackData cardback = AssetDatabase.LoadAssetAtPath<CardbackData>(assetPath);
|
||||
if (cardback != null)
|
||||
{
|
||||
cardback.cardback_path = "";
|
||||
cardback.deck_path = "";
|
||||
EditorUtility.SetDirty(cardback);
|
||||
}
|
||||
}
|
||||
|
||||
AssetDatabase.SaveAssets();
|
||||
Debug.Log("Clear Success");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
11
Assets/TcgEngine/Scripts/Tools/ResourcePathHelper.cs.meta
Normal file
11
Assets/TcgEngine/Scripts/Tools/ResourcePathHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 354fdcbafb88747fabb12a3d8e719722
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
321
Assets/TcgEngine/Scripts/Tools/SpriteLoader.cs
Normal file
321
Assets/TcgEngine/Scripts/Tools/SpriteLoader.cs
Normal file
@@ -0,0 +1,321 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TcgEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 动态Sprite加载器,支持从本地下载的资源中加载图片
|
||||
/// </summary>
|
||||
public class SpriteLoader : MonoBehaviour
|
||||
{
|
||||
private static SpriteLoader instance;
|
||||
private Dictionary<string, Sprite> spriteCache = new Dictionary<string, Sprite>();
|
||||
private Dictionary<string, Texture2D> textureCache = new Dictionary<string, Texture2D>();
|
||||
|
||||
public static SpriteLoader Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
GameObject go = new GameObject("SpriteLoader");
|
||||
instance = go.AddComponent<SpriteLoader>();
|
||||
DontDestroyOnLoad(go);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else if (instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从本地文件加载Sprite
|
||||
/// </summary>
|
||||
/// <param name="relativePath">相对于Sprites文件夹的路径</param>
|
||||
/// <returns>加载的Sprite,失败返回null</returns>
|
||||
public Sprite LoadSprite(string relativePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(relativePath))
|
||||
{
|
||||
Debug.LogWarning("[SpriteLoader] Empty relativePath provided");
|
||||
return null;
|
||||
}
|
||||
|
||||
Debug.Log($"[SpriteLoader] Loading sprite: {relativePath}");
|
||||
|
||||
// 检查缓存
|
||||
if (spriteCache.ContainsKey(relativePath))
|
||||
{
|
||||
Debug.Log($"[SpriteLoader] Found in cache: {relativePath}");
|
||||
return spriteCache[relativePath];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 先尝试从下载的本地资源加载
|
||||
string localPath = GetLocalSpritePath(relativePath);
|
||||
Debug.Log($"[SpriteLoader] Checking local path: {localPath}");
|
||||
|
||||
if (File.Exists(localPath))
|
||||
{
|
||||
Debug.Log($"[SpriteLoader] Local file exists, loading from: {localPath}");
|
||||
Sprite sprite = LoadSpriteFromFile(localPath);
|
||||
if (sprite != null)
|
||||
{
|
||||
Debug.Log($"[SpriteLoader] Successfully loaded from local file: {relativePath}");
|
||||
spriteCache[relativePath] = sprite;
|
||||
return sprite;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[SpriteLoader] Failed to load sprite from local file: {localPath}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[SpriteLoader] Local file does not exist: {localPath}");
|
||||
}
|
||||
|
||||
// 如果本地文件不存在,尝试从Resources加载(fallback)
|
||||
Debug.Log($"[SpriteLoader] Trying Resources fallback for: {relativePath}");
|
||||
Sprite fallbackSprite = LoadSpriteFromResources(relativePath);
|
||||
if (fallbackSprite != null)
|
||||
{
|
||||
Debug.Log($"[SpriteLoader] Successfully loaded from Resources: {relativePath}");
|
||||
spriteCache[relativePath] = fallbackSprite;
|
||||
return fallbackSprite;
|
||||
}
|
||||
|
||||
Debug.LogWarning($"[SpriteLoader] Failed to load sprite from both local and Resources: {relativePath}");
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[SpriteLoader] Error loading sprite {relativePath}: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件系统加载Sprite
|
||||
/// </summary>
|
||||
private Sprite LoadSpriteFromFile(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 加载纹理
|
||||
Texture2D texture = LoadTextureFromFile(filePath);
|
||||
if (texture == null)
|
||||
return null;
|
||||
|
||||
// 创建Sprite
|
||||
Sprite sprite = Sprite.Create(
|
||||
texture,
|
||||
new Rect(0, 0, texture.width, texture.height),
|
||||
new Vector2(0.5f, 0.5f)
|
||||
);
|
||||
|
||||
return sprite;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Error loading sprite from file {filePath}: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件加载纹理
|
||||
/// </summary>
|
||||
private Texture2D LoadTextureFromFile(string filePath)
|
||||
{
|
||||
if (textureCache.ContainsKey(filePath))
|
||||
{
|
||||
return textureCache[filePath];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
byte[] fileData = File.ReadAllBytes(filePath);
|
||||
Texture2D texture = new Texture2D(2, 2);
|
||||
|
||||
if (texture.LoadImage(fileData))
|
||||
{
|
||||
textureCache[filePath] = texture;
|
||||
return texture;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(texture);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Error loading texture from file {filePath}: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Resources文件夹加载Sprite(fallback)
|
||||
/// </summary>
|
||||
private Sprite LoadSpriteFromResources(string relativePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 移除文件扩展名
|
||||
string resourcePath = Path.ChangeExtension(relativePath, null);
|
||||
|
||||
// 尝试直接加载Sprite
|
||||
Sprite sprite = Resources.Load<Sprite>(resourcePath);
|
||||
if (sprite != null)
|
||||
return sprite;
|
||||
|
||||
// 如果直接加载失败,尝试加载Texture2D然后转换
|
||||
Texture2D texture = Resources.Load<Texture2D>(resourcePath);
|
||||
if (texture != null)
|
||||
{
|
||||
return Sprite.Create(
|
||||
texture,
|
||||
new Rect(0, 0, texture.width, texture.height),
|
||||
new Vector2(0.5f, 0.5f)
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Error loading sprite from resources {relativePath}: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取本地Sprite文件路径
|
||||
/// </summary>
|
||||
private string GetLocalSpritePath(string relativePath)
|
||||
{
|
||||
if (ResourceDownloader.Get() != null)
|
||||
{
|
||||
return ResourceDownloader.Get().GetLocalSpritePath(relativePath);
|
||||
}
|
||||
|
||||
// Fallback路径
|
||||
string basePath = Path.Combine(Application.persistentDataPath, "Sprites");
|
||||
return Path.Combine(basePath, relativePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理缓存
|
||||
/// </summary>
|
||||
public void ClearCache()
|
||||
{
|
||||
foreach (var sprite in spriteCache.Values)
|
||||
{
|
||||
if (sprite != null)
|
||||
Destroy(sprite);
|
||||
}
|
||||
spriteCache.Clear();
|
||||
|
||||
foreach (var texture in textureCache.Values)
|
||||
{
|
||||
if (texture != null)
|
||||
Destroy(texture);
|
||||
}
|
||||
textureCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 预加载指定目录下的所有图片
|
||||
/// </summary>
|
||||
public void PreloadSprites(string directory)
|
||||
{
|
||||
try
|
||||
{
|
||||
string localDir = GetLocalSpritePath(directory);
|
||||
if (!Directory.Exists(localDir))
|
||||
return;
|
||||
|
||||
string[] files = Directory.GetFiles(localDir, "*.png", SearchOption.AllDirectories);
|
||||
foreach (string file in files)
|
||||
{
|
||||
string relativePath = GetRelativePath(file);
|
||||
LoadSprite(relativePath);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Error preloading sprites from {directory}: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取相对路径
|
||||
/// </summary>
|
||||
private string GetRelativePath(string fullPath)
|
||||
{
|
||||
string spritesPath = GetLocalSpritePath("");
|
||||
if (fullPath.StartsWith(spritesPath))
|
||||
{
|
||||
return fullPath.Substring(spritesPath.Length + 1);
|
||||
}
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查Sprite是否存在
|
||||
/// </summary>
|
||||
public bool SpriteExists(string relativePath)
|
||||
{
|
||||
if (spriteCache.ContainsKey(relativePath))
|
||||
return true;
|
||||
|
||||
string localPath = GetLocalSpritePath(relativePath);
|
||||
if (File.Exists(localPath))
|
||||
return true;
|
||||
|
||||
// 检查Resources
|
||||
string resourcePath = Path.ChangeExtension(relativePath, null);
|
||||
Sprite sprite = Resources.Load<Sprite>(resourcePath);
|
||||
if (sprite != null)
|
||||
{
|
||||
Resources.UnloadAsset(sprite);
|
||||
return true;
|
||||
}
|
||||
|
||||
Texture2D texture = Resources.Load<Texture2D>(resourcePath);
|
||||
if (texture != null)
|
||||
{
|
||||
Resources.UnloadAsset(texture);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ClearCache();
|
||||
}
|
||||
|
||||
public static SpriteLoader Get()
|
||||
{
|
||||
return Instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/TcgEngine/Scripts/Tools/SpriteLoader.cs.meta
Normal file
11
Assets/TcgEngine/Scripts/Tools/SpriteLoader.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a6288ab659a946d2a8819eb4d8fbb41
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user