Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Level unlock system

Discussion in 'iOS and tvOS' started by MaxUylings, Oct 1, 2012.

  1. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    Hey guys,

    I am working on a level unlocking system for my iOS game. At this moment I got it working but I was wondering what you guys thought about my approach. I am asking you for feedback because I am fairly new to unity and programming in general and would very much like to improve my skills.
    This is the code I have used. There is more in these scripts but to make it easier to read I have deleted some of the code that is not relevant to my question.

    I have put this code on my first scene
    Code (csharp):
    1. #pragma strict
    2.  
    3. var numberOfLevels : int;
    4.  
    5. private var levels : int[];
    6.  
    7.  
    8. function Start () {
    9.  
    10. levels = new int [numberOfLevels];
    11.     for(var i :int = 1; i<levels.Length;i++){
    12.         if (PlayerPrefs.GetInt("Level"+(i+1)) == null){
    13.             PlayerPrefs.SetInt("Level"+(i+1),0);
    14.         }
    15.     }
    16.  
    17. }
    I have put the following code on a trigger at the end of all my levels:
    Code (csharp):
    1. #pragma strict
    2. var numberOfLevels : int;
    3.  
    4.  
    5. function Start () {
    6. }
    7.  
    8. function Update () {
    9. }
    10.  
    11. function OnTriggerEnter(collision : Collider){
    12. NextLevel();
    13. }
    14.  
    15. function NextLevel(){
    16. UnlockLevel();
    17. }
    18.  
    19. function UnlockLevel(){
    20. for (var i : int = 1; i < numberOfLevels; i++){
    21.     if(Application.loadedLevelName == "Level"+i){
    22.     PlayerPrefs.SetInt("Level"+(i+1),1);
    23.     }
    24. }
    25. }
    And have put the following script on my levelselect menu:

    Code (csharp):
    1. #pragma strict
    2. var numberOfLevelsWorld01 : int;
    3.  
    4.  
    5. function Start () {
    6.     for(var i : int = 1; i<numberOfLevelsWorld01; i++){
    7.         if(PlayerPrefs.GetInt("Level"+(i+1))==1){
    8.             GameObject.Find("Level"+(i+1)).GetComponent(BoxCollider).enabled= true;
    9.             GameObject.Find("ButtonLocked"+(i+1)).active = false;
    10.         }
    11.  
    12.     }
    13. }
    Thank in advance!
    Kind regards,
    Max Uijlings
     
  2. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Looks good to me. I like how you locked your level buttons. Might have to keep this in mind when I do something with levels.
     
  3. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    Thanks good to hear I'm on the right track. :D
     
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    I did something real similar with my game. Looks good, and this will work well. Good thinking :)

    And then you may want to darken the unavailable levels so they look different or something.
     
  5. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    Thanks for your feedback. At this moment I have a different texture representing the button being disabled. Would you say I should darken that out as well?
     
  6. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    I would, but do what you think looks best :)
     
  7. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    oke thanks. I will experiment a little with it. I'll let you know what I ended up with :)

    Kind Regards,
    Max
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    I use a stageIndex and levelIndex of type int for each level and its managers.

    Code (csharp):
    1.  
    2. PlayerPrefs.SetInt("levelUnlocked" + stageIndex.ToString() + levelIndex.ToString());
    3.  
    Code (csharp):
    1.  
    2. LevelUnlocked = PlayerPrefs.GetInt("levelUnlocked" + stageIndex.ToString() + levelIndex.ToString());
    3.  
     
  9. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    @renman3000 Thank you very much for this solution. It definitely made my code a lot cleaner. :)

    Kind Regards,
    Max
     
  10. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    I personally would have used PlayerPrefs.HasKey("levelname"), because it returns a boolean.. Also, you've got an empty update loop in there that should be removed ;)
     
  11. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    As pointed out by a fellow board member tho, I am going to modify it to be more like this, to prevent situation where stage 1 level index 11 don't get confusd with stage index 11 level 1.


    Code (csharp):
    1.  
    2. PlayerPrefs.SetInt("levelUnlocked" + stageIndex.ToString() + ":" + levelIndex.ToString());
    3.  
     
  12. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    hahahaha yeah good point I'll change that right away.
     
  13. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    Thanks for the advise. And indeed you are correct about the update function. I accidentally missed it. But off course I know that I should not have empty update functions in my code. ;)
     
  14. MaxUylings

    MaxUylings

    Joined:
    May 30, 2012
    Posts:
    66
    Oke guys so I changed the scripts to how I think it is good. I implemented your suggestions and ended up with the following.

    In my first script at the beginning of the game I put the following code:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. static var worlds : int = 2;
    5. static var levels : int = 18;
    6.  
    7. private var worldIndex : int;
    8. private var levelIndex : int;
    9.  
    10.  
    11. function Start () {
    12. PlayerPrefs.DeleteAll();
    13. LockLevels();
    14. }
    15.  
    16.  
    17. function LockLevels(){
    18.  
    19.     for (var i : int = 0; i < worlds; i++){
    20.         for (var j : int = 1; j < levels; j++){
    21.             worldIndex  = (i+1);
    22.             levelIndex  = (j+1);
    23.             if(!PlayerPrefs.HasKey("level"+worldIndex.ToString() +":" +levelIndex.ToString())){
    24.                 PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),0);
    25.             }
    26.        
    27.         }
    28.     }
    29.  
    30. }
    31.  
    In the second script in my levelSelectMenu I have the following code:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4.  
    5. private var worldIndex : int;
    6. private var levelIndex : int;
    7.  
    8. function Start () {
    9.     for(var i : int = 1; i<=TempLevelSystem.worlds; i++){
    10.         if(Application.loadedLevelName == "World"+i){
    11.             worldIndex = i;
    12.             CheckLockedLevels();
    13.  
    14.         }
    15.  
    16.     }
    17.  
    18. }
    19.  
    20. function CheckLockedLevels(){
    21.     for(var j : int = 1; j<TempLevelSystem.levels; j++){
    22.         levelIndex = (j+1);
    23.         Debug.Log(""+worldIndex +levelIndex);
    24.         if((PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1){
    25.             GameObject.Find("Level"+worldIndex.ToString() +"." +levelIndex.ToString()).GetComponent(BoxCollider).enabled= true;
    26.             GameObject.Find("ButtonLocked"+(j+1)).active = false;
    27.         }
    28.     }
    29. }
    And the last script that will unlock the next level I put this code in:
    Code (csharp):
    1.  
    2. #pragma strict
    3. var level : String;
    4. private var worldIndex : int;
    5. private var levelIndex : int;
    6. private var currentLevel : String;
    7.  
    8.  
    9.  
    10. function Start () {
    11. currentLevel = Application.loadedLevelName;
    12. }
    13.  
    14. function OnTriggerEnter(collision : Collider){
    15.     NextLevel();
    16. }
    17.  
    18. function NextLevel(){
    19.     UnlockLevel();
    20.     Application.LoadLevel(""+level);
    21. }
    22.  
    23. function UnlockLevel(){
    24.     for(var i : int = 0; i<TempLevelSystem.worlds; i++){
    25.         for(var j : int = 1; j<TempLevelSystem.levels; j++){               
    26.             if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
    27.                 worldIndex  = (i+1);
    28.                 levelIndex  = (j+1);
    29.                 PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
    30.             }
    31.         }
    32.     }
    33. }
    34.  
    Again I have removed the code that is not relevant to this thread.

    I hope you guys think I did a better job this time and that I have implemented your advice like you intended.
    Your help has been very much appreciated. :D

    Kind Regards,
    Max Uijlings
     
  15. Sillan

    Sillan

    Joined:
    Feb 12, 2013
    Posts:
    2
    I just discovered this-

    First, you need to place this on an empty game object in your scene. It will provide 8 levels.
    You also will need locked and unlocked icons going from 1-8.
    Put this on the GameObject :

    #pragma strict

    var level1_locked:GameObject;
    var level1_unlocked:GameObject;
    var level2_locked:GameObject;
    var level2_unlocked:GameObject;
    var level3_locked:GameObject;
    var level3_unlocked:GameObject;
    var level4_locked:GameObject;
    var level4_unlocked:GameObject;
    var level5_locked:GameObject;
    var level5_unlocked:GameObject;
    var level6_locked:GameObject;
    var level6_unlocked:GameObject;
    var level7_locked:GameObject;
    var level7_unlocked:GameObject;
    var level8_locked:GameObject;
    var level8_unlocked:GameObject;

    var YouWin:String;


    var levelReached1 : int = 0;


    levelReached1 = PlayerPrefs.GetInt("SavedLevel1");

    function Update(){





    if(levelReached1 == 0)

    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = true;
    level2_unlocked.active = false;

    level3_locked.active = true;
    level3_unlocked.active = false;

    level4_locked.active = true;
    level4_unlocked.active = false;

    level5_locked.active = true;
    level5_unlocked.active = false;

    level6_locked.active = true;
    level6_unlocked.active = false;

    level7_locked.active = true;
    level7_unlocked.active = false;

    level8_locked.active = true;
    level8_unlocked.active = false;

    }
    if(levelReached1 == 1)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = true;
    level3_unlocked.active = false;

    level4_locked.active = true;
    level4_unlocked.active = false;

    level5_locked.active = true;
    level5_unlocked.active = false;

    level6_locked.active = true;
    level6_unlocked.active = false;

    level7_locked.active = true;
    level7_unlocked.active = false;

    level8_locked.active = true;
    level8_unlocked.active = false;


    }
    if(levelReached1 == 2)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = false;
    level3_unlocked.active = true;

    level4_locked.active = true;
    level4_unlocked.active = false;

    level5_locked.active = true;
    level5_unlocked.active = false;

    level6_locked.active = true;
    level6_unlocked.active = false;

    level7_locked.active = true;
    level7_unlocked.active = false;

    level8_locked.active = true;
    level8_unlocked.active = false;

    }
    if(levelReached1 == 3)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = false;
    level3_unlocked.active = true;

    level4_locked.active = false;
    level4_unlocked.active = true;

    level5_locked.active = true;
    level5_unlocked.active = false;

    level6_locked.active = true;
    level6_unlocked.active = false;

    level7_locked.active = true;
    level7_unlocked.active = false;

    level8_locked.active = true;
    level8_unlocked.active = false;

    }
    if(levelReached1 == 4)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = false;
    level3_unlocked.active = true;

    level4_locked.active = false;
    level4_unlocked.active = true;

    level5_locked.active = false;
    level5_unlocked.active = true;

    level6_locked.active = true;
    level6_unlocked.active = false;

    level7_locked.active = true;
    level7_unlocked.active = false;

    level8_locked.active = true;
    level8_unlocked.active = false;
    }
    if(levelReached1 == 5)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = false;
    level3_unlocked.active = true;

    level4_locked.active = false;
    level4_unlocked.active = true;

    level5_locked.active = false;
    level5_unlocked.active = true;

    level6_locked.active = false;
    level6_unlocked.active = true;

    level7_locked.active = true;
    level7_unlocked.active = false;

    level8_locked.active = true;
    level8_unlocked.active = false;
    }
    if(levelReached1 == 6)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = false;
    level3_unlocked.active = true;

    level4_locked.active = false;
    level4_unlocked.active = true;

    level5_locked.active = false;
    level5_unlocked.active = true;

    level6_locked.active = false;
    level6_unlocked.active = true;

    level7_locked.active = false;
    level7_unlocked.active = true;

    level8_locked.active = true;
    level8_unlocked.active = false;
    }
    if(levelReached1 == 7)
    {
    level1_locked.active = false;
    level1_unlocked.active = true;

    level2_locked.active = false;
    level2_unlocked.active = true;

    level3_locked.active = false;
    level3_unlocked.active = true;

    level4_locked.active = false;
    level4_unlocked.active = true;

    level5_locked.active = false;
    level5_unlocked.active = true;

    level6_locked.active = false;
    level6_unlocked.active = true;

    level7_locked.active = false;
    level7_unlocked.active = true;

    level8_locked.active = false;
    level8_unlocked.active = true;
    }
    if(levelReached1 == 8)
    {
    Application.LoadLevel(YouWin);
    }

    }


    Last of all you need to place this bit of code where you want to unlock a level.

    PlayerPrefs.SetInt("SavedLevel1", 1);
    Debug.Log("SavedLeve1 = 1");
    yield WaitForSeconds(2.0);
    //maybe have Application.LoadLevel("Whatever level you want to load");

    do this with each level but change the places where it has the number 1 to 2 when you want to unlock level 2 and the same for 3-8.

    I hope this helped.
    Johnny ;D
     
  16. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    I would recommend using arrays rather than numbers variables.
     
  17. ptdnet

    ptdnet

    Joined:
    Apr 20, 2011
    Posts:
    100
    Good lord yes... how does that solution work with 255 levels?