Author Topic: Fading Out after LoadLevel() button press  (Read 2253 times)

Bentlman1981

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Fading Out after LoadLevel() button press
« on: August 24, 2014, 10:16:35 PM »
Hello all,

I have been trying to figure out for a day now how to press a button and have the panel fade to black AND THEN load the scene.

I have no problems fading in or out at all, it's the instant jump on Appliction.LoadLevel before the tween can play. Is there a way to have it wait for the fade to finish first?

I have looked and searched everywhere. If there is a way and somebody could be kind enough to explain it in C# terms (as this is the language I am learning) I would be eternally grateful.

Apologies for my ignorance. I've searched all day for the answer to this and any topics discussing anything remotely close to this has been out of my scope of understanding.


EDIT :

I have accomplished this but is this considered correct?

using UnityEngine;
using System.Collections;


public class StartButton : MonoBehaviour {

   
       
        public string levelName;    //Scene to load
   public GameObject fader;   //Fading Object (Right now it's a black sprite)


   void OnClick()
   {
      TweenAlpha.Begin (fader, 1, 1);
      Invoke("LoadScene", 1);
         
      }

   void LoadScene() {
      Application.LoadLevel(levelName);
   }

   
}
« Last Edit: August 25, 2014, 08:32:51 AM by Bentlman1981 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fading Out after LoadLevel() button press
« Reply #1 on: August 26, 2014, 01:28:33 AM »
  1. void OnClick () { StartCoroutine(LoadScene()); }
  1. IEnumerator LoadScene ()
  2. {
  3.     UIRect rect = fader.GetComponent<UIRect>();
  4.     while (rect.alpha > 0f)
  5.     {
  6.         rect.alpha = rect.alpha - Time.deltaTime;
  7.         yield return null;
  8.     }
  9.     Application.LoadLevel(levelName);
  10. }