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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing PlayerPrefs from Android native Java plugin

Discussion in 'Android' started by taxvi, Nov 13, 2015.

  1. taxvi

    taxvi

    Joined:
    Feb 18, 2013
    Posts:
    30
    I'm working with my exported Google Android Project in my Android Studio. I needed to access Unity's PlayerPrefs in my Java code. This is how it's done:

    Code (Java):
    1. //there are other ways you can get your app context, this is for using in static functions:
    2. Context appContext = UnityPlayer.currentActivity.getApplicationContext();
    3.  
    4. //PlayerPrefs: unity uses package name for preferences file ("com.example.app")
    5. SharedPreferences prefs = appContext.getSharedPreferences(appContext.getPackageName(), Context.MODE_PRIVATE);
    6.  
    7. //set stuff:
    8. prefs.edit().putInt("NumberOfShownAds", 0).commit();
    9.  
    10. //get stuff:
    11. prefs.getInt("NumberOfShownAds", 0);

    Source: this answer, but I spent some time finding it so hope this thread saved you some time getting your answers.
     
    Last edited: Jan 14, 2016
    defic and jprocha101 like this.
  2. defic

    defic

    Joined:
    Apr 29, 2013
    Posts:
    18
    for some reason, this is not working for me. No errors or anything, but nothing goes to playerprefs. I also tried to log all preferences, but nothing is being logged (and there should be a lot of data).


    Code (CSharp):
    1. Map<String,?> keys = prefs.getAll();
    2. Log.d("UnityAndroid", "All PlayerPrefs: ");
    3. for(Map.Entry<String,?> entry : keys.entrySet()){
    4.       Log.d("UnityAndroid",entry.getKey() + ": " +
    5.              entry.getValue().toString());
    6. }
     
  3. defic

    defic

    Joined:
    Apr 29, 2013
    Posts:
    18
    Okay, so first problem solved.
    Code (CSharp):
    1. Editor prefEditor = prefs.edit();
    2. prefEditor.putString("LaunchUrl", nosde.toString());
    3. prefEditor.putInt("number", 1337);
    4. prefEditor.commit();
    this is because edit() returns a new instance of an Editor, and you also have to commit your changes. But now I have a new issue. Unity saves my playerprefs to:
    /data/data/com.myapp.dev/shared_prefs/com.myapp.dev.v2.playerprefs.xml

    but my android plugin saves the data into:
    /data/data/com.myapp.dev/shared_prefs/com.myapp.dev.xml

    Code (CSharp):
    1. SharedPreferences prefs = appContext.getSharedPreferences(appContext.getPackageName() + ".v2.playerprefs", Context.MODE_PRIVATE);
    Changing it like that fixes it, but Im not sure if this is the right way to do it? Why the file name is +v2.playerprefs instead what the OP wrote?
     
  4. taxvi

    taxvi

    Joined:
    Feb 18, 2013
    Posts:
    30
    thanks @defic , I updated the top post

    that is strange, what Unity version do you use?
     
  5. defic

    defic

    Joined:
    Apr 29, 2013
    Posts:
    18
    I am using Unity 5.3.0f4. I will try again with a fresh project.

    EDIT: just tried with a fresh project, and the same thing, playerprefs file is named "com.your.app.v2.playerprefs.xml". Also there is a keyvalue pair in playerprefs:
    <int name="__UNITY_PLAYERPREFS_VERSION__" value="1" />, which may be related to this?

    How this is not documented anywhere? Too much wasted time. Must be tied to the unity version somehow, I guess? Also I am worried if they decide to change it again later and my plugins stop working.
     
    Last edited: Jan 14, 2016
    Ali_V_Quest likes this.
  6. taxvi

    taxvi

    Joined:
    Feb 18, 2013
    Posts:
    30
    exactly, man, I've used this code in my games. I hope once deployed they will work all the same. I tested this code on Unity 5.2.1 . I'll also need to fiddle with this later today, thanks for the bump.
     
    defic likes this.
  7. cmbellman

    cmbellman

    Joined:
    Sep 19, 2013
    Posts:
    20
    This is so messed up.. Fortunately I found your thread and could change in the android code, so I only lost a couple of hours debugging and head scratching. Now deep linking in my game is working again. But who knows when they will change again without documenting..?
     
    defic likes this.
  8. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    I don't know how unity made some mistake like this without considering the huge number of plugins that will stop working (AND without adding any notes or updating the docs)
     
    defic likes this.
  9. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    @cmbellman can you make a native android plugin to access the android shared preferences with any name?

    I have an android plugin that stores data in the old player prefs file & I need to read the data from unity
    - I'm willing to buy it for it 10$
    - (don't know if many will buy it or not, but if you have experience creating android plugins, I would love if you solve this problem for the community developers who face this issue)
     
    defic likes this.
  10. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138

    this plugin have a shared preferences feature which I think can be used to access the old player prefs data

    https://www.assetstore.unity3d.com/en/#!/content/38872
     
    taxvi likes this.