Android/iOS embedded Unity development example

Table of contents

foreword

background

text

surroundings

New Construction

Unity export

Android access

how to use

as Activity

Summarize

1.Android calls Unity

2.Unity calls Android

3.C/C++ "transit station"

References


foreword

background

There are two main ways of cross-development between Unity and Android/iOS. Taking Android as an example, one is to generate a jar or aar package for Android and import it into the unity3d plugin/bin/ directory; the other is to export Android projects from Unity and use them as libraries in Android Studio Continue to develop . The online tutorials are almost all the first way, and this article mainly analyzes the second way.

In this way, the Unity project that has been written can be integrated in the form of a library in the NativeApp of the mobile terminal, and the convenient development method of the Unity game engine can be used for cross-platform development.

Unity Official Documentation  Unity as a Library integration example to iOS and Android

The following uses the Android platform as an example.


text

surroundings

  • Android Studio 3.5.3

  • Unity version 2019.3.7f1


New Construction

Android Studio new project:

Unity new project:

The final project structure is as follows:


Unity export

  • Open UnityProject via Unity

  • choose File -> Build Settings -> Switch Android Platform -> Export Project

At this time, selecting Export may prompt an error in the JDK path configuration. It doesn't matter if we  Preference -> Externl Tools set the path

Now you can click Export, and the path selection can be freely selected. It is recommended to follow the official website.


Android access

  • Open NativeAndroidApp via Android Studio

  • Select  the setting.gradle  file to add the unityLibrary module

include ':unityLibrary'
project(':unityLibrary').projectDir = new File('..\\UnityProject\\androidBuild\\unityLibrary')
  • Select  build.gradle (Module:app) to add dependencies

dependencies {
    implementation project(':unityLibrary')
    implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString() + ('\\libs'), include: ['*.jar'])
    // 自己项目的配置
}
  • Select  build.gradle (Module: NativeAndroidApp)

allprojects {
    repositories {
        google()
        jcenter()

        // Add Code
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
        // End
    }
}
  • Select the  strings.xml of NativeAndroidApp to  add

<resources>
    <string name="app_name">NativeAndroidApp</string>
    <string name="action_settings">Settings</string>
    // Add Code
    <string name="game_view_content_description">Game view</string>
    // End
</resources>
  • Click the sync project of AS , you can see that there is an additional module :

Now we can directly use the java class in this module .


how to use

as Activity

Now our project contains two modules , one is the app that comes with the new project, and the other is the unityLibrary that was just imported . Click on the unityLibrary directory to see two main classes :

Among them, UnityPlayerActivity is the most important class, through which we can display the Unity scene in the Android app.

As for how to use it - OverrideUnityActivity is an official example of the usage of UnityPlayerActivity , and the code inside is also very simple.

import com.unity3d.player.UnityPlayerActivity;

public abstract class OverrideUnityActivity extends UnityPlayerActivity
{
    public static OverrideUnityActivity instance = null;

    abstract protected void showMainActivity(String setToColor);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        instance = this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }
}

Obviously, the official hope that we use an Activity in Android to display the scene rendered by Unity. As for this Activity , it is basically finished by directly inheriting UnityPlayerActivity , which is very simple.

Create a new  Activity , here named SourceUnityActivity :

public class SourceUnityActivity extends UnityPlayerActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Now just inherit from UnityPlayerActivity . Now build the app, and when you enter the SourceUnityActivity , the Unity scene will be displayed.


Summarize

1.Android calls Unity

//向unity发消息
UnityPlayer.UnitySendMessage("Main Camera", //gameobject的名字
                             "ChangeColor", //调用方法的名字
                             "");			//参数智能传字符串,没有参数则传空字符串

2.Unity calls Android

//通过该API来实例化java代码中对应的类
AndroidJavaObject jc = new AndroidJavaObject("com.xxx.xxx.UnityPlayer");
jo.Call("Test");//调用void Test()方法
jo.Call("Text1", msg);//调用string Test1(string str)方法
jo.Call("Text2", 1, 2);//调用int Test1(int x, int y)方法

3.C/C++ "transit station"

The above parameter passing method can only pass strings to each other. This method can be used when a small amount of data is passed. If it is a large amount of data, it is a bit limited. In fact, we can use the C/C++ code as a transit station to achieve data reference and sharing on both sides. As long as you add NDK support and C++ source code to the Android project, you can call it on the Unity side.

Please explore the rest of the advanced usage by yourself hahaha, including the extra programming of some scenes of the built-in plug-in automatic camera in Unity, but not only this...


References

Unity - Manual: Android Library Projects and Android Archive plug-ins

Unity - Manual: Extend the default Unity activity

Unity - Manual: Native plug-ins for Android

Integration Unity as a library in native Android app - Unity Forum

unity3d - Display Unity Scene as Sub View in android studio - Stack Overflow

Guess you like

Origin blog.csdn.net/flyTie/article/details/127132130