yyl
2026-02-11 1ad03cc2f91d75e80fc3dc42e2ac1fadc9a2bfec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
 
/// <summary>
/// 广告管理器 - 统一封装各平台广告调用
/// </summary>
public class AdManager : MonoBehaviour
{
    private static AdManager _instance;
    public static AdManager Instance
    {
        get
        {
            if (_instance == null)
            {
                var go = new GameObject("[AdManager]");
                _instance = go.AddComponent<AdManager>();
                DontDestroyOnLoad(go);
            }
            return _instance;
        }
    }
    
    [Header("广告配置")]
    [Tooltip("视频广告冷却时间(秒)")]
    public float videoAdCooldown = 30f;
    
    [Tooltip("是否启用广告")]
    public bool enableAds = true;
    
    // 广告事件
    public Action<AdResult> OnVideoAdCompleted;
    public Action<string> OnAdError;
    
    // 内部状态
    private IPlatformService _platform;
    private float _lastVideoAdTime = 0f;
    private bool _isShowingAd = false;
    
    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(gameObject);
            return;
        }
        _instance = this;
        DontDestroyOnLoad(gameObject);
    }
    
    /// <summary>
    /// 初始化广告管理器
    /// </summary>
    public void Initialize(IPlatformService platform)
    {
        _platform = platform;
        Debug.Log("[AdManager] 广告管理器初始化完成");
    }
    
    /// <summary>
    /// 显示激励视频广告
    /// </summary>
    /// <param name="onSuccess">观看完成回调</param>
    /// <param name="onFail">失败回调</param>
    public async UniTask<bool> ShowRewardedVideoAd(Action onSuccess = null, Action onFail = null)
    {
        if (!enableAds)
        {
            Debug.LogWarning("[AdManager] 广告已禁用");
            onFail?.Invoke();
            return false;
        }
        
        if (_platform == null)
        {
            Debug.LogError("[AdManager] 平台服务未初始化");
            onFail?.Invoke();
            return false;
        }
        
        if (_isShowingAd)
        {
            Debug.LogWarning("[AdManager] 已有广告正在播放");
            onFail?.Invoke();
            return false;
        }
        
        // 检查冷却时间
        float timeSinceLastAd = Time.time - _lastVideoAdTime;
        if (timeSinceLastAd < videoAdCooldown)
        {
            float remainingTime = videoAdCooldown - timeSinceLastAd;
            Debug.LogWarning($"[AdManager] 广告冷却中,剩余 {remainingTime:F0} 秒");
            OnAdError?.Invoke($"广告冷却中,请等待 {remainingTime:F0} 秒");
            onFail?.Invoke();
            return false;
        }
        
        _isShowingAd = true;
        
        try
        {
            Debug.Log("[AdManager] 开始播放激励视频广告");
            
            AdResult result = await _platform.ShowAdAsync(AdType.Video);
            
            if (result.Success && result.Completed)
            {
                Debug.Log("[AdManager] 用户观看完广告");
                _lastVideoAdTime = Time.time;
                OnVideoAdCompleted?.Invoke(result);
                onSuccess?.Invoke();
                return true;
            }
            else
            {
                Debug.LogWarning($"[AdManager] 广告播放失败: {result.ErrorMessage}");
                OnAdError?.Invoke(result.ErrorMessage);
                onFail?.Invoke();
                return false;
            }
        }
        catch (Exception e)
        {
            Debug.LogError($"[AdManager] 广告播放异常: {e.Message}");
            OnAdError?.Invoke(e.Message);
            onFail?.Invoke();
            return false;
        }
        finally
        {
            _isShowingAd = false;
        }
    }
    
    /// <summary>
    /// 显示横幅广告
    /// </summary>
    public async UniTask<bool> ShowBannerAd()
    {
        if (!enableAds || _platform == null)
            return false;
        
        try
        {
            AdResult result = await _platform.ShowAdAsync(AdType.Banner);
            return result.Success;
        }
        catch (Exception e)
        {
            Debug.LogError($"[AdManager] 横幅广告显示失败: {e.Message}");
            return false;
        }
    }
    
    /// <summary>
    /// 显示插屏广告
    /// </summary>
    public async UniTask<bool> ShowInterstitialAd()
    {
        if (!enableAds || _platform == null)
            return false;
        
        try
        {
            AdResult result = await _platform.ShowAdAsync(AdType.Interstitial);
            return result.Success;
        }
        catch (Exception e)
        {
            Debug.LogError($"[AdManager] 插屏广告显示失败: {e.Message}");
            return false;
        }
    }
    
    /// <summary>
    /// 检查是否可以播放广告
    /// </summary>
    public bool CanShowAd()
    {
        if (!enableAds || _platform == null || _isShowingAd)
            return false;
        
        float timeSinceLastAd = Time.time - _lastVideoAdTime;
        return timeSinceLastAd >= videoAdCooldown;
    }
    
    /// <summary>
    /// 获取广告冷却剩余时间
    /// </summary>
    public float GetCooldownRemaining()
    {
        float timeSinceLastAd = Time.time - _lastVideoAdTime;
        float remaining = videoAdCooldown - timeSinceLastAd;
        return Mathf.Max(0f, remaining);
    }
}