Access to android bank in unity3d

I have an Android project for a flashlight for a camera that works fine when deployed from eclipse. I am trying to access the flashlight function from my C # code in a unique3d file, but it does not work. To check if I am calling the android method correctly, I created a string function in the same action and returned the string correctly. I am not familiar with the encoding of my native android. It would be great if you could take a look at the code and help me.

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

Below is the Android MainActivity.java (which I converted to a jar file from eclipse and copied to the 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 of 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"); } } } 

Permissions to access the camera in AndroidManifest.xml when added, the application does not start at all. When an xml file is excluded from the project, the "dummyString" method still returns a string.

Below is 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> 

The following is a warning that shows unity in the console with the above xml included during build and 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. Any help is greatly appreciated.

Thank you in advance!

+6
source share
2 answers

Got his job thanks to Stevthorn's answer (to unity). About extending my Java activity to "UnityPlayerActivity" I got it working. That is, com.unity3d.player.UnityPlayerActivity. Learn more about UnityPlayerActivity here.

+1
source

I am not sure if the above CaffeineCoder answer is accurate. It may not be possible to extend UnityPlayerActivity, and I did it. You need to specify these special Unity actions in your manifest along with your own activities.

First, Unity will start UnityPlayerProxyActivity and continue from there. Your Java code is only called when it is called / initialized.

Listed below are some actions 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" /> 
+2
source

All Articles