// Use this for initialization
    void Start()
    {
        //In case this object gets recreated, we need to make sure a few settings are cleared:
        AdGemPlugin.fullScreenEndCardShowing = AdGemPlugin.customEndCardShowing = AdGemPlugin.carouselEndCardShowing = false;



        videoPlayer = goAdGemVideoCamera.GetComponent <VideoPlayer>();
        videoPlayer.loopPointReached += VideoEndReached;
        videoPlayer.errorReceived    += VideoPlayerErrorReceived;
        audioSource             = gameObject.AddComponent <AudioSource>();
        vidoePlayerRawImage     = videoHolder.GetComponent <RawImage>();
        endCardImageHolderRect  = endCardImageObjectHolder.GetComponent <RectTransform>();
        endCardImageRect        = endCardImageObject.GetComponent <RectTransform>();
        iconImageRect           = iconImageObject.GetComponent <RectTransform>();
        iconMaskRect            = iconMaskObject.GetComponent <RectTransform>();
        videoHolderRect         = videoHolder.GetComponent <RectTransform>();
        customEndCardInfoBgRect = customEndCardInfoBg.GetComponent <RectTransform>();
        customEndCardPanelRect  = customEndCardPanel.GetComponent <RectTransform>();
        countdownRingBgRect     = countdownRingBg.GetComponent <RectTransform>();
        countdownRingrect       = countdownRing.GetComponent <RectTransform>();
        blackBgRect             = goBlackBg.GetComponent <RectTransform>();
        countdownTextRect       = countdownTextObject.GetComponent <RectTransform>();
        countdownText           = countdownTextObject.GetComponent <Text>();
        countdownRingImage      = countdownRing.GetComponent <Image>();
        titleText                = goTitleText.GetComponent <Text>();
        titleTextRect            = goTitleText.GetComponent <RectTransform>();
        descriptionText          = goDescriptionText.GetComponent <Text>();
        descriptionTextRect      = goDescriptionText.GetComponent <RectTransform>();
        starsRect                = goStars.GetComponent <RectTransform>();
        downloadButtonRect       = goDownloadButton.GetComponent <RectTransform>();
        downloadButtonText       = goDownloadButtonText.GetComponent <Text>();
        downloadButtonTextRect   = goDownloadButtonText.GetComponent <RectTransform>();
        reviewCountRect          = goReviewCount.GetComponent <RectTransform>();
        reviewCountText          = goReviewCount.GetComponent <Text>();
        adgemLogoRect            = goAdgemLogo.GetComponent <RectTransform>();
        iconImage                = iconImageObject.GetComponent <Image>();
        endCardImage             = endCardImageObject.GetComponent <Image>();
        xButtonClickableAreaRect = xButtonCLickableAreaObject.GetComponent <RectTransform>();
        xButtonRect              = xButtonObject.GetComponent <RectTransform>();
        adGemWebview             = goWebview.GetComponent <AdGemWebview>();



        AdGemPlugin.initPrefabElements(videoPlayer, audioSource, videoHolderRect, vidoePlayerRawImage, iconImageRect, blackBgRect, iconMaskRect, countdownRingBgRect,
                                       countdownRingrect, countdownText, countdownTextRect, countdownRingImage, titleText, titleTextRect, descriptionText, descriptionTextRect, starsRect, downloadButtonRect,
                                       downloadButtonText, downloadButtonTextRect, reviewCountText, adgemLogoRect, iconImage, xButtonRect, xButtonClickableAreaRect, goWebview, goAdGemCanvas, goAdGemVideoCamera, xButtonObject);
    }
    public void playVideo(bool rewarded)
    {
        if (AdGem.verboseLogging)
        {
            Debug.Log("Adgem ----()-----() playVideo ()-----()-----");
        }

        if (AdGem.verboseLogging)
        {
            Debug.Log("Adgem standard? " + rewarded);
        }
        if (AdGem.verboseLogging)
        {
            Debug.Log("Adgem end type? " + AdGemPlugin.standardVideoEndType);
        }

        if (AdGemPlugin.videoIsPlaying)
        {
            if (AdGem.verboseLogging)
            {
                Debug.Log("Error: You cannot start a video ad when another ad is already playing");
            }
            return; //Make sure one video stops playing before another starts.
        }

        //Reposition the elements in case the resolution has changed
        AdGemPlugin.initPrefabElements(videoPlayer, audioSource, videoHolderRect, vidoePlayerRawImage, iconImageRect, blackBgRect, iconMaskRect, countdownRingBgRect,
                                       countdownRingrect, countdownText, countdownTextRect, countdownRingImage, titleText, titleTextRect, descriptionText, descriptionTextRect, starsRect, downloadButtonRect,
                                       downloadButtonText, downloadButtonTextRect, reviewCountText, adgemLogoRect, iconImage, xButtonRect, xButtonClickableAreaRect, goWebview, goAdGemCanvas, goAdGemVideoCamera, xButtonObject);

        AdGemPlugin.rewardMode = rewarded;

        //This is logic to skip the video if the end type is "no video"
        if (!rewarded && AdGemPlugin.standardVideoEndType == AdGemPlugin.END_TYPE_NO_VIDEO)
        {
            if (AdGem.verboseLogging)
            {
                Debug.Log("Image only ad type - playVideo fired. *****");
            }
            goAdGemCanvas.SetActive(true);
            endCardImageObject.SetActive(true);
            currentVideoEndType = AdGemPlugin.END_TYPE_NO_VIDEO;
            AdGemPlugin.fullScreenEndCardShowing = true;
            StartCoroutine(MonitorScreenSize());
            VideoEndReached(videoPlayer);
            AdGemPlugin.interstitialVideoReady = false;
            return;
        }

        xButtonObject.SetActive(false);
        customEndCardPanel.SetActive(false);


        // Start playback. This means the VideoPlayer may have to prepare (reserve
        // resources, pre-load a few frames, etc.). To better control the delays
        // associated with this preparation one can use videoPlayer.Prepare() along with
        // its prepareCompleted event.
        try
        {
            bool isIos =
#if UNITY_IPHONE
                true;
#else
                false;
#endif
            videoPlayer.source = VideoSource.Url;
            if (rewarded)
            {
                if (AdGemPlugin.rewardVideoReady)
                {
                    currentVideoEndType = AdGemPlugin.rewardVideoEndType;
                    videoPlayer.url     = Application.persistentDataPath + "/" + AdGemPlugin.rewardVideoFilename;
                    titleText.text      = AdGemPlugin.rewardVideoName;

                    if (titleText.text.Length > 45)
                    {
                        titleText.fontSize = AdGemPlugin.smallerTitleTextFontSize;
                    }
                    else
                    {
                        titleText.fontSize = AdGemPlugin.standardTitleTextFontSize;
                    }

                    reviewCountText.text    = "(" + AdGemPlugin.rewardVideoReviewCount.ToString() + ")";
                    downloadButtonText.text = AdGemPlugin.rewardVideoButtonText;
                    if (AdGemPlugin.rewardVideoExitButtonSeconds >= 0)
                    {
                        StartCoroutine(waitThenTurnOnXButton((float)AdGemPlugin.rewardVideoExitButtonSeconds));
                    }
                    if (AdGemPlugin.rewardVideoEndType == AdGemPlugin.END_TYPE_CAROUSEL)
                    {
                        AdGemPlugin.loadCarouselData(rewarded, isIos, carouselContainerRect.gameObject, newEndType =>
                        {
                            if (!AdGemPlugin.customEndCardShowing && !AdGemPlugin.fullScreenEndCardShowing)
                            {
                                currentVideoEndType = newEndType;
                            }
                        });
                    }
                }
                else
                {
                    if (AdGem.verboseLogging)
                    {
                        Debug.Log("Adgem--- Reward Video not ready");
                    }
                    return;
                }
            }
            else
            {
                if (AdGemPlugin.interstitialVideoReady)
                {
                    //Debug.Log("standard video was ready!");

                    currentVideoEndType = AdGemPlugin.standardVideoEndType;
                    videoPlayer.url     = Application.persistentDataPath + "/" + AdGemPlugin.standardVideoFilename;
                    titleText.text      = AdGemPlugin.standardVideoName;

                    if (titleText.text.Length > 45)
                    {
                        titleText.fontSize = AdGemPlugin.smallerTitleTextFontSize;
                    }
                    else
                    {
                        titleText.fontSize = AdGemPlugin.standardTitleTextFontSize;
                    }

                    reviewCountText.text    = "(" + AdGemPlugin.standardVideoReviewCount.ToString() + ")";
                    downloadButtonText.text = AdGemPlugin.standardVideoButtonText;
                    if (AdGemPlugin.standardVideoExitButtonSeconds >= 0)
                    {
                        StartCoroutine(waitThenTurnOnXButton((float)AdGemPlugin.standardVideoExitButtonSeconds));
                    }
                    if (AdGemPlugin.standardVideoEndType == AdGemPlugin.END_TYPE_CAROUSEL)
                    {
                        AdGemPlugin.loadCarouselData(rewarded, isIos, carouselContainerRect.gameObject, newEndType =>
                        {
                            if (!AdGemPlugin.customEndCardShowing && !AdGemPlugin.fullScreenEndCardShowing)
                            {
                                currentVideoEndType = newEndType;
                            }
                        });
                    }
                }
                else
                {
                    if (AdGem.verboseLogging)
                    {
                        Debug.Log("Adgem--- Standard Video not ready");
                    }
                    return;
                }
            }



            StartCoroutine(prepareThenPlayVideo());
        }
        catch (Exception ex)
        {
            if (AdGem.verboseLogging)
            {
                Debug.Log("Adgem--- " + ex.ToString());
            }
        }
    }