I have a Unity game exported into Android Studio. I have a list of saved games that stores each game's last scene that the player played. Basically stores the player's progression.
The writing of the last scene played from Unity to Android Studio works great. However, I don't know how to read from Android Studio in Unity. I have a putExtra intent that should pass the scene index into Unity but I don't know how. I should also probably mention that this java script is the only one I wrote, meaning I don't have other methods that are not mentioned here.
I found a code online that does that but I don't know to modify it so it would work with my Java script. I would really appreciate it if somebody could help!
My Java script (in Android Studio):
public class MainActivity extends AppCompatActivity {
private int lastscene = 99;
Button btnstart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//gets the PlayerPrefs from Unity
SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + ".v2.playerprefs", Context.MODE_PRIVATE);
//Saves the last played scene (which is stores in the PlayerPrefs we got) in "lastscene"
lastscene = sharedPreferences.getInt("Scene",99);
//The reading from Unity works! Toasts the last played scene when you open the Android Studio app
Toast.makeText(getApplicationContext(), ""+lastscene,
Toast.LENGTH_LONG).show();
btnstart = (Button) findViewById(R.id.btnstart);
//Once you press the button, Unity should open and start playing from the last scene that we saved into Android Studio.
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, UnityPlayerActivity.class);
i.putExtra("listScene", lastscene);
startActivity(i);
}
});
}
}
C# that I found online and don't know how to make it work (Unity):
private void Awake ()
{
getIntentData ();
}
private bool getIntentData () {
#if (!UNITY_EDITOR && UNITY_ANDROID)
return CreatePushClass (new AndroidJavaClass ("com.unity3d.player.UnityPlayer"));
#endif
return false;
}
public bool CreatePushClass (AndroidJavaClass UnityPlayer) {
#if UNITY_ANDROID
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject> ("getIntent");
AndroidJavaObject extras = GetExtras (intent);
if (extras != null) {
string ex = GetProperty (extras, "listScene");
return true;
}
#endif
return false;
}
private AndroidJavaObject GetExtras (AndroidJavaObject intent) {
AndroidJavaObject extras = null;
try {
extras = intent.Call<AndroidJavaObject> ("getExtras");
} catch (Exception e) {
Debug.Log (e.Message);
}
return extras;
}
private string GetProperty (AndroidJavaObject extras, string name) {
string s = string.Empty;
try {
s = extras.Call<string> ("getString", name);
} catch (Exception e) {
Debug.Log (e.Message);
}
return s;
}
Credit to where I took the Unity script from: Credit