c# - Saving PlayerPrefs for multiple instances -


i have setup simple shop when player clicks 'buy' character, item gets unlocked , stays saved unlocked. in 1 scene (shop scene).

i have second scene character gets unlocked based on purchase player able select (character scene).

i placing scripts on empty gameobjects on each scene. works fine when buying 1 character. how replicate multiple characters.

i place scripts on individual buttons , place corresponding gameobjects under inspector not practical if have 100 characters.

please advice how make work across multiple instances. thought of tagging , doesn't seem feasible. open suggestions if there better way of doing this. thanks.

//class controlling shop scene via empty gameobject  public class shopmanager : monobehaviour   {      private bool unlocked;       public gameobject greyimg;       void start()      {          unlocked = playerprefs.getint("unlocked") == 1;          greyimg.setactive(unlocked);      }       public void buy()      {          unlocked = true;          playerprefs.setint("unlocked", 1);          playerprefs.save();          greyimg.setactive(true);      }  } 

this how unity setup looks shop scene. if item bought, grey image set active not allowing user click green buy button more.

enter image description here

when character unlocked/bought

enter image description here

 //class controlling character select scene via empty gameobject  public class charactermanager : monobehaviour   {      private bool unlocked;      public gameobject greyselect;       void start()      {          unlocked = playerprefs.getint("unlocked") == 1;      }       void update()      {          if (unlocked)          {              greyselect.setactive(false);          }      }  } 

this how unity setup looks character select scene. if character unlocked, grey select image set inactive , orange select button visible allowing character selected.

enter image description here

when character unlocked

enter image description here

there many ways in tackle problem. here's one;

you're going need separate player prefs entry each character. so, you'll need nice way keep track of characters have , unlock state. instead of saving 'unlocked' in player prefs, why not create class contains unlocked information?

class unlockedcharacters {     bool characteraunlocked = false;     bool characterbunlocked = false;     bool charactercunlocked = true; } 

you can serialize whole class , save whole class inside player prefs. then, when load game can load class player prefs populate character information. way, data managed , consistent across saved states.

you go 1 step further , keep relating characters inside of dictionary whereby int enum referring character , bool unlock state. can save/load dictionary again using player prefs.

inside class have helper methods generic gameobject scripts call keep things nice , encapsulated.

this way, individual gameobjects handle specific characters can hold reference enum , can use general script set/modify data contents based on enum field can set via inspector, or in initialisation code object.

edit comment below - example:

your generic character controller go on individual objects, , changing char_type same script work unlock multiple characters.

class charactermanager {     public enum char_type = { charactera, characterb, characterc }     private dictionary<char_type, bool> characterunlockstate;      void start()     {         // seeding data example purposes         characterunlockstate = new dictionary<char_type, bool>();         characterunlockstate.add(char_type.charactera, false);         characterunlockstate.add(char_type.characterb, false);         characterunlockstate.add(char_type.characterc, true);     }      public bool ischaracterunlocked(char_type character)     {         if (characterunlockstate.contains(character)) return characterunlockstate[character];          return false;     }      public void unlockcharacter(char_type character)     {         if (characterunlockstate.contains(character)) characterunlockstate[character] = true;     } }  class genericcharactercontroller {     public char_type character;     public charactermanager manager;      public void unlockbuttonpressed()     {         manager.unlockcharacter(character);     }  } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -