Files
test/Assets/PerfectWorld/Scripts/Editor/AnimScenePlayerBootstrapEditor.cs
T
2026-05-19 10:46:26 +07:00

156 lines
6.3 KiB
C#

#if UNITY_EDITOR
using BrewMonster;
using BrewMonster.Scripts;
using UnityEditor;
using UnityEngine;
namespace PerfectWorld.Scripts
{
[CustomEditor(typeof(AnimScenePlayerBootstrap))]
internal sealed class AnimScenePlayerBootstrapEditor : UnityEditor.Editor
{
private Profession _editorSwitchProfession = Profession.PROF_MAGE;
private Gender _editorSwitchGender = Gender.GENDER_FEMALE;
private int _playActionIndex;
private bool _playActionRestart = true;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
EditorGUILayout.Space(8f);
EditorGUILayout.LabelField("Play Mode — switch model", EditorStyles.boldLabel);
EditorGUILayout.HelpBox(
"Enter Play Mode, pick profession / gender below, then click Switch model. " +
"This calls SwitchRole on this component (same as code).",
MessageType.Info);
using (new EditorGUI.DisabledScope(!Application.isPlaying))
{
_editorSwitchProfession =
(Profession)EditorGUILayout.EnumPopup("Switch to profession", _editorSwitchProfession);
_editorSwitchGender =
(Gender)EditorGUILayout.EnumPopup("Switch to gender", _editorSwitchGender);
}
using (new EditorGUI.DisabledScope(!Application.isPlaying))
{
if (GUILayout.Button("Switch model", GUILayout.Height(28f)))
{
var boot = (AnimScenePlayerBootstrap)target;
boot.SwitchRole(_editorSwitchProfession, _editorSwitchGender);
}
}
EditorGUILayout.Space(8f);
EditorGUILayout.LabelField("Play Mode — play action", EditorStyles.boldLabel);
EditorGUILayout.HelpBox(
"Calls CECHostPlayer.PlayAction(index, restart). ACT_ATTACK_1..4 may still be blocked by design — " +
"use combat/skill paths for those.",
MessageType.Info);
serializedObject.Update();
SerializedProperty playerProp = serializedObject.FindProperty("player");
var host = playerProp != null ? playerProp.objectReferenceValue as CECHostPlayer : null;
using (new EditorGUI.DisabledScope(!Application.isPlaying))
{
_playActionIndex = EditorGUILayout.IntField("Action index (PLAYER_ACTION_TYPE)", _playActionIndex);
_playActionRestart = EditorGUILayout.Toggle("Restart", _playActionRestart);
if (host != null && Application.isPlaying)
{
string preflight = host.AnimSceneTryExplainPlayActionFailure(_playActionIndex);
if (!string.IsNullOrEmpty(preflight))
{
EditorGUILayout.HelpBox(preflight, MessageType.Warning);
}
}
EditorGUILayout.BeginHorizontal();
using (new EditorGUI.DisabledScope(!Application.isPlaying || host == null))
{
if (GUILayout.Button("Stand (0)", GUILayout.Height(22f)))
{
_playActionIndex = (int)PLAYER_ACTION_TYPE.ACT_STAND;
TryPlayActionOnHost((AnimScenePlayerBootstrap)target, host, _playActionIndex, _playActionRestart);
}
if (GUILayout.Button("Fight stand (1)", GUILayout.Height(22f)))
{
_playActionIndex = (int)PLAYER_ACTION_TYPE.ACT_FIGHTSTAND;
TryPlayActionOnHost((AnimScenePlayerBootstrap)target, host, _playActionIndex, _playActionRestart);
}
if (GUILayout.Button("Run (3)", GUILayout.Height(22f)))
{
_playActionIndex = (int)PLAYER_ACTION_TYPE.ACT_RUN;
TryPlayActionOnHost((AnimScenePlayerBootstrap)target, host, _playActionIndex, _playActionRestart);
}
}
EditorGUILayout.EndHorizontal();
using (new EditorGUI.DisabledScope(host == null))
{
if (GUILayout.Button("Play action", GUILayout.Height(28f)))
{
TryPlayActionOnHost((AnimScenePlayerBootstrap)target, host, _playActionIndex, _playActionRestart);
}
}
}
EditorGUILayout.Space(8f);
EditorGUILayout.LabelField("Play Mode — weapon", EditorStyles.boldLabel);
using (new EditorGUI.DisabledScope(!Application.isPlaying))
{
if (GUILayout.Button("Re-apply active weapon slot", GUILayout.Height(28f)))
{
var boot = (AnimScenePlayerBootstrap)target;
boot.ApplyWeaponForActiveSlot();
}
}
}
private static void TryPlayActionOnHost(
AnimScenePlayerBootstrap boot,
CECHostPlayer host,
int index,
bool restart)
{
if (boot == null || host == null)
{
return;
}
boot.AnimSceneEnsureFashionPathSafeBeforePlayAction(host);
string reason = host.AnimSceneTryExplainPlayActionFailure(index);
if (reason != null && reason.IndexOf("m_PlayerActions is null", System.StringComparison.Ordinal) >= 0)
{
host.AnimSceneRebindPlayerActions();
reason = host.AnimSceneTryExplainPlayActionFailure(index);
}
if (!string.IsNullOrEmpty(reason))
{
Debug.LogWarning("[AnimSceneBootstrap] Play action skipped: " + reason);
return;
}
bool ok = host.PlayAction(index, restart);
if (!ok)
{
Debug.LogWarning("[AnimSceneBootstrap] PlayAction(" + index + ") returned false.");
}
else
{
Debug.Log(
"[AnimSceneBootstrap] PlayAction(" + index + ") called. If you see no motion, " +
"CECModel.PlayActionByName may not have matched a clip on the loaded CombinedActionSO / Animancer.");
}
}
}
}
#endif