Files
BingoGrassland/Assets/MaxSdk/Scripts/MaxEventExecutor.cs
T

109 lines
3.0 KiB
C#
Raw Normal View History

2026-04-20 13:49:36 +08:00
//
// MaxEventExecutor.cs
// Max Unity Plugin
//
// Created by Jonathan Liu on 1/22/2024.
// Copyright © 2024 AppLovin. All rights reserved.
//
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace AppLovinMax.Internal
{
public class MaxEventExecutor : MonoBehaviour
{
2026-05-08 11:03:00 +08:00
private static MaxEventExecutor _instance;
private static readonly List<MaxAction> AdEventsQueue = new List<MaxAction>();
2026-04-20 13:49:36 +08:00
2026-05-08 11:03:00 +08:00
private static volatile bool _adEventsQueueEmpty = true;
2026-04-20 13:49:36 +08:00
struct MaxAction
{
2026-05-08 11:03:00 +08:00
public readonly Action ActionToExecute;
public readonly string EventName;
2026-04-20 13:49:36 +08:00
public MaxAction(Action actionToExecute, string nameOfEvent)
{
2026-05-08 11:03:00 +08:00
ActionToExecute = actionToExecute;
EventName = nameOfEvent;
2026-04-20 13:49:36 +08:00
}
}
public static void InitializeIfNeeded()
{
2026-05-08 11:03:00 +08:00
if (_instance != null) return;
2026-04-20 13:49:36 +08:00
var executor = new GameObject("MaxEventExecutor");
executor.hideFlags = HideFlags.HideAndDontSave;
DontDestroyOnLoad(executor);
2026-05-08 11:03:00 +08:00
_instance = executor.AddComponent<MaxEventExecutor>();
2026-04-20 13:49:36 +08:00
}
#region Public API
#if UNITY_EDITOR || !(UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS)
public static MaxEventExecutor Instance
{
get
{
InitializeIfNeeded();
2026-05-08 11:03:00 +08:00
return _instance;
2026-04-20 13:49:36 +08:00
}
}
#endif
public static void ExecuteOnMainThread(Action action, string eventName)
{
2026-05-08 11:03:00 +08:00
lock (AdEventsQueue)
2026-04-20 13:49:36 +08:00
{
2026-05-08 11:03:00 +08:00
AdEventsQueue.Add(new MaxAction(action, eventName));
_adEventsQueueEmpty = false;
2026-04-20 13:49:36 +08:00
}
}
public static void InvokeOnMainThread(UnityEvent unityEvent, string eventName)
{
ExecuteOnMainThread(() => unityEvent.Invoke(), eventName);
}
#endregion
public void Update()
{
2026-05-08 11:03:00 +08:00
if (_adEventsQueueEmpty) return;
2026-04-20 13:49:36 +08:00
var actionsToExecute = new List<MaxAction>();
2026-05-08 11:03:00 +08:00
lock (AdEventsQueue)
2026-04-20 13:49:36 +08:00
{
2026-05-08 11:03:00 +08:00
actionsToExecute.AddRange(AdEventsQueue);
AdEventsQueue.Clear();
_adEventsQueueEmpty = true;
2026-04-20 13:49:36 +08:00
}
foreach (var maxAction in actionsToExecute)
{
2026-05-08 11:03:00 +08:00
if (maxAction.ActionToExecute.Target != null)
2026-04-20 13:49:36 +08:00
{
try
{
2026-05-08 11:03:00 +08:00
maxAction.ActionToExecute.Invoke();
2026-04-20 13:49:36 +08:00
}
catch (Exception exception)
{
2026-05-08 11:03:00 +08:00
MaxSdkLogger.UserError("Caught exception in publisher event: " + maxAction.EventName + ", exception: " + exception);
2026-04-20 13:49:36 +08:00
MaxSdkLogger.LogException(exception);
}
}
}
}
public void Disable()
{
2026-05-08 11:03:00 +08:00
_instance = null;
2026-04-20 13:49:36 +08:00
}
}
}