Playvoi/Assets/ShiroginSDK/Samples/AnalyticsSample/Scripts/AnalyticsSampleUI.cs
2025-10-30 22:48:16 +03:00

204 lines
No EOL
7.5 KiB
C#

using System;
using System.Collections;
using ShiroginSDK.Runtime.Modules.Events;
using ShiroginSDK.Runtime.Services.Base;
using ShiroginSDK.Runtime.Services.Interfaces;
using UnityEngine;
using UnityEngine.UI;
namespace ShiroginSDK.Samples.AnalyticsSample.Scripts
{
/// <summary>
/// Sample UI that manually fires ShiroginEvents for analytics testing.
/// Each button simulates a real analytics event for validation and debugging.
/// </summary>
[RequireComponent(typeof(Canvas))]
public class AnalyticsEventSampleUI : MonoBehaviour
{
[Header("UI References")] [Tooltip("Parent RectTransform where all buttons will be created.")] [SerializeField]
private RectTransform buttonContainer;
[Header("Button Settings")] [SerializeField]
private Font buttonFont;
[SerializeField]
private Color buttonColor = new(0.2f, 0.4f, 0.8f);
[SerializeField]
private Color textColor = Color.white;
[SerializeField]
private Vector2 buttonSize = new(250, 80);
[SerializeField]
private int columns = 3;
[SerializeField]
private int spacing = 15;
private IEventService _eventService;
private void Awake()
{
_eventService = ServiceLocator.Get<IEventService>();
}
private void Start()
{
if (buttonContainer == null)
{
Debug.LogError("[AnalyticsEventSampleUI] Missing ButtonContainer RectTransform.");
return;
}
SetupGrid();
StartCoroutine(CreateButtonsGradually());
}
private void SetupGrid()
{
var grid = buttonContainer.GetComponent<GridLayoutGroup>()
?? buttonContainer.gameObject.AddComponent<GridLayoutGroup>();
grid.cellSize = buttonSize;
grid.spacing = new Vector2(spacing, spacing);
grid.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
grid.constraintCount = columns;
grid.childAlignment = TextAnchor.UpperCenter;
Canvas.ForceUpdateCanvases();
}
private IEnumerator CreateButtonsGradually()
{
CreateButton("Purchase Success", OnPurchaseSuccess);
yield return null;
CreateButton("Purchase Fail", OnPurchaseFail);
yield return null;
CreateButton("Reward Claimed", OnRewardClaimed);
yield return null;
CreateButton("Ad Watched", OnAdWatched);
yield return null;
CreateButton("Ad Revenue", OnAdRevenue);
yield return null;
CreateButton("Player SignUp", OnPlayerSignUp);
yield return null;
CreateButton("Player Login", OnPlayerLogin);
yield return null;
CreateButton("Session Start", OnSessionStart);
yield return null;
CreateButton("Session End", OnSessionEnd);
yield return null;
CreateButton("Tutorial Start", OnTutorialStart);
yield return null;
CreateButton("Tutorial Complete", OnTutorialComplete);
yield return null;
CreateButton("Level Complete", OnLevelComplete);
yield return null;
}
private void CreateButton(string label, Action callback)
{
var go = new GameObject(label, typeof(RectTransform), typeof(Button), typeof(Image));
go.transform.SetParent(buttonContainer, false);
var img = go.GetComponent<Image>();
img.color = buttonColor;
var btn = go.GetComponent<Button>();
btn.onClick.AddListener(() => callback?.Invoke());
var textGO = new GameObject("Label", typeof(RectTransform), typeof(Text));
textGO.transform.SetParent(go.transform, false);
var txt = textGO.GetComponent<Text>();
txt.text = label;
txt.alignment = TextAnchor.MiddleCenter;
txt.color = textColor;
txt.font = buttonFont != null ? buttonFont : Resources.GetBuiltinResource<Font>("Arial.ttf");
txt.resizeTextForBestFit = true;
var rect = textGO.GetComponent<RectTransform>();
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.offsetMin = Vector2.zero;
rect.offsetMax = Vector2.zero;
}
// ============================================================
// 🔹 Event Samples
// ============================================================
private void OnPurchaseSuccess()
{
Debug.Log("[AnalyticsEventSampleUI] PurchaseCompletedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.IAP.PurchaseCompletedEvent(null));
}
private void OnPurchaseFail()
{
Debug.Log("[AnalyticsEventSampleUI] PurchaseFailedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.IAP.PurchaseFailedEvent(null, "network_error", "Timeout"));
}
private void OnRewardClaimed()
{
Debug.Log("[AnalyticsEventSampleUI] RewardClaimedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Rewards.RewardClaimedEvent("daily_reward_1", "daily_login", 100, "Gold"));
}
private void OnAdWatched()
{
Debug.Log("[AnalyticsEventSampleUI] AdWatchedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Ads.AdWatchedEvent("rewarded_video_1", true, "AppLovin"));
}
private void OnAdRevenue()
{
Debug.Log("[AnalyticsEventSampleUI] AdRevenueEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Ads.AdRevenueEvent("AppLovin", "rewarded_video_1", "Rewarded", 0.005));
}
private void OnPlayerSignUp()
{
Debug.Log("[AnalyticsEventSampleUI] PlayerSignedUpEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Player.PlayerSignedUpEvent("player_12345", "guest"));
}
private void OnPlayerLogin()
{
Debug.Log("[AnalyticsEventSampleUI] PlayerLoggedInEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Player.PlayerLoggedInEvent("player_12345", "guest"));
}
private void OnSessionStart()
{
Debug.Log("[AnalyticsEventSampleUI] SessionStartedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Analytics.SessionStartedEvent("player_12345", DateTime.UtcNow));
}
private void OnSessionEnd()
{
Debug.Log("[AnalyticsEventSampleUI] SessionEndedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Analytics.SessionEndedEvent("player_12345", DateTime.UtcNow, 120f));
}
private void OnTutorialStart()
{
Debug.Log("[AnalyticsEventSampleUI] TutorialStartedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Tutorial.TutorialStartedEvent("tutorial_intro"));
}
private void OnTutorialComplete()
{
Debug.Log("[AnalyticsEventSampleUI] TutorialCompletedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Tutorial.TutorialCompletedEvent("tutorial_intro", 45f));
}
private void OnLevelComplete()
{
Debug.Log("[AnalyticsEventSampleUI] LevelCompletedEvent fired.");
_eventService?.Invoke(new ShiroginEvents.Progression.LevelCompletedEvent(4, true, 90f));
}
}
}