Practice_Unity/Assets/Scripts/UI/VirtualButtons.cs
Seonkyu.kim 488c0858ad 작업
1. UI 버튼 작업 했음
2. 칼, 방패 따로 붙이는 작업 헀음
3. 공격 모션에 이제 버튼 연동함

Todo
1. 공격시 화면 이상하게 흔들리는거 수정할 차례
2. 히트 박스 해서 몬스터 공격하는거 연동하기
2025-09-29 17:59:40 +09:00

98 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VirtualButtons : MonoBehaviour
{
private Button _attackButton;
private Button[] _skillButtons = new Button[3];
public event Action OnAttackButtonClicked;
public event Action<int> OnSkillButtonClicked;
private float _mainButtonSize = 180f; // 조이스틱 크기(300f)의 0.6배
private float _subButtonSize = 120f;
private float _subButtonDistance = 200f; // 메인과 서브의 거리
private void Awake()
{
_skillButtons = new Button[3];
CreateAttackButtons();
CreateSkillButtons();
}
private void Start()
{
Manager.Input.Buttons = this;
// CreateAttackButtons();
}
void CreateAttackButtons()
{
GameObject ButtonObj = Manager.Resource.Instantiate("Prefabs/UI/VirtualButton", transform);
ButtonObj.name = "AttackButton";
_attackButton = ButtonObj.GetComponent<Button>();
RectTransform rectTransform = _attackButton.GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(_mainButtonSize, _mainButtonSize);
rectTransform.anchorMin = new Vector2(1, 0);
rectTransform.anchorMax = new Vector2(1, 0);
rectTransform.pivot = new Vector2(0.5f, 0.5f);
rectTransform.anchoredPosition = new Vector2(-100f - _mainButtonSize / 2, 100f + _mainButtonSize / 2);
_attackButton.onClick.AddListener(ClickedAttackButton);
}
void CreateSkillButtons()
{
float[] angles = { 90f, 135f, 180f};
for (int i = 0; i < 3; i++)
{
GameObject ButtonObj = Manager.Resource.Instantiate("Prefabs/UI/VirtualButton", transform);
ButtonObj.name = $"SkillButton_{i}";
_skillButtons[i] = ButtonObj.GetComponent<Button>();
RectTransform rectTransform = _skillButtons[i].GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(_subButtonSize, _subButtonSize);
rectTransform.anchorMin = new Vector2(1, 0);
rectTransform.anchorMax = new Vector2(1, 0);
rectTransform.pivot = new Vector2(0.5f, 0.5f);
float angle = angles[i] * Mathf.Deg2Rad;
Vector2 mainPos = _attackButton.GetComponent<RectTransform>().anchoredPosition;
Vector2 skillPos = new Vector2(
mainPos.x + _subButtonDistance * Mathf.Cos(angle),
mainPos.y + _subButtonDistance * Mathf.Sin(angle));
rectTransform.anchoredPosition = skillPos;
int idx = i;
_skillButtons[i].onClick.AddListener(() => ClickedSkillButton(idx));
}
}
void ClickedAttackButton()
{
OnAttackButtonClicked?.Invoke();
}
void ClickedSkillButton(int index)
{
OnSkillButtonClicked?.Invoke(index);
}
public void SetSkillButtonEvent(int index, UnityEngine.Events.UnityAction action)
{
if (index >= 0 && index < _skillButtons.Length)
{
_skillButtons[index].onClick.AddListener(action);
}
}
}