Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TheBloop · Mar 21, 2021 at 04:30 AM · androidcallandroidjavaobjectintent

Read intent from Android Studio in Unity

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 or classes 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 tried changing it but it made my game stop compiling because I just don't understand the script since I've never done things like that. I would really appreciate it if somebody could help, I've been dealing with this problem for a long time!

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): Credit

    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;
 }


Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

257 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 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 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

Android Share intent Twitter not working? 0 Answers

Passing parameter from native android 2 Answers

NoSuchMethodError when calling putExtra 0 Answers

Android Share text and image? 4 Answers

How do I override an Android Java class in Unity? 1 Answer


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