Java Unity3D 和 Android Studio 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24564470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:02:26  来源:igfitidea点击:

Unity3D and Android Studio Integration

javaandroidxmlunity3dandroid-studio

提问by Daniel Arantes Loverde

Anyone know integrate Android with Unity Studio? (i will explain)
I created a simple scene in Unity (4.3.x on OSX Maverics) for testing. Has a 3D object and nothing else.
I do this in XCode using the Stackoverflow explanations here and i post my complete code here ( Touch a UIButton and show Unity on UIView or UIViewController) to show it's really simple.
But now, I need to do the same on Android Studio (which I installed and I can export the project from Unity to Android)
The only thing I know is that the AndroidManifest.xml and the file is in "res/layout/(something).xml" files that are the first (read) and (displays) the layout on the screen when you create a project on Android Studio.

有人知道将 Android 与 Unity Studio 集成吗?(我会解释)
我在 Unity(OSX Maverics 上的 4.3.x)中创建了一个简单的场景进行测试。有一个 3D 对象,没有别的。
我在这里使用 Stackoverflow 解释在 XCode 中执行此操作,并在此处发布我的完整代码(触摸 UIButton 并在 UIView 或 UIViewController 上显示 Unity)以显示它非常简单。
但是现在,我需要在 Android Studio 上做同样的事情(我安装了它,我可以将项目从 Unity 导出到 Android)
我唯一知道的是 AndroidManifest.xml 和文件在“res/layout/(something .xml”文件,这些文件是您在 Android Studio 上创建项目时第一个(读取)和(显示)屏幕上的布局。

When you open the generated project from Unity, the only XML you have is AndroidManifest.
So I'm lost. Since the document of third part site (http://www.rbcafe.com/Softwares/Unity/Documentation/Manual/android-GettingStarted.html) only mentions the Unity Eclipse and a JAVA files that do not exist in the project, makes me more lost.

当您从 Unity 打开生成的项目时,您拥有的唯一 XML 是 AndroidManifest。
所以我迷路了。由于第三方站点的文档(http://www.rbcafe.com/Softwares/Unity/Documentation/Manual/android-GettingStarted.html)只提到了 Unity Eclipse 和项目中不存在的 JAVA 文件,使得我更失落。

In Objective-C, you create your somethingDelegate.mm and .h and inserts "a line of code" and ready. In Android does not seem to be as simple as this official document (http://docs.unity3d.com/Manual/PluginsForAndroid.html) says.

在Objective-C 中,您创建somethingDelegate.mm 和.h 并插入“一行代码”并准备就绪。在 Android 中似乎并不像这个官方文档 ( http://docs.unity3d.com/Manual/PluginsForAndroid.html) 说的那么简单。

Someone already did this in the Android Studio and could help me?

有人已经在 Android Studio 中做到了这一点,可以帮助我吗?

Thanks in advance.

提前致谢。

** Edited for bounty:**

Need to create 3 views:
1 - Main View with 2 buttons;
2 - One button go to a second View;
3 - Other button go to Unity View;

There must be a simple way to do this on Android as a studio made ??the link above.

**为赏金编辑:**

需要创建 3 个视图:
1 - 带有 2 个按钮的主视图;
2 - 一个按钮转到第二个视图;
3 - 其他按钮转到 Unity 视图;

必须有一种简单的方法可以在 Android 上做到这一点,作为工作室制作的??上面的链接。

回答by Stefan Hoffmann

Setting up an Android project is really simple. The document you linked is outdated and you don't need to move any files at all.

设置一个 Android 项目非常简单。您链接的文档已过时,您根本不需要移动任何文件。

Unity

统一

  1. Goto File -> Build Settings -> Androidand click Switch Platform
  2. Enable Google Android Project
  3. Click Exportand choose where you want the Android project to be
  1. 转到File -> Build Settings -> Android并单击Switch Platform
  2. 使能够 Google Android Project
  3. 单击Export并选择您希望 Android 项目所在的位置

Android Studio

安卓工作室

  1. Import or open the project you have just created with Unity
  2. Done :)
  1. 导入或打开你刚刚用 Unity 创建的项目
  2. 完毕 :)

If you want to display the Unity engine inside a subactivity, I suggest you take a look at this answer.

如果你想在子活动中显示 Unity 引擎,我建议你看看这个答案

If you're completely unfamiliar with Android development, you should start with a simple "Hello World" app without complicating things by adding Unity to the mix. Just follow some of the official Android guides.

如果您完全不熟悉 Android 开发,您应该从一个简单的“Hello World”应用程序开始,不要通过将 Unity 添加到组合中来使事情复杂化。只需遵循一些官方 Android 指南即可

回答by tyoc213

For open a Android Studio project from Unity3Dinside Android studioas march 2016 you need to File -> New -> Importand let the thing do his work.

Android Studio project from Unity3DAndroid studio2016 年 3 月打开一个内部,你需要 File -> New -> Import让这件事完成他的工作。

Or if you have the quick start window:

或者,如果您有快速启动窗口:

enter image description here

在此处输入图片说明

回答by mustaq

I integrated a Unity project to an Android application.

我将一个 Unity 项目集成到一个 Android 应用程序中。

I had a simple activity with a button created using Android Studio.

我有一个使用 Android Studio 创建的按钮的简单活动。

On clicking this button the Unity scenes will start.

单击此按钮后,Unity 场景将启动。

My ActivityUnity Activity

我的活动团结活动



The android project exported from unity should like this: enter image description here

unity 导出的 android 项目应该是这样的: 在此处输入图片说明



It needed to be converted to Gradle project first.

它需要先转换为 Gradle 项目。

For that:

为了那个原因:

  1. Open Android Studio -> choose import project -> select the unity project (Non-Gradle project) -> save project and close.
  1. 打开Android Studio -> 选择导入项目-> 选择unity 项目(非Gradle 项目)-> 保存项目并关闭。

Now your unity project folder looks like this:

现在您的统一项目文件夹如下所示:

enter image description here

在此处输入图片说明



Now open your project and do the following to import the unity project in to your project:

现在打开您的项目并执行以下操作以将统一项目导入到您的项目中:

  1. Click File -> New -> New Module -> Select "import Gradle Project" -> choose the unity gradle project folder you just created -> rename module name if prompted by checking the “import” checkbox in Android studio -> Click Finish
  2. Verify in Settings.gradle file to see if the new module name is added in the include command:

    include ':app', ':yournewmodulename'
    
  3. Open build.gradle file of the new unity module from navigation window and rename the below line:

    apply plugin: 'com.android.application'
    

    to

    apply plugin: 'com.android.library'
    
  4. Remove the line from the defaultConfig section of the new unity module:

    applicationId "com.xxx.xxx"
    
  5. Open Manifest of this new unity module and comment the applicationtag (Do not remove the uses-sdktag).Integrate the new unity module's Manifest with your project's Manifest by moving necessary tags to our project's Manifest (e.g. :Activity, uses-featureetc.).

  6. Open build.gradle of your project and add below line in the dependencies section:

    dependencies {
    …
    compile project(path: ':yournewmodulename')
    
    }
    
  1. 点击 File -> New -> New Module -> 选择“import Gradle Project” -> 选择你刚刚创建的 unity gradle 项目文件夹 -> 如果在 Android studio 中勾选“import”复选框提示重命名模块名称 -> 点击 Finish
  2. 在 Settings.gradle 文件中验证是否在 include 命令中添加了新的模块名称:

    include ':app', ':yournewmodulename'
    
  3. 从导航窗口打开新统一模块的 build.gradle 文件并重命名以下行:

    apply plugin: 'com.android.application'
    

    apply plugin: 'com.android.library'
    
  4. 从新统一模块的 defaultConfig 部分中删除该行:

    applicationId "com.xxx.xxx"
    
  5. 打开这个新统一模块的清单并注释应用程序标签(不要删除uses-sdk标签)。通过将必要的标签移动到我们项目的清单(例如:Activityuses-feature等),将新统一模块的清单与您项目的清单集成.)

  6. 打开项目的 build.gradle 并在依赖项部分添加以下行:

    dependencies {
    …
    compile project(path: ':yournewmodulename')
    
    }
    


Now on button click you can call the Activity in the unity module from your activity as below:

现在点击按钮,您可以从您的活动中调用统一模块中的活动,如下所示:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.xxx.xxx.UnityPlayerActivity;

public class MainActivity extends AppCompatActivity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button= (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, UnityPlayerActivity.class);
            startActivity(i);
        }
    });
}
}