Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在unity3d中访问android jar_C#_Android_Jar_Unity3d - Fatal编程技术网

C# 在unity3d中访问android jar

C# 在unity3d中访问android jar,c#,android,jar,unity3d,C#,Android,Jar,Unity3d,我有一个android项目,用于照相机手电筒,从eclipse部署时效果很好。我正试图从unity3d中的C#代码访问闪光灯功能,但它不起作用。为了验证我是否正确调用了android方法,我在同一活动中创建了一个string函数,并且它正确返回了字符串。我不熟悉原生android编码。如果你能看一下代码并帮助我,那就太好了 我知道unity论坛和stackoverflow中有一些线程,我试图在这些线程上找到解决方案,但没有成功!所以,发布了这个帖子 下面是android MainActivity

我有一个android项目,用于照相机手电筒,从eclipse部署时效果很好。我正试图从unity3d中的C#代码访问闪光灯功能,但它不起作用。为了验证我是否正确调用了android方法,我在同一活动中创建了一个string函数,并且它正确返回了字符串。我不熟悉原生android编码。如果你能看一下代码并帮助我,那就太好了

我知道unity论坛和stackoverflow中有一些线程,我试图在这些线程上找到解决方案,但没有成功!所以,发布了这个帖子

下面是android MainActivity.java(我从eclipse转换成jar文件,并复制到unity项目中,~Assets/Plugins/android/bin/)

下面是我的统一C代码

如果有人能帮我,那就太好了。非常感谢您的帮助


提前谢谢你

多亏了史蒂夫索恩的回答(关于unityAnswers)。在将我的java活动扩展到“UnityPlayerPractivity”时,我让它工作起来了。也就是说,com.unity3d.player.unityPlayerPractivity。有关UnityPlayerPractivity的更多详细信息。

我不能100%确定CaffeineCoder的上述回复是否准确。不扩展UnityPlayerPractivity肯定是可能的,我已经做到了。您需要在清单中指定这些特殊的统一活动以及您自己的活动

Unity将首先启动UnityPlayerProxy活动,然后从那里开始。Java代码只有在调用/初始化时才会被调用

以下是您可以指定的一些活动:

<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"
/>

您是对的,也可以不扩展“活动”而仍能正常工作。我的错!我已经编辑了我的答案。谢谢你提到这件事。
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");
        }
    }
}
<?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>
Unable to find unity activity in manifest. You need to make sure orientation attribut is set to sensor manually.
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()
<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"
/>