拉取资源
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user