133 lines
4.0 KiB
C#
133 lines
4.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace DDD
|
|
{
|
|
/// <summary>
|
|
/// Simple test script to verify UiButton functionality.
|
|
/// Attach this to a GameObject with UiButton component to test.
|
|
/// </summary>
|
|
public class UiButtonTest : MonoBehaviour
|
|
{
|
|
[SerializeField] private UiButton _uiButton;
|
|
[SerializeField] private Text _statusText;
|
|
|
|
private int _clickCount = 0;
|
|
|
|
private void Start()
|
|
{
|
|
// Get UiButton if not assigned
|
|
if (_uiButton == null)
|
|
{
|
|
_uiButton = GetComponent<UiButton>();
|
|
}
|
|
|
|
if (_uiButton != null)
|
|
{
|
|
// Subscribe to events
|
|
_uiButton.OnClicked += HandleButtonClicked;
|
|
_uiButton.OnStateChanged += HandleStateChanged;
|
|
|
|
Debug.Log("[DEBUG_LOG] UiButton test initialized");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("[DEBUG_LOG] UiButton component not found!");
|
|
}
|
|
|
|
UpdateStatusText();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_uiButton != null)
|
|
{
|
|
_uiButton.OnClicked -= HandleButtonClicked;
|
|
_uiButton.OnStateChanged -= HandleStateChanged;
|
|
}
|
|
}
|
|
|
|
private void HandleButtonClicked()
|
|
{
|
|
_clickCount++;
|
|
Debug.Log($"[DEBUG_LOG] UiButton clicked! Count: {_clickCount}");
|
|
UpdateStatusText();
|
|
}
|
|
|
|
private void HandleStateChanged()
|
|
{
|
|
Debug.Log($"[DEBUG_LOG] UiButton state changed. Interactable: {_uiButton.IsInteractable}");
|
|
}
|
|
|
|
private void UpdateStatusText()
|
|
{
|
|
if (_statusText != null)
|
|
{
|
|
_statusText.text = $"Clicks: {_clickCount}\nInteractable: {(_uiButton?.IsInteractable ?? false)}";
|
|
}
|
|
}
|
|
|
|
// Test methods that can be called from inspector or other scripts
|
|
[ContextMenu("Toggle Interactable")]
|
|
public void ToggleInteractable()
|
|
{
|
|
if (_uiButton != null)
|
|
{
|
|
_uiButton.SetInteractable(!_uiButton.IsInteractable);
|
|
Debug.Log($"[DEBUG_LOG] Button interactable set to: {_uiButton.IsInteractable}");
|
|
UpdateStatusText();
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Force Update State")]
|
|
public void ForceUpdateState()
|
|
{
|
|
if (_uiButton != null)
|
|
{
|
|
_uiButton.ForceUpdateState();
|
|
Debug.Log("[DEBUG_LOG] Button state forcefully updated");
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Reset Click Count")]
|
|
public void ResetClickCount()
|
|
{
|
|
_clickCount = 0;
|
|
Debug.Log("[DEBUG_LOG] Click count reset");
|
|
UpdateStatusText();
|
|
}
|
|
|
|
[ContextMenu("Toggle Use Toggle Mode")]
|
|
public void ToggleUseToggleMode()
|
|
{
|
|
if (_uiButton != null)
|
|
{
|
|
_uiButton.UseToggle = !_uiButton.UseToggle;
|
|
Debug.Log($"[DEBUG_LOG] Toggle mode set to: {_uiButton.UseToggle}");
|
|
UpdateStatusText();
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Set Toggle State On")]
|
|
public void SetToggleStateOn()
|
|
{
|
|
if (_uiButton != null)
|
|
{
|
|
_uiButton.SetToggleState(true);
|
|
Debug.Log($"[DEBUG_LOG] Toggle state set to ON. Toggled: {_uiButton.IsToggled}");
|
|
UpdateStatusText();
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Set Toggle State Off")]
|
|
public void SetToggleStateOff()
|
|
{
|
|
if (_uiButton != null)
|
|
{
|
|
_uiButton.SetToggleState(false);
|
|
Debug.Log($"[DEBUG_LOG] Toggle state set to OFF. Toggled: {_uiButton.IsToggled}");
|
|
UpdateStatusText();
|
|
}
|
|
}
|
|
}
|
|
} |