1

I have a SQLite helper class like:

public class GameDbHelper extends SQLiteOpenHelper {...}

If in the android java world, I have to initialize it in the body of an activity like:

public class MainActivity extends AppCompatActivity {
    ...
    GameDbHelper gameDBHelper;

    protected void onCreate(Bundle savedInstanceState) {
        ...
        gameDBHelper = new GameDbHelper(this);// "this" being the Context

        // database: what ultimately is needed to perform CRUD
        database = databaseHelper.getWritableDatabase();
    }

I want to initialize this gameDBHelper in Unity side and from there call, say, database.insert(...);
From Unity documentation I learned it is possible to initialize an object to call it's methods:

void Start() {
    AndroidJavaObject jo = new AndroidJavaObject("android.content.res.Configuration");
    jo.Call("setToDefaults");
}


I think that perhaps I must first initialize the "gameDBHelper = new GameDbHelper(this);" in the Unity Activity:
- export the Unity project into Android Studio
- in the UnityPlayerActivity, define object and initialize it
- "somehow" call it from Unity code (defined before exporting)

A different approach:
- to export the database class as a lib from Android studio into Unity

it is more comfortable for me in Android studio, because in this project I have, Unity is not to be the launcher activity.

Please instruct me how to properly do this.

Ariam1
  • 1,673
  • 2
  • 13
  • 32
  • But why are you doing this? Why don't you just use built-in sqlite in C# to do this? No Java required. Just C# code that calls C++ native lib. – Programmer Jun 26 '18 at 13:02
  • Thank you, short answer: I use Room in Android side in other activities so User without launching the unity game can operate the database. I don't know how to connect Room to that SQLite DB in Unity side. – Ariam1 Jun 26 '18 at 13:13
  • 1
    Looks like your Android app embeds Unity game. Instead of using `AndroidJavaObject`, you can try to access the path where you saved the database from Unity side. Write a Java function that gets the path of the db file. If that doesn't work, write a java function that reads the the db file as byte array then send this to Unity when needed. You can the save the file to `Application.persistentDataPath` and access it from Unity. See [this](https://stackoverflow.com/questions/50753569/setup-database-sqlite-for-unity/50808156#50808156) post for how to set this up in Unity – Programmer Jun 26 '18 at 13:17
  • I will try to realize this approach on the link, thank you very much. – Ariam1 Jun 26 '18 at 13:31

1 Answers1

0

I try to document relevant parts of my own solution, just in case that may help someone. Please if you see any problem in it, let me know (I'm only learning).

I didn't know that I could pass (inject) "Context" to the constractor of a java class that need it, from Unity side. I found it on this forum, thaks to the forum!
Unity side:

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaObject jo = new AndroidJavaObject("com.my.package.MyDbHelper", context);
string word = jo.Call<string>("returnData");
tx.text = word;

This will pass context to an Android studio java class constructor, which will then initialize a SQLite database and a helper class that will manipulate it. The class relevant data as example, in Android studio:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    // implement as documentation describes, link is provided bellow
}

// this is the class, addressed from Unity
public class MyDbHelper{
    String str = null;
    private SQLiteDatabase db;
    MySQLiteOpenHelper mySQLiteOpenHelper;

    public myDbHelper(Context context) {
            mySQLiteOpenHelper = new mySQLiteOpenHelper(context);
            db = mySQLiteOpenHelper.getWritableDatabase();
    }

    // this is the method that unity calls
    public String returnData(){
        return queryAndDisplayAll();
    }

    private String queryAndDisplayAll() {
            // using Cursor and db perform query and get the result, say, in a str, and return it, that is like:
            // Cursor cursor = db.query(DicSQLiteOpenHelper.TABLE_NAME,...
            // str = ...result from Curson...
            return (str == null ? "No result!" : str);
    }


Here you could read about the SQLiteOpenHelper:
Link to SQLiteOpenHelper documentation.

Ariam1
  • 1,673
  • 2
  • 13
  • 32