hello developers!
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 forums and stackoverflow, I tried to find a solution in almost all the links I could find but no luck!
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!
Jainam
hi Steve! Thanks for the link! Following all the steps on the link, I still have the same results, string is called but flashlight doesn't work. I don't have experience in Android coding, so am confused about *.so files. I have not generated *.so files.. If *.so files are required how did the string function get called without them?
– jainamHi Steve! Your link was really helpful. I found my mistake in the manifest file. now it's working. thanks!
– jainamHow did you get it? I'm trying to reproduce your sample code, but when I press the torch button, doesn't occur anything. I extends from UnityPlayerActivity the Launcher Activity, it was what I changed from Manifest. I'm working on Unity 4.3.4 and Nexus Tablet.
– toritobravombelow is the working code
– jainam