156 lines
5.0 KiB
C#
156 lines
5.0 KiB
C#
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||
using System;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.SceneManagement;
|
||
using TMPro;
|
||
using BaseGames.Core;
|
||
using BaseGames.Core.Events;
|
||
using BaseGames.Player;
|
||
using BaseGames.Player.States;
|
||
using BaseGames.Enemies;
|
||
using BaseGames.Combat;
|
||
|
||
/// <summary>
|
||
/// 调试作弊控制台(仅 UNITY_EDITOR 或 DEVELOPMENT_BUILD 下编译)。
|
||
/// 按反引号键(`)呼出/隐藏输入框,输入指令后按 Enter 执行。
|
||
/// 指令格式:指令名 [参数1] [参数2] ...
|
||
/// </summary>
|
||
public class DebugCheatSystem : MonoBehaviour
|
||
{
|
||
[Header("UI 引用")]
|
||
[SerializeField] private GameObject _consoleRoot;
|
||
[SerializeField] private TMP_InputField _inputField;
|
||
[SerializeField] private TMP_Text _outputText;
|
||
|
||
[Header("事件频道")]
|
||
[SerializeField] private VoidEventChannelSO _onPlayerRevive;
|
||
|
||
private bool _visible;
|
||
|
||
private void Update()
|
||
{
|
||
if (UnityEngine.Input.GetKeyDown(KeyCode.BackQuote))
|
||
Toggle();
|
||
|
||
if (_visible && _inputField != null && UnityEngine.Input.GetKeyDown(KeyCode.Return))
|
||
ExecuteCommand(_inputField.text);
|
||
}
|
||
|
||
private void Toggle()
|
||
{
|
||
_visible = !_visible;
|
||
if (_consoleRoot != null) _consoleRoot.SetActive(_visible);
|
||
if (_visible && _inputField != null)
|
||
{
|
||
_inputField.text = string.Empty;
|
||
_inputField.Select();
|
||
_inputField.ActivateInputField();
|
||
}
|
||
}
|
||
|
||
private void ExecuteCommand(string raw)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(raw)) return;
|
||
_inputField.text = string.Empty;
|
||
_inputField.ActivateInputField();
|
||
|
||
string[] parts = raw.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||
if (parts.Length == 0) return;
|
||
|
||
string cmd = parts[0].ToLowerInvariant();
|
||
string result;
|
||
|
||
try
|
||
{
|
||
result = cmd switch
|
||
{
|
||
"help" => "指令: heal | addgeo [n] | godmode | ungodmode | unlock [ability] | killall | scene [name] | revive",
|
||
"heal" => CmdHeal(),
|
||
"addgeo" => CmdAddGeo(parts),
|
||
"godmode" => CmdGodMode(true),
|
||
"ungodmode" => CmdGodMode(false),
|
||
"unlock" => CmdUnlock(parts),
|
||
"killall" => CmdKillAll(),
|
||
"scene" => CmdLoadScene(parts),
|
||
"revive" => CmdRevive(),
|
||
_ => $"未知指令: {cmd}",
|
||
};
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
result = $"[错误] {ex.Message}";
|
||
}
|
||
|
||
Log(result);
|
||
}
|
||
|
||
// ── 指令实现 ──────────────────────────────────────────────────────────────
|
||
private string CmdHeal()
|
||
{
|
||
var player = FindFirstObjectByType<PlayerController>();
|
||
if (player == null) return "找不到 PlayerController";
|
||
player.Stats.FullHeal();
|
||
return "HP 已满";
|
||
}
|
||
|
||
private string CmdAddGeo(string[] parts)
|
||
{
|
||
int amount = parts.Length > 1 && int.TryParse(parts[1], out int v) ? v : 100;
|
||
var player = FindFirstObjectByType<PlayerController>();
|
||
if (player == null) return "找不到 PlayerController";
|
||
player.Stats.AddGeo(amount);
|
||
return $"增加 Geo: {amount}";
|
||
}
|
||
|
||
private string CmdGodMode(bool on)
|
||
{
|
||
var player = FindFirstObjectByType<PlayerController>();
|
||
if (player == null) return "找不到 PlayerController";
|
||
player.Stats.SetGodMode(on);
|
||
return on ? "无敌模式 ON" : "无敌模式 OFF";
|
||
}
|
||
|
||
private string CmdUnlock(string[] parts)
|
||
{
|
||
if (parts.Length < 2) return "用法: unlock [AbilityType]";
|
||
if (!Enum.TryParse<AbilityType>(parts[1], true, out var abilityType))
|
||
return $"未知能力: {parts[1]}(可用:{string.Join(", ", Enum.GetNames(typeof(AbilityType)))})";
|
||
var player = FindFirstObjectByType<PlayerController>();
|
||
if (player == null) return "找不到 PlayerController";
|
||
player.Stats.UnlockAbility(abilityType);
|
||
return $"解锁能力: {abilityType}";
|
||
}
|
||
|
||
private string CmdKillAll()
|
||
{
|
||
var enemies = FindObjectsByType<EnemyBase>(FindObjectsSortMode.None);
|
||
var dmg = new DamageInfo.Builder().SetRaw(999999).Build();
|
||
foreach (var e in enemies) e.TakeDamage(dmg);
|
||
return $"已击杀 {enemies.Length} 个敌人";
|
||
}
|
||
|
||
private string CmdLoadScene(string[] parts)
|
||
{
|
||
if (parts.Length < 2) return "用法: scene [SceneName]";
|
||
SceneManager.LoadScene(parts[1]);
|
||
return $"加载场景: {parts[1]}";
|
||
}
|
||
|
||
private string CmdRevive()
|
||
{
|
||
_onPlayerRevive?.Raise();
|
||
return "已触发玩家复活事件";
|
||
}
|
||
|
||
private void Log(string msg)
|
||
{
|
||
Debug.Log($"[Cheat] {msg}");
|
||
if (_outputText != null)
|
||
{
|
||
_outputText.text = $"> {msg}";
|
||
}
|
||
}
|
||
}
|
||
#endif
|