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);
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!