Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: stingman on May 19, 2012, 08:41:11 PM

Title: Depending on which button is clicked, load a specific level
Post by: stingman on May 19, 2012, 08:41:11 PM
Hey, I wanted to know how I can achieve this with NGUI.  I have 50 buttons on my menu.  Each button will load a specific level in the game if it is clicked.  But I don't want to write 50 different scripts relating to each button.  This is my idea... but it's not working.  Is there an easy way to go about doing this... like if "this button is clicked"?

using UnityEngine;
using System.Collections;

public class MenuButtonLoadLevel : MonoBehaviour {
   
   public GameObject[] button;
   
   void OnClick() {
      
      if (button[0])
         Application.LoadLevel(3);
      if (button[1])
         Application.LoadLevel(4);
      if (button[2])
         Application.LoadLevel(5);
      if (button[3])
         Application.LoadLevel(6);
      if (button[4])
         Application.LoadLevel(7);
      if (button[5])
         Application.LoadLevel(8);
}
}

Title: Re: Depending on which button is clicked, load a specific level
Post by: ArenMook on May 19, 2012, 10:22:37 PM
  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("Game/Load Level on Click")]
  4. public class LoadLevelOnClick : MonoBehaviour
  5. {
  6.         public string levelName;
  7.  
  8.         void OnClick ()
  9.         {
  10.                 if (!string.IsNullOrEmpty(levelName))
  11.                 {
  12.                         NGUITools.Broadcast("End");
  13.                         Application.LoadLevel(levelName);
  14.                 }
  15.         }
  16. }