J'utilise le script suivant pour afficher des publicités avec unity-Ad. Je publie pourtant bien ma question dans cette partie du forum car c'est bien un problème de script, vous allez comprendre.
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class AdDisplay : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsListener, IUnityAdsShowListener
{
public string myGameIdAndroid = "XXXXXX";
public string myGameIdIOS = "XXXXXX";
public string myVideoPlacement = "supportad";
public bool adStarted = false;
public bool adInitializedComplete = false;
private bool testMode = true;
public GameObject panel;
void Awake()
{
Debug.Log("Test 1");
InitializedAd();
Debug.Log("Test2)");
}
public void InitializedAd()
{
Debug.Log("Test3");
Advertisement.Initialize(myGameIdAndroid, testMode, this);
panel.SetActive(false);
Debug.Log("Test4");
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete");
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.Log($"Unity Ads Initialization Failed : {error.ToString()} - {message}");
}
// Start is called before the first frame update
void Update()
{
if (Advertisement.isInitialized)
{
adInitializedComplete = true;
}
//Debug.Log("test start ad_script");
//Advertisement.AddListener(this);
//Advertisement.Initialize(myGameIdAndroid, testMode);
//panel.SetActive(false);
}
// Update is called once per frame
public void ShowAd()
{
Advertisement.Load("supportad",this);
//Debug.Log("test update ad_script");
//Debug.Log("Advertissement ready : " + Advertisement.IsReady(supportad));
// Debug.Log("adStarted" + adStarted);
panel.SetActive(true);
if (adInitializedComplete && /*Advertisement.IsReady(supportad) &&*/ !adStarted)
{
Advertisement.Show("supportad",this);
adStarted = true;
}
}
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
//Debug.Log("Test Show Complete");
switch (showCompletionState)
{
case UnityAdsShowCompletionState.COMPLETED:
Debug.Log("The ad was successfully shown.");
//
// CODE TO REWARD THE GAMER
// Give coins etc.
panel.SetActive(false);
adStarted = false;
Advertisement.Load("supportad", this);
break;
case UnityAdsShowCompletionState.SKIPPED:
Debug.Log("The ad was skipped before reaching the end.");
adStarted = false;
Advertisement.Load("supportad", this);
break;
case UnityAdsShowCompletionState.UNKNOWN:
Debug.LogError("The ad failed to be shown.");
adStarted = false;
Advertisement.Load("supportad", this);
break;
}
}
public void OnUnityAdsAdLoaded(string placementId)
{
Debug.Log("Cool c'est load");
//throw new System.NotImplementedException();
}
public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
{
throw new System.NotImplementedException();
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
throw new System.NotImplementedException();
}
public void OnUnityAdsShowStart(string placementId)
{
Debug.Log("Cool c'est start to be show");
//throw new System.NotImplementedException();
}
public void OnUnityAdsShowClick(string placementId)
{
throw new System.NotImplementedException();
}
/*public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
throw new System.NotImplementedException();
}*/
void OnGUI()
{
GUI.Label(new Rect(300, 150, 300, 100), "adInitializedComplete : " + adInitializedComplete);
GUI.Label(new Rect(300, 175, 300, 100), "adStarted : " + adStarted);
}
}
Code : Tout sélectionner
panel.SetActive(false);
Code : Tout sélectionner
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
//Debug.Log("Test Show Complete");
switch (showCompletionState)
{
case UnityAdsShowCompletionState.COMPLETED:
Debug.Log("The ad was successfully shown.");
//
// CODE TO REWARD THE GAMER
// Give coins etc.
panel.SetActive(false);
adStarted = false;
Advertisement.Load("supportad", this);
break;
Code : Tout sélectionner
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
AdDisplay.OnUnityAdsShowComplete (System.String adUnitId, UnityEngine.Advertisements.UnityAdsShowCompletionState showCompletionState) (at Assets/ScriptGameVal/AdDisplay.cs:90)
UnityEngine.Advertisements.Platform.Editor.EditorPlatform+<>c__DisplayClass14_0.<Show>b__1 (System.Object sender, UnityEngine.Advertisements.Events.FinishEventArgs e) (at Library/PackageCache/com.unity.ads@4.0.0/Runtime/Advertisement/Platform/Editor/EditorPlatform.cs:156)
UnityEngine.Advertisements.Placeholder.<CreatePlaceholder>b__15_1 () (at Library/PackageCache/com.unity.ads@4.0.0/Runtime/Advertisement/Platform/Editor/Placeholder.cs:132)
UnityEngine.Events.InvokableCall.Invoke () (at <ad50157ee00e45cdb3c8bd67012f8804>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <ad50157ee00e45cdb3c8bd67012f8804>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
J'ai surligné en rouge la partie qui cause problème et qui renvoie donc à la ligne
Code : Tout sélectionner
panel.SetActive(false);
Pourriez-vous m'aider à identifier le soucis ?
Bien à vous,
Bilu