Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing android jar in unity3d

I have an android project for camera flashlight, which when deployed from eclipse works fine. I am trying to access the flashlight function from my C# code in unity3d but it doesn't work. To verify if I am calling the android method correctly, I created a string function in the same activity and it is returning the string correctly. I am not familiar to native android coding. It would be great if you could have a look at the code and help me out.

I know there are some threads in unity forum and stackoverflow explaining the same, I tried to find a solution on these threads but no luck! So, posted this thread..

Below is android MainActivity.java (which I converted into a jar file from eclipse and copied in unity project, ~Assets/Plugins/Android/bin/),

package com.example.newflash;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    private static Camera camera;
    private static Parameters parameters;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    public static String dummyString()
    {
        return "dummy string";
    }

    public static void setFlashOn()
    {
        if (camera == null)
            camera = Camera.open();
        parameters = camera.getParameters();
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    }

    public static void setFlashOff()
    {
        parameters = camera.getParameters();
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);     
        camera.setParameters(parameters);
    }
}

Below is my unity C# code,

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;

public class testJar : MonoBehaviour
{
    bool torchon;
    AndroidJavaClass testClasslight;

    void Start ()
    {
        torchon = false;
    }

    void OnGUI ()
    {
        string teststring = "empty";

        AndroidJavaClass testClass = new AndroidJavaClass("com.example.glassplaces.MainActivity");
        teststring = testClass.CallStatic<string>("dummyString");

        GUI.Label (new Rect (20, 20, 100, 60), teststring);

        if(GUI.Button(new Rect (20, 100, 100, 60), "ON"))
        {
            torchon = true;
        }

        if(torchon == true)
        {
            GUI.Label(new Rect(200, 20,100,60), "torch ON");
            testClass.CallStatic<AndroidJavaObject>("setFlashOn");
        }
    }
}

The permissions to access camera in AndroidManifest.xml when added, the app doesn't start at all. On excluding the xml file from the project, the "dummyString" method still returns the string.

Below is the AndroidManifest.xml,

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name">
        <activity
            android:name="com.example.newflash.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Below is the warning which unity shows in console with the above xml included during Build & Run,

Unable to find unity activity in manifest. You need to make sure orientation attribut is set to sensor manually.
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

It would be great if someone could help me out. Any help is much appreciated.

Thank you in advance!

like image 277
jainam Avatar asked Dec 16 '13 13:12

jainam


Video Answer


2 Answers

I am not 100% sure the above response by CaffeineCoder is accurate. It is certain possible to not extend UnityPlayerActivity and I've done it. You need to specify these special Unity activities in your Manifest along with your own Activity.

Unity will start UnityPlayerProxyActivity at first and proceed from there. Your Java code is only called when you call/initialize it.

Following are some of the activities you can specify:

<activity
  android:name="com.unity3d.player.UnityPlayerProxyActivity"
  android:label="AngryBots Gamme"
  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  android:screenOrientation="landscape"
>

  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>

</activity>

<activity
  android:name="com.unity3d.player.UnityPlayerActivity"
  android:label="AngryBots Gamme"
  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  android:screenOrientation="landscape"
/>

<activity
  android:name="com.unity3d.player.UnityPlayerNativeActivity"
  android:label="AngryBots Gamme"
  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  android:screenOrientation="landscape"
/>
like image 150
Goat Avatar answered Sep 28 '22 12:09

Goat


Got it working, thanks to Stevethorne's answer (on unityAnswers). On extending my java activity to "UnityPlayerActivity", I got it working. That is, com.unity3d.player.UnityPlayerActivity. Here are further details about UnityPlayerActivity.

like image 31
jainam Avatar answered Sep 28 '22 14:09

jainam