NLDClient-yudde/ProjectNLD/Assets/Art/temp/Editor/PrefabTool/PrefabBuilderWindow.cs

213 lines
8.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Art.temp.Editor.AnimationTools;
using artcfg.ArtDesign;
using Framework;
using UnityEditor;
using UnityEditor.Animations;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace Art.temp.Editor.PrefabTool
{
public class PrefabBuilderWin : EditorWindow
{
[MenuItem("Tools/*角色工具/Prefab 制作工具")]
public static void ShowWindow()
{
GetWindow<PrefabBuilderWin>("Prefab 制作工具");
}
public void CreateGUI()
{
var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Art/temp/Editor/CommonUSSFile.uss");
rootVisualElement.styleSheets.Add(styleSheet);
var toolbar = new Toolbar();
var refreshBtn = new ToolbarButton(ArtTableManager.instance.ForceReload) { text = "刷新" };
toolbar.Add(refreshBtn);
rootVisualElement.Add(toolbar);
Label label1 = new() { text = "选择模型Fbx创建prefab" };
rootVisualElement.Add(label1);
Button btn1 = new() { text = "apply" };
rootVisualElement.Add(btn1);
btn1.RegisterCallback<ClickEvent>(_ => CreatePrefab());
Label label3 = new() { text = "创建所有展示用载具prefab" };
rootVisualElement.Add(label3);
Button btn2 = new() { text = "apply" };
rootVisualElement.Add(btn2);
btn2.RegisterCallback<ClickEvent>(_ => CreateDisplayVehicle());
/*Button btn4 = new() { text = "battle story character" };
rootVisualElement.Add(btn4);
btn4.RegisterCallback<ClickEvent>(_ => CreateBattleCharacter());*/
/*Button test = new() { text = "combine skinned mesh" };
rootVisualElement.Add(test);
test.RegisterCallback<ClickEvent>(_ => Combine());*/
Label label2 = new() { text = "---------------------------" };
rootVisualElement.Add(label2);
Button show = new() { text = "将所有选择的prefab排列在场景中" };
rootVisualElement.Add(show);
show.RegisterCallback<ClickEvent>(_ => ShowInScene());
}
private static void CreatePrefab()
{
foreach (GameObject go in Selection.gameObjects)
{
string id = go.name;
if (id.StartsWith("Build"))
{
FightBuildInfo fightBuildInfo = new(id);
fightBuildInfo.Rebuild();
}
else if (id.StartsWith("G"))
{
GunInfo gun = new(id);
gun.Rebuild();
}
else if (id.StartsWith("R"))
{
AnimHelper.PreSetModel(go);
BatteryInfo battery = new(id);
battery.Rebuild();
battery.RebuildAuto();
}
else if (id.StartsWith("C"))
{
AnimHelper.PreSetModel(go);
VehicleInfo car = new(id);
car.Rebuild();
}
else if (id.StartsWith("A"))
{
CharacterFactory.CreatePlayer(id).Rebuild();
}
else if (id.StartsWith("E"))
{
CharacterFactory.CreateEmeny(id).Rebuild();
}
else if (id.StartsWith("N"))
{
CharacterFactory.CreateNpc(id).Rebuild();
}
else if (id.StartsWith("B"))
{
BagInfo bag = new(id);
bag.Rebuild();
}
else
{
Debug.LogWarning("Invalid file name");
}
}
AssetDatabase.Refresh();
}
// 使用 json 配置生成载具 300001
private static void CreateDisplayVehicle()
{
var config = ArtTableManager.instance.tables.VehicleConfig.DataList;
foreach (DataVehicle v in config)
{
if (string.IsNullOrEmpty(v.NameId) || string.IsNullOrEmpty(v.Car) ||
string.IsNullOrEmpty(v.Buttery))
{
Debug.LogWarning($"{v.NameId}: 配置不全");
continue;
}
GameObject carAsset = AssetDatabase.LoadAssetAtPath<GameObject>(
$"Assets/Art/Character/Prefabs/Vehicle/{v.Car}.prefab"); // P_C00001_01
GameObject batteryAsset = AssetDatabase.LoadAssetAtPath<GameObject>(
$"Assets/Art/Character/Prefabs/Battery/{v.Buttery}.prefab"); // P_R00001_01
if (carAsset == null || batteryAsset == null)
{
Debug.LogWarning($"{v.NameId}: 缺少底盘或炮台 prefab");
continue;
}
string nameId = v.Car.Split('_')[1];
GameObject prefab = (GameObject)PrefabUtility.InstantiatePrefab(carAsset);
// set battery
Transform pointBattery =
prefab.transform.GetChild(0).Find($"Root/G_{nameId}_bone001/Battery_point01");
if (pointBattery == null)
pointBattery = prefab.transform.GetChild(0).Find("Root/cog/Battery_point01");
PrefabUtility.InstantiatePrefab(batteryAsset, pointBattery);
// set animator
AnimatorOverrideController overrideController = new(AssetDatabase.LoadAssetAtPath<AnimatorController>(
"Assets/Art/Animations/animator/Vehicle/animator_vehicle.controller"));
VehicleInfo info = new(nameId);
foreach (string anim in info.anims)
{
overrideController[$"ani_C00001_D_{anim}"] = AssetDatabase.LoadAssetAtPath<AnimationClip>(
$"Assets/Art/Animations/compressed/compressed_ani_{nameId}_D_{anim}.anim");
}
AssetDatabase.CreateAsset(overrideController,
$"Assets/Art/Character/Prefabs/Vehicle_S/ov_vehicle_{v.NameId}.overrideController");
AssetDatabase.SaveAssets();
var instance = prefab.transform.GetChild(0).gameObject;
var animator = instance.GetComponent<Animator>() ?? instance.AddComponent<Animator>();
animator.runtimeAnimatorController = overrideController;
PrefabUtility.UnpackPrefabInstance(prefab, PrefabUnpackMode.Completely,
InteractionMode.AutomatedAction);
string path = $"Assets/Art/Character/Prefabs/Vehicle_S/{v.NameId}.prefab";
PrefabUtility.SaveAsPrefabAssetAndConnect(prefab, path, InteractionMode.AutomatedAction);
prefab.name = v.NameId;
Debug.Log(path);
}
}
// 战斗角色 由 player npc 等创建,已废弃
private static void CreateBattleCharacter()
{
var battleCharacterData = EditorTableManager.instance.tables.FightNPCConfig.DataList;
foreach (var battleCharacter in battleCharacterData)
{
string id = battleCharacter.NameId;
string modelNameId = battleCharacter.NameRead.Split("_")[0];
if (id.StartsWith("BN"))
{
// 找到 display prefab 角色
GameObject oldPrefab = AnimHelper.FindDisplayPrefab(modelNameId);
GameObject prefab = (GameObject)PrefabUtility.InstantiatePrefab(oldPrefab);
// 重命名
prefab.name = "P_" + id;
// 保存 prefab
PrefabUtility.UnpackPrefabInstance(prefab, PrefabUnpackMode.OutermostRoot,
InteractionMode.AutomatedAction);
string path = $"Assets/Art/Character/Prefabs/Npc/{prefab.name}.prefab";
PrefabUtility.SaveAsPrefabAssetAndConnect(prefab, path, InteractionMode.AutomatedAction);
Debug.Log($"创建战斗剧情角色:{prefab}");
}
}
}
private static void ShowInScene()
{
var objects = Selection.gameObjects;
for (int i = 0; i < objects.Length; i++)
{
GameObject prefab = (GameObject)PrefabUtility.InstantiatePrefab(objects[i]);
prefab.transform.localPosition = new Vector3(-i, 0, 0);
}
}
}
}