Building Android plugin for Unity part 2

in #android5 years ago (edited)

Ok, On part 1 is how we can use Unity to call android native to do stuff, on part 2 we want to use android native to call Unity to do stuff, to do that, we must first get the Unity classes.jar and put it into our android studio lib folder


So the location of Unity classes.jar file is located on Application-> Unity->PlaybackEngines->AndroidPlayer->Variations->mono-> classes-> classes.jar


Then you should copy the jar file into your AndroidStudioProjects->YOURPROJECTNAME->YOURPLUGINNAME->libs

Now that you have done that you can go to your android studio and make sure inside your build.gradle file under your plugin folder have this

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

then you can modify your plugin1class.java like below

package com.example.plugin1lib;

import android.util.Log;
import com.unity3d.player.UnityPlayer;
public class plugin1class {

public void mylog()
{
        Log.d("Unity","this is calling from native");

        UnityPlayer.UnitySendMessage("mainscript","change_to_a","from android");
    }

So inside your unity, you can create an empty object called "mainscript", attach a C# script into it, inside your C# script you can have a function call "change_to_a" which accept a string parameter, so when everything is successful, you can now calling Unity using this function

"UnityPlayer.UnitySendMessage"

 

So what if you want to call other java plugin to do stuff? You can then copy and paste all the .aar file or .jar file into your Unity folder->Assets->Plugins->Android and you can import their classes and call their function example like below

import com.meetone.meetlib.MeetOneCallBack;
import com.meetone.meetlib.MeetOneManager;
import com.meetone.meetlib.been.AppInfo;
import com.meetone.meetlib.been.Authorize;
import com.meetone.meetlib.been.CallBack;
import com.meetone.meetlib.been.Signature;
import com.meetone.meetlib.been.Transfer;
import com.meetone.meetlib.been.Transaction;

public void testAuthorize() {

Log.d("Unity","ahahahahahaha this is native 3");

    MeetOneManager.getInstance().setDebugMode(true);
    AppInfo appInfo = new AppInfo();
    appInfo.setName("moreone");
    appInfo.setIcon("app icon");
    appInfo.setDappCallbackScheme("moreone");
    //appInfo.setDappRedirectURL("http://www.baidu.com");
    appInfo.setDescription("this is a demo");
    appInfo.setVersion("2.4.0");
    appInfo.setUuID(getUUID(UnityPlayer.currentActivity.getApplicationContext()));

    Authorize authorize = new Authorize();
    authorize.setDescription("memo");
    MeetOneManager.getInstance().requestAuthorize(UnityPlayer.currentActivity.getApplicationContext(), authorize, appInfo, new MeetOneCallBack() {
        @Override
        public void callBack(Map<String, Object> paramMap, CallBack callBack) {
            //Log.d("MeetOneManager", paramMap.toString());
            Log.d("MeetOneManager",paramMap.toString()+"  "+callBack.getData());
            UnityPlayer.UnitySendMessage("mainscript","change_to_a",callBack.getData()+"");
        }
    });
}

So here I copy paste the meetone .aar file into my Unity, then inside my plugin1class.java i can import them and use them like above example, so basically you can have as many plugin as you want all dump into your Unityproject->Assets->plugins->Android folder and you can use all of them

One last thing is the plugin that you build will not include AndroidManifest.xml file so you must include your AndroidManifest.xml file inside your Unityproject->Assets->plugins->Android folder as well, so you can control all the activity within it such as

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.plugin1lib">

<application>


    <activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@String/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />



</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>


    <activity android:name="com.meetone.meetlib.RouterActivity"
        android:screenOrientation="portrait"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="moreone" />
        </intent-filter>
    </activity>


</application>

</manifest>

Thats all for now, thanks for reading

<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : http://fundurian.vornix.blog/2019/01/11/building-android-plugin-for-unity-part-2/ </em><hr/></center>

Sort:  

You’ve been upvoted by TeamMalaysia community. Here are trending posts by other TeamMalaysia authors at http://steemit.com/trending/teammalaysia

To support the growth of TeamMalaysia Follow our upvotes by using steemauto.com and follow trail of @myach

Vote TeamMalaysia witness bitrocker2020 using this link vote for witness

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by fundurian from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Hi @fundurian!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.978 which ranks you at #3767 across all Steem accounts.
Your rank has improved 4 places in the last three days (old rank 3771).

In our last Algorithmic Curation Round, consisting of 346 contributions, your post is ranked at #240.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • The readers like your work!
  • Try to work on user engagement: the more people that interact with you via the comments, the higher your UA score!

Feel free to join our @steem-ua Discord server

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.032
BTC 64579.45
ETH 3101.05
USDT 1.00
SBD 3.83