Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MartinDaniel · Jul 25, 2016 at 11:04 PM · androidunity5playerprefs

Write on Player Prefs from Unity and read Shared Preferences from Android returns wrong value

I'm working on a Unity game exported to Android. I have an Activity to save values on SharedPreferences that will be retrieve from Unity Activity and updated from PlayerPrefs. Also these values will be read from SharedPreferences from the Main Activity.

The values that I get not match with the values that I write on the main activity and only reads when I close the entire application.

Here is my code and how actually works.

The Main Activity save a value on Shared Preferences and launch the UnityPlayerActivity.

MainActivity

 public void writeOnSharedAndLaunch(String text) {
         String sharedPreferenceName = context.getPackageName() + ".v2.playerprefs";
         SharedPreferences sharedPreferences = context.getSharedPreferences(sharedPreferenceName, MODE_PRIVATE);;
         Editor editor = sharedPreferences.edit();
 
         editor.putString("PlayerName", playerText.getText().toString());
         editor.apply();
 
         Intent intent = new Intent(context, UnityPlayerActivity.class);
         startActivity(intent);
     }

From Unity I get the value from PlayerPrefs without problems, but when I update the preference on PlayerPrefs and return to Main Activity the value is the same.

UnityPlayerActivity

I completely close the application and I open it again.

And as you can see the value changes but the spaces are replaced with %20 value.

UnityPlayerActivity UnityPlayerActivity

This is my code from Unity Controller:

 public void saveOnDataFromShared(string name) {
     PlayerPrefs.SetString("PlayerName", name);
     textShared.text = name;
 }
 public void loadStringFromPlayerPrefs() {
     textShared.text = PlayerPrefs.GetString ("PlayerName", "Default value");
 }

I can't figure it out what happen on the process, any help is appreciated.

screen-shot-2016-07-25-at-33926-pm.png (44.0 kB)
screen-shot-2016-07-25-at-34342-pm.png (19.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alxcancado · Jan 30, 2018 at 06:33 PM 0
Share

Help!

How can another native android app write values to my Unity PlayerPrefs? I guess in android this preferences files are all stored as SharedPreferences.

I did this within iOS but no clue on how to do this in Android!

Can someone elaborate how can I get similar thing as this?

Thank's!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by liortal · Jul 26, 2016 at 04:30 AM

PlayerPrefs stores its data in a file - as you already know on Android it uses SharedPreferences.

When saving values, Unity doesn't automatically write the changrs back to file. They are kept in memory until you "flush" them by calling PlayerPrefs.Save

This is called automatically when you quit the game, so what you described makes sense.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MartinDaniel · Jul 26, 2016 at 02:37 PM 0
Share

Thanks for the answer, I didn't notice this function in the documentation.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs works fine in Unity Editor but not on Android 0 Answers

issue [FAILED BINDER TRANSACTION] android unity3d. JNI ERROR (app bug): accessed stale local reference 0 Answers

Playerprefs being cleared on some devices when the phone is restarted...why!? 2 Answers

CommandInvokationFailure: Gradle build failed. with OneSignal and google analytics 1 Answer

I have trouble with Player Prefs. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges