145 lines
5.9 KiB
C#
145 lines
5.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 机械蝎八连抽配置生成器(编辑器工具)。
|
||
///
|
||
/// 用法:菜单 Tools → 机械蝎 → 生成八连抽配置资产
|
||
/// 1. 自动查找项目中 PW_Effect_FreeDropChip / PW_Effect_Settle_Scorpion 预制体
|
||
/// 2. 自动查找 PW_FreeItem_DropGemstone* / DropCard* / DropProp* Sprite,按文件名数字排序填入
|
||
/// 3. 生成 JiXieXieConfig.asset 到 JiXieXie 目录(已存在则覆盖并刷新)
|
||
/// 4. 选中生成的资产,拖到爆奖预制体的 JiXieXieEvent.config 字段即可
|
||
/// </summary>
|
||
public static class JiXieXieConfigAutoFill
|
||
{
|
||
private const string ConfigDir = "Assets/MyGame/aa/Game/XJMH/Boss/JiXieXie";
|
||
private const string ConfigPath = ConfigDir + "/JiXieXieConfig.asset";
|
||
|
||
[MenuItem("Tools/机械蝎/生成八连抽配置资产")]
|
||
public static void GenerateConfig()
|
||
{
|
||
// 1. 查找预制体
|
||
GameObject chipPrefab = FindAsset<GameObject>("PW_Effect_FreeDropChip");
|
||
GameObject settlePrefab = FindAsset<GameObject>("PW_Effect_Settle_Scorpion");
|
||
|
||
// 2. 查找图片(按名字数字排序)
|
||
Sprite[] gemstones = FindSpritesSorted("PW_FreeItem_DropGemstone");
|
||
Sprite[] cards = FindSpritesSorted("PW_FreeItem_DropCard");
|
||
Sprite[] goods = FindSpritesSorted("PW_FreeItem_DropProp");
|
||
|
||
if (chipPrefab == null)
|
||
Debug.LogWarning("[JiXieXie] 未找到 PW_Effect_FreeDropChip 预制体,config.freeDropChipPrefab 为空");
|
||
if (settlePrefab == null)
|
||
Debug.LogWarning("[JiXieXie] 未找到 PW_Effect_Settle_Scorpion 预制体,config.settleScorpionPrefab 为空");
|
||
if (gemstones.Length == 0 || cards.Length == 0 || goods.Length == 0)
|
||
Debug.LogWarning($"[JiXieXie] 部分图片没找到:宝石={gemstones.Length} 卡牌={cards.Length} 道具={goods.Length}");
|
||
|
||
// 3. 生成/覆盖配置资产
|
||
JiXieXieConfig cfg = AssetDatabase.LoadAssetAtPath<JiXieXieConfig>(ConfigPath);
|
||
bool isNew = cfg == null;
|
||
if (isNew)
|
||
{
|
||
cfg = ScriptableObject.CreateInstance<JiXieXieConfig>();
|
||
EnsureDir();
|
||
AssetDatabase.CreateAsset(cfg, ConfigPath);
|
||
}
|
||
|
||
cfg.freeDropChipPrefab = chipPrefab;
|
||
cfg.settleScorpionPrefab = settlePrefab;
|
||
cfg.gemstoneSprites = gemstones;
|
||
cfg.cardSprites = cards;
|
||
cfg.goodsSprites = goods;
|
||
cfg.propNames = BuildPropNames(goods);
|
||
|
||
EditorUtility.SetDirty(cfg);
|
||
AssetDatabase.SaveAssets();
|
||
AssetDatabase.Refresh();
|
||
|
||
// 4. 选中并提示
|
||
Selection.activeObject = cfg;
|
||
EditorGUIUtility.PingObject(cfg);
|
||
Debug.Log($"[JiXieXie] 配置资产{(isNew ? "已生成" : "已更新")}:{ConfigPath}\n" +
|
||
$" 宝石={cfg.gemstoneSprites.Length} 卡牌={cfg.cardSprites.Length} 道具={cfg.goodsSprites.Length}\n" +
|
||
" 请把该资产拖到爆奖预制体 JiXieXieEvent 的 config 字段");
|
||
}
|
||
|
||
// ==================== 工具方法 ====================
|
||
|
||
private static void EnsureDir()
|
||
{
|
||
if (!AssetDatabase.IsValidFolder(ConfigDir))
|
||
{
|
||
string parent = "Assets/MyGame/aa/Game/XJMH/Boss";
|
||
if (!AssetDatabase.IsValidFolder(parent))
|
||
{
|
||
Debug.LogError($"[JiXieXie] 目录不存在:{parent}");
|
||
return;
|
||
}
|
||
AssetDatabase.CreateFolder(parent, "JiXieXie");
|
||
}
|
||
}
|
||
|
||
/// <summary>按名称模糊查找第一个指定类型资产</summary>
|
||
private static T FindAsset<T>(string nameFilter) where T : UnityEngine.Object
|
||
{
|
||
string[] guids = AssetDatabase.FindAssets(nameFilter + " t:" + typeof(T).Name);
|
||
foreach (string guid in guids)
|
||
{
|
||
string path = AssetDatabase.GUIDToAssetPath(guid);
|
||
T asset = AssetDatabase.LoadAssetAtPath<T>(path);
|
||
if (asset != null) return asset;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>查找名称带前缀的所有 Sprite,并按文件名末尾数字排序</summary>
|
||
private static Sprite[] FindSpritesSorted(string namePrefix)
|
||
{
|
||
string[] guids = AssetDatabase.FindAssets(namePrefix + " t:Sprite");
|
||
List<Sprite> list = new List<Sprite>();
|
||
foreach (string guid in guids)
|
||
{
|
||
string path = AssetDatabase.GUIDToAssetPath(guid);
|
||
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
|
||
if (sprite != null)
|
||
list.Add(sprite);
|
||
}
|
||
|
||
// 按文件名末尾数字排序:DropGemstone1 < DropGemstone2 ... < DropGemstone10
|
||
list.Sort((a, b) => ExtractTrailingNumber(a.name).CompareTo(ExtractTrailingNumber(b.name)));
|
||
return list.ToArray();
|
||
}
|
||
|
||
/// <summary>提取文件名末尾的数字(如 "PW_FreeItem_DropProp1000" → 1000)</summary>
|
||
private static int ExtractTrailingNumber(string name)
|
||
{
|
||
Match m = Regex.Match(name, @"(\d+)$");
|
||
return m.Success ? int.Parse(m.Value) : 0;
|
||
}
|
||
|
||
/// <summary>按 goods 数量生成默认道具名(1000+ 猜测为火箭炮碎片,其余叫 道具{ID})</summary>
|
||
private static string[] BuildPropNames(Sprite[] goods)
|
||
{
|
||
if (goods == null || goods.Length == 0) return new string[0];
|
||
|
||
string[] names = new string[goods.Length];
|
||
for (int i = 0; i < goods.Length; i++)
|
||
{
|
||
if (goods[i] == null) { names[i] = ""; continue; }
|
||
int id = ExtractTrailingNumber(goods[i].name);
|
||
if (id >= 1000 && id <= 1003)
|
||
names[i] = "火箭炮碎片";
|
||
else if (id == 0)
|
||
names[i] = "金币";
|
||
else if (id == 1)
|
||
names[i] = "钻石";
|
||
else
|
||
names[i] = $"道具{id}";
|
||
}
|
||
return names;
|
||
}
|
||
}
|