Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Does iOS support PlayerPrefs class?

Discussion in 'iOS and tvOS' started by liudibo, Feb 6, 2011.

  1. liudibo

    liudibo

    Joined:
    Dec 9, 2010
    Posts:
    75
    I did not find any information about iOS in the script reference of this class. If not, how to share information among different levels(Scenes)?
     
  2. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,572
    Yes, you can use PlayerPrefs on iOS.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You use DontDestroyOnLoad for sharing information between scenes, not PlayerPrefs. PlayerPrefs is for long-term storage such as preferences (of course), high scores, etc.

    --Eric
     
  4. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    I use a class called GameManager, which I made a singleton, to store data that I want to share between levels. Just make sure you have the following in your class:

    Code (csharp):
    1.  
    2. void Awake()
    3.     {
    4.         DontDestroyOnLoad(this);
    5.     }
    6.  
    I use PlayerPrefs to store high scores and data required to gain open feint achievements. I am also building to iOS.
     
  5. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    Here is my GameManager class if you want to use it:

    Code (csharp):
    1.  
    2. public sealed class GameManager : ScriptableObject {
    3.  
    4.     public int data1;
    5.     public int data2;
    6.     public string data3;
    7.  
    8.     GameManager()
    9.     {
    10.     }
    11.  
    12.     void Start () {
    13.        
    14.     }
    15.  
    16.     public void Init()
    17.     {
    18.        
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.     }
    25.  
    26.     void Awake()
    27.     {
    28.         DontDestroyOnLoad(this);
    29.     }
    30.  
    31.     public static GameManager Instance()
    32.     {
    33.         return Nested.instance;
    34.     }
    35.  
    36.     class Nested
    37.     {
    38.         static Nested()
    39.         {
    40.         }
    41.         internal static readonly GameManager instance = (GameManager)CreateInstance("GameManager");
    42.     }
    43. }
    44.  
    Attach the script to an empty game object (I made one called GameManager). You can now access it anywhere in your program, from any level, and the data will never be deleted.

    Code (csharp):
    1.  
    2. GameManager.Instance().data1 = 15;
    3. GameManager.Instance().data2 += 20;
    4. GameManager.Instance().data3 = "hello world";
    5.  
     
  6. liudibo

    liudibo

    Joined:
    Dec 9, 2010
    Posts:
    75
    Thanks very much for all helps, and more appreciate to "disband85" for sharing code.

    I think I'll use one "DontDestroyOnLoad" game object to share information.

    One more questions to disband85:
    What's the purpose to use a nested class instead of defining the instance variable in the manager class?
     
  7. disband85

    disband85

    Joined:
    Dec 7, 2010
    Posts:
    83
    There are several different ways to implement a singleton class in C#. I had never done it in C# before, only C++, so did a bit of research into it. The implementation above is fully lazy (the instance isn't created until it is needed), thread-safe, and performs well.

    The following website shows 5 different implementations of a singleton in C# and their subtle differences:

    http://csharpindepth.com/Articles/General/Singleton.aspx

    The one I used above is number 5.
     
  8. sumiguchi

    sumiguchi

    Joined:
    Mar 29, 2012
    Posts:
    4
  9. HuskyPanda213

    HuskyPanda213

    Joined:
    Mar 24, 2013
    Posts:
    37
    When using dont destroy on load make sure you use something to make it that there is not more than one of the object. This would work(This works in C# but im not sure about javascript or boo.)

    if (FindObjectsOfType(GetType()).Length > 1)
    {
    Destroy(gameObject);
    }



    Also try to never save playerprefs ever frame use it when the app is paused or before it quits, optimization is key.
     
  10. MohanadHamed

    MohanadHamed

    Joined:
    Sep 18, 2013
    Posts:
    3
    Thank you, this is the simplest way to pass variables between scenes.