NLDClient-yudde/ProjectNLD/Assets/Editor/FightDebug/SingleUnitWindow.cs

201 lines
7.1 KiB
C#

using System;
using cfg.LevelCfg;
using System.Collections.Generic;
using Gameplay.Area;
using Gameplay.Buff;
using Gameplay.Level;
using Gameplay.Unit;
using Gameplay.Unit.Data;
using UnityEngine;
using UnityEditor;
namespace FightDebug
{
public class SingleUnitWindow : EditorWindow
{
private Vector2 _scrollPos1;
private GameUnit _targetUnit;
public static void ShowWindow(GameUnit unit)
{
var win = GetWindow<SingleUnitWindow>("单位:" + unit.GetDescString());
win._targetUnit = unit;
win.Show();
win.Focus();
}
private bool _CheckCanShow()
{
if (!Application.isPlaying)
{
return false;
}
if (LevelManager.Instance == null)
{
return false;
}
if (GameUnitManager.instance == null)
{
return false;
}
return true;
}
private void _ShowNotInFight()
{
EditorGUILayout.HelpBox("仅支持在开战后使用", MessageType.Error);
}
private void _ShowLabel(string label, string value)
{
GUILayout.BeginHorizontal();
GUILayout.Label(label, GUILayout.Width(80));
GUILayout.Label(value);
GUILayout.EndHorizontal();
}
private void _ShowIcon(string iconPath)
{
GUILayout.BeginHorizontal();
GUILayout.Label("头像:", GUILayout.Width(60));
GUILayout.Label(new GUIContent(_LoadIcon(iconPath)), GUILayout.Width(100), GUILayout.Height(100));
GUILayout.EndHorizontal();
}
private Texture2D _LoadIcon(string iconPath)
{
const string defaultIconPath = "Assets/_Test/Texture/no_pic.png";
if (string.IsNullOrEmpty(iconPath))
{
return AssetDatabase.LoadAssetAtPath<Texture2D>(defaultIconPath);
}
if (!iconPath.StartsWith("Assets/"))
{
return AssetDatabase.LoadAssetAtPath<Texture2D>(defaultIconPath);
}
var iconObj = AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
if (iconObj == null)
{
return AssetDatabase.LoadAssetAtPath<Texture2D>(defaultIconPath);
}
return iconObj;
}
private void _ShowProgress(string label, int currentValue, int maxValue)
{
GUILayout.BeginHorizontal(GUILayout.Height(40));
GUILayout.Label(label, GUILayout.Width(60));
if (maxValue == 0)
{
GUILayout.Label("-无-");
}
else
{
GUILayout.BeginVertical();
var progress = 1f * currentValue / maxValue;
var progressStr = $"{currentValue}/{maxValue} ({progress:P})";
GUILayout.Label(progressStr);
GUILayout.HorizontalSlider(progress, 0, 1);
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
private void _ShowBuff(BaseBuff buff)
{
GUILayout.BeginVertical("box");
_DisplayBuffIcon(buff);
_ShowLabel("名字:", buff.configData.Name);
_ShowLabel("BuffId:", buff.configData.ID + "");
GUILayout.EndVertical();
}
private void _DisplayBuffIcon(BaseBuff buff)
{
// Assets/Art/UI/Texture/Icon/BuffIcon/UI_Armor.png
var displayIcon = buff.configData.BuffDisplayIcon;
if (string.IsNullOrEmpty(displayIcon))
{
GUILayout.Label("无图标");
return;
}
var iconPath = "Assets/Art_Out/UI/Texture/Icon/BuffIcon/" + displayIcon + ".png";
var icon = _LoadIcon(iconPath);
if (icon == null)
{
EditorGUILayout.HelpBox("无法找到图标:" + displayIcon, MessageType.Error);
}
else
{
GUILayout.BeginHorizontal();
GUILayout.Label("图标:", GUILayout.Width(60));
var originWidth = icon.width;
var originHeight = icon.height;
var scaleSize = 40f / originWidth;
GUILayout.Label(new GUIContent(icon), GUILayout.Width(originWidth * scaleSize), GUILayout.Height(originHeight * scaleSize));
GUILayout.EndHorizontal();
}
}
private void _ShowDebugArea(List<int> cellIndexs)
{
var fakeParent = new GameObject("FakeParent").transform;
for (int i = 0; i < cellIndexs.Count; i++)
{
var worldPos = AreaManager.instance.GetWorldPosByIndex(cellIndexs[i]);
// 在对应位置创建一个球体
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.SetParent(fakeParent);
sphere.transform.position = worldPos;
}
}
private void _ShowInFight()
{
var unit = _targetUnit;
_scrollPos1 = GUILayout.BeginScrollView(_scrollPos1, GUILayout.Width(350));
GUILayout.BeginVertical("box",GUILayout.Width(320));
_ShowLabel("ID:", unit.GetID() + "");
_ShowLabel("类型:", EGameUnitTypeUtils.GetStrByType(unit.gameunitType));
_ShowLabel("名字:", unit.debugShowInfo.name);
_ShowLabel("配置ID:", unit.debugShowInfo.configId);
_ShowIcon(unit.debugShowInfo.iconPath);
if (GUILayout.Button("显示攻击范围"))
{
var centerCellIndex = unit.transData.cellIndex;
var attackDistance = unit.fightData.attackDistance;
var attackArea = AreaManager.instance.attackAreaManager.GetAttackArea(centerCellIndex, attackDistance);
var isIgnoreBlock = unit.fightData.isAttackIgnoreBlock;
var attackCells = isIgnoreBlock ? attackArea.totalCells : attackArea.canAttackCells;
_ShowDebugArea(attackCells);
}
_ShowProgress("生命值:", unit.aliveData.currentHp, unit.aliveData.maxHp);
_ShowProgress("护甲值:", unit.aliveData.currentShield, unit.aliveData.maxShield);
_ShowProgress("士气值:", unit.aliveData.currentMorale, unit.aliveData.maxMorale);
var allBuff = unit.buffManager.totalBuffList;
_ShowLabel("Buff数量:", allBuff.Count + "个");
for (int i = 0; i < allBuff.Count; i++)
{
var buff = allBuff[i];
_ShowBuff(buff);
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
}
private void OnGUI()
{
if (_CheckCanShow())
{
_ShowInFight();
}
else
{
_ShowNotInFight();
// 关闭窗口
Close();
}
}
}
}