Skip to main content
added 1242 characters in body
Source Link
McAden
  • 594
  • 3
  • 7

Something like the following. I'd attach it individually to each button and then set "TargetScene" for each as well. This should work for most of them though you'd have to do something slightly different for "Instruction" and "Exit"

public class SceneMenuItemController : MonoBehaviour
{
    public string TargetScene;

    private float triggerEnterTime = 0;

    void OnMouseUpAsButton()
    {
        if (!string.IsNullOrEmpty(TargetScene))
        {
            Application.LoadLevel(TargetScene);
        }
    }
    
    void OnTriggerEnter2D(Collider2D col)
    {
        triggerEnterTime = Time.time;
    }
    
    void OnTriggerStay2D(Collider2D col)
    {
        // Too soon?
        if(Time.time - triggerEnterTime < 3)
        {
            // Might want to do something to indicate graphically that you're holding
            //     like a progress bar. For now, simply abort for this frame.
            return;
        }

        if (!string.IsNullOrEmpty(TargetScene))
        {
            Application.LoadLevel(TargetScene);
        }
    }
}

Something like the following. I'd attach it individually to each button and then set "TargetScene" for each as well. This should work for most of them though you'd have to do something slightly different for "Instruction" and "Exit"

public class SceneMenuItemController : MonoBehaviour
{
    public string TargetScene;

    private float triggerEnterTime = 0;

    void OnMouseUpAsButton()
    {
        if (!string.IsNullOrEmpty(TargetScene))
        {
            Application.LoadLevel(TargetScene);
        }
    }
    
    void OnTriggerEnter2D(Collider2D col)
    {
        triggerEnterTime = Time.time;
    }
    
    void OnTriggerStay2D(Collider2D col)
    {
        // Too soon?
        if(Time.time - triggerEnterTime < 3)
        {
            // Might want to do something to indicate graphically that you're holding
            //     like a progress bar. For now, simply abort for this frame.
            return;
        }

        if (!string.IsNullOrEmpty(TargetScene))
        {
            Application.LoadLevel(TargetScene);
        }
    }
}
Source Link
McAden
  • 594
  • 3
  • 7

Your WaitTime Coroutine will not pause the evaluation of the following code. The Coroutine will be kicked off and logic will immediately continue. Your Coroutine is waiting 3 seconds and will call that print function wherever it resides but that's all it will do.

You should probably go with something like:

private float triggerEnterTime = 0;

void OnTriggerEnter2D(Collider2D col)
{
    triggerEnterTime = Time.time;
}

void OnTriggerStay2D(Collider2D col)
{
    // Too soon?
    if(Time.time - triggerEnterTime < 3)
    {
        // Might want to do something to indicate graphically that you're holding
        //     like a progress bar. For now, simply abort for this frame.
        return;
    }

    // Has been held for 3 seconds, evaluate it
    if(col.gameObject.tag == "Button")
    {
        btnName = col.gameObject.name;
        switch(btnName)
        {
            case "musicBtn":
                Application.LoadLevel("Menu_Music");
                break;

            case "startBtn":
                Application.LoadLevel("Menu_Select");
                break;

            case "instructBtn":
                print("Instruction Clicked");
                break;

            case "exitBtn":
                Application.Quit();
                break;

            case "themeBtn":
                Application.LoadLevel("Menu_Theme");
                break;

            case "backBtn":
                Application.LoadLevel("Menu_LoadScreen");
                break;
        }
    }
}

That should do the trick but something that might be cleaner is to define a "MenuItemBehavior" that will handle itself rather than some master switch statement.