tryna getit to work

This commit is contained in:
Rapandrasmus 2023-10-13 09:05:34 +02:00
parent 5e5b9c245f
commit 46ba2e9f84
3 changed files with 80 additions and 4 deletions

View file

@ -12,6 +12,7 @@ GameObject:
- component: {fileID: 5892063193686803625}
- component: {fileID: 2207471109642228847}
- component: {fileID: 8328211396897494371}
- component: {fileID: 6891928905748349877}
m_Layer: 5
m_Name: RightClickDropdown
m_TagString: Untagged
@ -38,7 +39,7 @@ RectTransform:
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -17.3663}
m_SizeDelta: {x: 160, y: 35}
m_SizeDelta: {x: 160, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &5892063193686803625
CanvasRenderer:
@ -104,6 +105,20 @@ MonoBehaviour:
m_ChildScaleWidth: 0
m_ChildScaleHeight: 0
m_ReverseArrangement: 0
--- !u!114 &6891928905748349877
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1069446853043438909}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3245ec927659c4140ac4f8d17403cc18, type: 3}
m_Name:
m_EditorClassIdentifier:
m_HorizontalFit: 0
m_VerticalFit: 2
--- !u!1 &5102644904920450891
GameObject:
m_ObjectHideFlags: 0

View file

@ -6615,6 +6615,7 @@ GameObject:
m_Component:
- component: {fileID: 279538358}
- component: {fileID: 279538359}
- component: {fileID: 279538360}
m_Layer: 5
m_Name: Toggle
m_TagString: Untagged
@ -6691,6 +6692,25 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls: []
m_IsOn: 1
--- !u!114 &279538360
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 279538357}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e4972b7ea15fb7943ad12ed39e229140, type: 3}
m_Name:
m_EditorClassIdentifier:
_events:
- name: Reset
action:
m_PersistentCalls:
m_Calls: []
_masterTrans: {fileID: 1791483803}
_dropDown: {fileID: 8384641343036272350, guid: 56a45ac5cd0da2a4ab398d1a63e18d9f, type: 3}
--- !u!1 &282924607
GameObject:
m_ObjectHideFlags: 0
@ -21244,7 +21264,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 1589389271}
m_Direction: 2
m_Value: 0
m_Size: 1
m_Size: 0.99999213
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
@ -31067,6 +31087,7 @@ MonoBehaviour:
inAuthorativeMenu: 0
isCursorEnabled: 1
isDiscordEnabled: 1
ShouldQuit: 0
--- !u!114 &1423699438
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
@ -9,13 +10,52 @@ namespace HeavenStudio.Common
{
public class RightClickDropdownObject : MonoBehaviour, IPointerClickHandler
{
[SerializeField] private Event[] events;
[SerializeField] private Event[] _events;
[Header("Components")]
[SerializeField] private Transform _masterTrans;
[SerializeField] private RectTransform _dropDown;
private Button _dropDownButton;
private RectTransform _currentDropDown;
private void Awake()
{
_dropDownButton = _dropDown.GetChild(0).GetComponent<Button>();
}
private void OnDestroy()
{
if (_currentDropDown != null) Destroy(_currentDropDown.gameObject);
}
private void OnDisable()
{
if (_currentDropDown != null) Destroy(_currentDropDown.gameObject);
}
private void Update()
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
if (_currentDropDown != null) Destroy(_currentDropDown.gameObject);
}
}
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Right) return;
if (eventData.button != PointerEventData.InputButton.Right || _events.Length == 0) return;
_currentDropDown = Instantiate(_dropDown, _masterTrans);
_currentDropDown.transform.position = Input.mousePosition;
foreach(var e in _events)
{
Button spawnedButton = Instantiate(_dropDownButton, _currentDropDown);
spawnedButton.onClick.AddListener(new UnityAction(() => { e.action.Invoke(); }));
spawnedButton.transform.GetChild(0).GetComponent<TMP_Text>().text = e.name;
}
}
[System.Serializable]
private struct Event
{
public string name;