Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.1 kB
2
Indexable
Never
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class LeftClickDetection : MonoBehaviour
{
    private bool ButtonPressed = false;
    public GameObject PrimaryScene;
    public GameObject LandingScene;
    public GameObject ExtraObject1;
    public GameObject ExtraObject2;
    public GameObject ExtraObject3;
    public AudioSource BackgroundMusic;
    public AudioSource Scene_Background_Audio;

    public AudioSource Completed_Scene_Audio;
    public Text textObject;
    public GameObject IconObject;
    public Sprite Incomplete;
    public Sprite Unstarted;
    public Sprite Complete;
    public bool Var_InProgress = true;
    public Image BackgroundPanel;
    public int ScenesRemainingTally;
    public CanvasGroup canvasGroup; // Reference to the CanvasGroup you want to fade
    public float fadeDuration = 1.0f; // Duration of the fade

    void Start()
    {
        PrimaryScene.SetActive(false);
    }

    void Update()
    {
        if (!PrimaryScene.activeSelf && ScenesRemainingTally > 0)
        {
            textObject.color = new Color32(255, 255, 255, 255);
            BackgroundPanel.color =  new Color32(0, 86, 31, 255);
            Image iconImage = IconObject.GetComponent<Image>();
            iconImage.sprite = Unstarted;
            ButtonPressed = false;
            Scene_Background_Audio.volume = 0f;
        }
    }

    public void MyButtonFunction()
    {
        StartCoroutine(HandleButtonClick());
    }

    IEnumerator HandleButtonClick()
    {
        // Fade in
        yield return StartCoroutine(FadeInCanvasGroup());

        // Perform actions
        Debug.Log("Button has been Clicked");
        Image iconImage = IconObject.GetComponent<Image>();

        if (ButtonPressed == false)
        {
            PrimaryScene.SetActive(true);
            LandingScene.SetActive(false);
            ExtraObject1.SetActive(false);
            ExtraObject2.SetActive(false);
            ExtraObject3.SetActive(false);
            BackgroundMusic.volume = 0f;
            Scene_Background_Audio.volume = 1f;

            if (Var_InProgress)
            {
                Debug.Log("In Progress");
                textObject.color = new Color32(141, 199, 63, 255);
                BackgroundPanel.color = new Color32(29, 29, 27, 255);
                iconImage.sprite = Incomplete;
            }
        }
        else
        {
            BackgroundMusic.volume = 0.07f;
            Scene_Background_Audio.volume = 0.0f;
            PrimaryScene.SetActive(false);
            LandingScene.SetActive(true);
            ExtraObject1.SetActive(false);
            ExtraObject2.SetActive(false);
            ExtraObject3.SetActive(false);
        }

        ButtonPressed = !ButtonPressed;

        // Fade out
        yield return StartCoroutine(FadeOutCanvasGroup());
    }

    public void StartInProgress()
    {
        Var_InProgress = true;
    }

    public void TouchpointComplete()
    {
        ScenesRemainingTally--;

        if (ScenesRemainingTally <= 0)
        {
            BackgroundPanel.color = new Color32(193, 193, 193, 255);
            Image iconImage = IconObject.GetComponent<Image>();
            iconImage.sprite = Complete;
            textObject.color = new Color32(0, 86, 31, 255);
            Var_InProgress = false;
            Completed_Scene_Audio.Play();
        }
    }

    IEnumerator FadeInCanvasGroup()
    {
        float startTime = Time.time;
        while (Time.time < startTime + fadeDuration)
        {
            canvasGroup.alpha = Mathf.Lerp(0, 1, (Time.time - startTime) / fadeDuration);
            yield return null;
        }
        canvasGroup.alpha = 1;
    }

    IEnumerator FadeOutCanvasGroup()
    {
        float startTime = Time.time;
        while (Time.time < startTime + fadeDuration)
        {
            canvasGroup.alpha = Mathf.Lerp(1, 0, (Time.time - startTime) / fadeDuration);
            yield return null;
        }
        canvasGroup.alpha = 0;
    }
}