197 lines
6.1 KiB
C#
197 lines
6.1 KiB
C#
// // ©2015 - 2025 Candy Smith
|
|
// // All rights reserved
|
|
// // Redistribution of this software is strictly not allowed.
|
|
// // Copy of this software can be obtained from unity asset store only.
|
|
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// // THE SOFTWARE.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using VContainer;
|
|
using WordsToolkit.Scripts.Popups;
|
|
using WordsToolkit.Scripts.Services.Ads;
|
|
using WordsToolkit.Scripts.Services.Ads.AdUnits;
|
|
using WordsToolkit.Scripts.Services.IAP;
|
|
using WordsToolkit.Scripts.Settings;
|
|
using WordsToolkit.Scripts.System;
|
|
|
|
namespace WordsToolkit.Scripts.Services
|
|
{
|
|
public class AdsManager : MonoBehaviour, IAdsManager
|
|
{
|
|
private readonly List<AdSetting> adList = new();
|
|
private readonly List<AdUnit> adUnits = new();
|
|
private readonly Dictionary<AdUnit, IAdLifecycleManager> lifecycleManagers = new();
|
|
private EPlatforms platforms;
|
|
|
|
[Inject]
|
|
private GameSettings gameSettings;
|
|
|
|
[Inject]
|
|
private IIAPManager iapManager;
|
|
|
|
[SerializeField]
|
|
private ProductID noAdsProduct;
|
|
|
|
private void Awake()
|
|
{
|
|
if (!gameSettings.enableAds)
|
|
{
|
|
return;
|
|
}
|
|
|
|
platforms = GetPlatform();
|
|
var adElements = Resources.Load<AdsSettings>("Settings/AdsSettings").adProfiles;
|
|
foreach (var t in adElements)
|
|
{
|
|
if (t.platforms == platforms && t.enable)
|
|
{
|
|
if (Application.isEditor && !t.testInEditor)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
adList.Add(t);
|
|
foreach (var adElement in t.adElements)
|
|
{
|
|
var adUnit = new AdUnit(adElement.placementId, adElement.adReference);
|
|
var lifecycleManager = new AdLifecycleManager(t.adsHandler);
|
|
lifecycleManagers[adUnit] = lifecycleManager;
|
|
adUnits.Add(adUnit);
|
|
|
|
if (adUnit.AdReference.adType == EAdType.Banner && !IsNoAdsPurchased())
|
|
{
|
|
lifecycleManager.Show(adUnit);
|
|
}
|
|
}
|
|
|
|
t.adsHandler?.Init(t.appId, false, new AdsListener(adUnits, lifecycleManagers));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if(IsNoAdsPurchased())
|
|
RemoveAds();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
Popup.OnOpenPopup += OnOpenPopup;
|
|
Popup.OnClosePopup += OnClosePopup;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
Popup.OnOpenPopup -= OnOpenPopup;
|
|
Popup.OnClosePopup -= OnClosePopup;
|
|
}
|
|
|
|
private EPlatforms GetPlatform()
|
|
{
|
|
#if UNITY_ANDROID
|
|
return EPlatforms.Android;
|
|
#elif UNITY_IOS
|
|
return EPlatforms.IOS;
|
|
#elif UNITY_WEBGL
|
|
return EPlatforms.WebGL;
|
|
#else
|
|
return EPlatforms.Windows;
|
|
#endif
|
|
}
|
|
|
|
private void OnOpenPopup(Popup popup)
|
|
{
|
|
OnPopupTrigger(popup, true);
|
|
}
|
|
|
|
private void OnClosePopup(Popup popup)
|
|
{
|
|
OnPopupTrigger(popup, false);
|
|
}
|
|
|
|
private void OnPopupTrigger(Popup popup, bool open)
|
|
{
|
|
if (IsNoAdsPurchased())
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var ad in adList)
|
|
{
|
|
foreach (var adElement in ad.adElements)
|
|
{
|
|
var adUnit = adUnits.Find(i => i.AdReference == adElement.adReference);
|
|
if (!lifecycleManagers[adUnit].IsAvailable(adUnit))
|
|
{
|
|
lifecycleManagers[adUnit].Load(adUnit);
|
|
continue;
|
|
}
|
|
|
|
if (((open && adElement.popup.showOnOpen) || (!open && adElement.popup.showOnClose)) && popup.GetType() == adElement.popup.popup.GetType())
|
|
{
|
|
lifecycleManagers[adUnit].Show(adUnit);
|
|
lifecycleManagers[adUnit].Load(adUnit);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsNoAdsPurchased()
|
|
{
|
|
return !gameSettings.enableAds || iapManager.IsProductPurchased(noAdsProduct.ID);
|
|
}
|
|
|
|
public void ShowAdByType(AdReference adRef, Action<string> shown)
|
|
{
|
|
if (!gameSettings.enableAds)
|
|
{
|
|
shown?.Invoke(null);
|
|
return;
|
|
}
|
|
|
|
foreach (var adUnit in adUnits)
|
|
{
|
|
if (adUnit.AdReference == adRef && lifecycleManagers[adUnit].IsAvailable(adUnit))
|
|
{
|
|
adUnit.OnShown = shown;
|
|
lifecycleManagers[adUnit].Show(adUnit);
|
|
lifecycleManagers[adUnit].Load(adUnit);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsRewardedAvailable(AdReference adRef)
|
|
{
|
|
foreach (var adUnit in adUnits)
|
|
{
|
|
if (adUnit.AdReference == adRef)
|
|
{
|
|
return lifecycleManagers[adUnit].IsAvailable(adUnit);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void RemoveAds()
|
|
{
|
|
foreach (var adUnit in adUnits)
|
|
{
|
|
if (adUnit.AdReference.adType == EAdType.Banner)
|
|
{
|
|
lifecycleManagers[adUnit].Hide(adUnit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |