(client) feat:实现右键菜单

This commit is contained in:
m0_75251201
2025-07-23 18:53:14 +08:00
parent 046ebe3cfe
commit ac278fba46
19 changed files with 1146 additions and 790 deletions

View File

@ -1,13 +1,20 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace Entity
{
public class Outline:MonoBehaviour
public class Outline : MonoBehaviour
{
public GameObject body;
public SpriteRenderer outlineRenderer;
public CapsuleCollider2D outlineCollider;
public Entity entity;
private bool _select = false;
public void Init()
{
var size = GetSize();
@ -15,15 +22,17 @@ namespace Entity
outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
outlineCollider.size = size;
}
public void Show()
{
outlineRenderer.gameObject.SetActive(true);
outlineRenderer.enabled = true;
}
public void Hide()
{
outlineRenderer.gameObject.SetActive(false);
outlineRenderer.enabled = false;
}
/// <summary>
/// 获取指定对象及其所有子对象组成的图像的大小。
/// </summary>
@ -37,15 +46,52 @@ namespace Entity
if (renderers.Length == 0)
{
return new(-1,-1);
return new(-1, -1);
}
var totalBounds = renderers[0].bounds;
for (var i = 1; i < renderers.Length; i++)
{
totalBounds.Encapsulate(renderers[i].bounds);
}
var size = totalBounds.size;
return size;
}
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());
rightMenu.transform.position=Input.mousePosition;
rightMenu.Show();
}
}
private List<(string name, UnityAction callback)> GetMenu()
{
var result = new List<(string name, UnityAction callback)>();
if(entity.PlayerControlled)
result.Add(("结束操控",()=>entity.PlayerControlled=false));
else
result.Add(("手动操控",()=>entity.PlayerControlled=true));
result.Add(("杀死",()=>entity.Kill()));
return result;
}
}
}