Search Unity

Application crashing/freezing when instantiating a Texture2D from AndroidJavaProxy class

Discussion in 'Android' started by bdovaz, Oct 21, 2018.

  1. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
    @Yury-Habets I saw this post that I don't know if its related and it's applicable now (it was from 2014):

    https://answers.unity.com/questions/854901/android-plugins-that-raise-callbacks-on-non-script.html

    If it's still the case it would be nice to provide a guide on implementing a solution on that.

    I have a simple scene with this Unity sample script:

    https://docs.unity3d.com/ScriptReference/AndroidJavaProxy.html

    That I have edited (it has a bug, currentActivity should be called with "AndroidJavaObject" not "AndroidJavaClass") and I also added some Unity API calls inside the callback.

    Code (CSharp):
    1. // Opens an android date picker dialog and grabs the result using a callback.
    2. using UnityEngine;
    3. using System;
    4.  
    5. class ExampleClass : MonoBehaviour {
    6.     private static DateTime selectedDate = DateTime.Now;
    7.  
    8.     class DateCallback : AndroidJavaProxy {
    9.         public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener") { }
    10.         void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth) {
    11.             selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
    12.             Debug.Log(selectedDate);
    13.             new GameObject("go");
    14.             Debug.Log(GameObject.Find("go").name);
    15.             Debug.Log(new Texture2D(2, 2));
    16.         }
    17.     }
    18.  
    19.     void OnGUI() {
    20.         if (GUI.Button(new Rect(15, 15, 450, 75), string.Format("{0:yyyy-MM-dd}", selectedDate))) {
    21.             AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    22.             activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    23.             {
    24.                 new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), selectedDate.Year, selectedDate.Month - 1, selectedDate.Day).Call("show");
    25.             }));
    26.         }
    27.     }
    28. }
    First, I thought that it was something related to calling Unity API inside AndroidJavaProxy class but if you run this example you should see that it successfully creates the GameObject and it's able to find it.

    My problem is on the line that creates a Texture2D, it makes the app completely freeze.

    In my project it even crashes (in my code that of course has nothing to do with this example).

    This is the crash I get in my project (I repeat, it's not originated by the above code but my project code):

     
    Last edited: Oct 21, 2018