2025-07-25 19:16:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
using UnityEngine;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
using UnityEngine.Events;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
|
|
|
|
|
namespace Entity
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public class Outline : MonoBehaviour
|
2025-07-21 13:58:58 +08:00
|
|
|
|
{
|
|
|
|
|
public GameObject body;
|
|
|
|
|
public SpriteRenderer outlineRenderer;
|
|
|
|
|
public CapsuleCollider2D outlineCollider;
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public Entity entity;
|
|
|
|
|
|
|
|
|
|
private bool _select = false;
|
2025-08-05 14:54:30 +08:00
|
|
|
|
|
|
|
|
|
public static Vector3 minimum=new(0.5f,0.5f,0.5f);
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
var size = GetSize();
|
|
|
|
|
outlineRenderer.size = size;
|
|
|
|
|
outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
|
|
|
|
|
outlineCollider.size = size;
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public void Show()
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
outlineRenderer.enabled = true;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Hide()
|
|
|
|
|
{
|
2025-07-25 19:16:58 +08:00
|
|
|
|
outlineRenderer.enabled = false;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取指定对象及其所有子对象组成的图像的大小。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// 返回一个 Vector3 对象,表示对象在世界空间中的总大小(宽度、高度、深度)。
|
|
|
|
|
/// 如果没有找到任何渲染器,则返回 (-1, -1, -1) 表示无效大小。
|
|
|
|
|
/// </returns>
|
|
|
|
|
public Vector3 GetSize()
|
|
|
|
|
{
|
2025-08-05 14:54:30 +08:00
|
|
|
|
// 获取所有子对象的 Renderer 组件
|
2025-07-21 13:58:58 +08:00
|
|
|
|
var renderers = body.GetComponentsInChildren<Renderer>();
|
|
|
|
|
|
2025-08-05 14:54:30 +08:00
|
|
|
|
// 如果没有找到任何 Renderer,返回一个默认值 (-1, -1, -1)
|
2025-07-21 13:58:58 +08:00
|
|
|
|
if (renderers.Length == 0)
|
|
|
|
|
{
|
2025-08-05 14:54:30 +08:00
|
|
|
|
return minimum;
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-05 14:54:30 +08:00
|
|
|
|
// 初始化 totalBounds 为第一个 Renderer 的 bounds
|
2025-07-21 13:58:58 +08:00
|
|
|
|
var totalBounds = renderers[0].bounds;
|
2025-08-05 14:54:30 +08:00
|
|
|
|
|
|
|
|
|
// 遍历剩余的 Renderer,将它们的 bounds 合并到 totalBounds 中
|
2025-07-21 13:58:58 +08:00
|
|
|
|
for (var i = 1; i < renderers.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
totalBounds.Encapsulate(renderers[i].bounds);
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-05 14:54:30 +08:00
|
|
|
|
// 获取合并后的包围盒的大小
|
2025-07-21 13:58:58 +08:00
|
|
|
|
var size = totalBounds.size;
|
2025-08-05 14:54:30 +08:00
|
|
|
|
|
|
|
|
|
// 确保每个维度的大小都不小于 0.5
|
|
|
|
|
size.x = Mathf.Max(size.x, 0.5f);
|
|
|
|
|
size.y = Mathf.Max(size.y, 0.5f);
|
|
|
|
|
size.z = Mathf.Max(size.z, 0.5f);
|
|
|
|
|
|
2025-07-21 13:58:58 +08:00
|
|
|
|
return size;
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
|
|
|
|
private void OnMouseEnter()
|
|
|
|
|
{
|
|
|
|
|
Show();
|
|
|
|
|
_select = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMouseExit()
|
|
|
|
|
{
|
|
|
|
|
Hide();
|
|
|
|
|
_select = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMouseOver()
|
|
|
|
|
{
|
|
|
|
|
// 检测是否按下的是鼠标右键
|
|
|
|
|
if (Input.GetMouseButtonDown(1)) // 鼠标右键对应的是按钮索引 1
|
|
|
|
|
{
|
|
|
|
|
var rightMenu = Prefab.RightMenuPrefab.Instance;
|
|
|
|
|
rightMenu.Init(GetMenu());
|
2025-08-04 13:26:04 +08:00
|
|
|
|
rightMenu.transform.position = Input.mousePosition;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
rightMenu.Show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<(string name, UnityAction callback)> GetMenu()
|
|
|
|
|
{
|
|
|
|
|
var result = new List<(string name, UnityAction callback)>();
|
2025-08-04 13:26:04 +08:00
|
|
|
|
if (entity.PlayerControlled)
|
2025-08-05 14:54:30 +08:00
|
|
|
|
result.Add(("结束操控", EndControl));
|
2025-07-25 19:16:58 +08:00
|
|
|
|
else
|
2025-08-05 14:54:30 +08:00
|
|
|
|
result.Add(("手动操控", StartControl));
|
2025-08-04 13:26:04 +08:00
|
|
|
|
result.Add(("杀死", () => entity.Kill()));
|
2025-08-05 14:54:30 +08:00
|
|
|
|
result.Add(("变成笨蛋", BecomeDefault));
|
2025-07-25 19:16:58 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2025-07-31 17:45:50 +08:00
|
|
|
|
|
|
|
|
|
private void BecomeDefault()
|
|
|
|
|
{
|
|
|
|
|
entity.Kill();
|
|
|
|
|
Managers.EntityManage.Instance.GenerateDefaultEntity(entity.Position);
|
2025-08-04 13:26:04 +08:00
|
|
|
|
}
|
2025-08-05 14:54:30 +08:00
|
|
|
|
|
|
|
|
|
private void StartControl()
|
|
|
|
|
{
|
|
|
|
|
entity.PlayerControlled = true;
|
|
|
|
|
CameraControl.CameraControl.Instance.focusedEntity=entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EndControl()
|
|
|
|
|
{
|
|
|
|
|
entity.PlayerControlled = false;
|
|
|
|
|
CameraControl.CameraControl.Instance.focusedEntity=null;
|
|
|
|
|
}
|
2025-07-21 13:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|