我有一個android項目的相機手電筒,從eclipse部署時工作正常。我試圖從Unity3D中的C#代碼訪問手電筒功能,但它不起作用。爲了驗證我是否正確調用了android方法,我在同一個活動中創建了一個字符串函數,並且它正確地返回了字符串。我不熟悉原生android編碼。如果你能看看代碼並幫助我,那將是非常棒的。unity3d訪問android jar
我知道有統一論壇和stackoverflow解釋相同的一些線程,我試圖找到一個解決方案,但這些線程,但沒有運氣!所以,張貼了這個線程..
下面是Android的MainActivity.java(我轉換成從Eclipse的一個jar文件,並在統一的項目複製〜資產/插件/ 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);
}
}
下面是我單位的C#代碼,
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");
}
}
}
加時訪問攝像機在AndroidManifest.xml的權限,應用程序就無法啓動。在從項目中排除xml文件時,「dummyString」方法仍會返回字符串。
下面是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>
下面是一個統一顯示在上述XML控制檯構建&運行期間,包括警告,
Unable to find unity activity in manifest. You need to make sure orientation attribut is set to sensor manually.
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()
這將是巨大的,如果有人可以幫助我。任何幫助深表感謝。
預先感謝您!
[鏈接](http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html) – jainam