Unity and native android

Building and using plug-ins for Android

You can use plug-ins developed for Android to call Java and C++ code created outside of Unity from your C# scripts A piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary . This allows you to access features like OS calls and third-party code libraries that would otherwise not be available to Unity. For more information about using plug-ins with Unity, see documentation on plug-ins A set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary .

The pages in this section describe the process of creating your own plug-ins for use in Android projects. The information on these pages assumes you already know how to create native plug-ins for Unity. For more information about native plug-ins and their uses, see documentation on native plug-ins A platform-specific native code library that is created outside of Unity for use in Unity. Allows you can access features like OS calls and third-party code libraries that would otherwise not be available to Unity. More info
See in Glossary .

This section contains the following pages:

Unity as a Library

If you upgrade your project to Unity 2019.4 or above, the introduction of Unity as a Library might require you to adapt your native and managed plug-ins to work properly for Android.

Источник

Integrating Unity into Android applications

This page describes how to integrate the Unity Runtime Library into Android applications using the Unity as a Library feature.

You can use this feature to include Unity-powered features, such as 3D/2D Real-Time Rendering The process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. More info
See in Glossary , AR Augmented Reality (AR) uses computer graphics or video composited on top of a live video feed to augment the view and create interaction with real and virtual objects.
See in Glossary Experience, 3D model interaction, or 2D mini-games, into your application. The Unity Runtime Library exposes controls to manage when and how to load, activate, and unload content within the application.

Important: If you upgrade your project to Unity 2019.4 or above, the introduction of Unity as a Library might require you to adapt native and managed plug-ins A set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary to work properly for Android. Plug-ins that make changes to Gradle An Android build system that automates several build processes. This automation means that many common build errors are less likely to occur. More info
See in Glossary manifests need to use the the Gradle changes outlined in Using Unity as a library in native iOS/Android apps.

How it works

You don’t need to do anything different when you build your Gradle project from Unity.

Читайте также:  Лучшие мотогонки для андроид

Every Android Gradle project that Unity generates has the following structure:

  • A library part in the unityLibrary module that you can integrate into any other Gradle project. This contains the Unity runtime and Player data.
  • A thin launcher part in the launcher module that contains the application name and its icons. This is a simple Android application that launches Unity. You can replace this module with your own application.

To integrate Unity into another Android Gradle project, you must include the unityLibrary module of the generated Android Gradle project in your Android Unity Project through the settings.gradle file.

This repository contains example Projects and plug-ins that demonstrate how to integrate Unity into an Android app, along with further documentation.

To control a Player, relay an Intent to launch Unity activity and extend it if needed. For more information, see Android developer documentation on Intents and Intent Filters. You can also use the UnityPlayer Java API.

IUnityPlayerLifecycleEvents

IUnityPlayerLifecycleEvents provides a way to interact with two important lifecycle events of the Unity Player:

  • Unload — The application calls IUnityPlayerLifecycleEvents.onUnityPlayerUnloaded when Application.Unload or UnityPlayer.unload() unloads the Unity Player. This puts the Unity Player in a paused state where it unloads all Scenes A Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
    See in Glossary , but keeps everything else loaded in the memory.
  • Quit — The application calls IUnityPlayerLifecycleEvents.onUnityPlayerQuitted when the Unity Player quits. The process that was running Unity ends after this call.

You can pass an instance of IUnityPlayerLifecycleEvents to the UnityPlayer constructor, or to override methods in subclasses of UnityPlayer and UnityPlayerActivity .

Limitations

Unity doesn’t control the runtime lifecycle, so Unity as a Library might not work for all possible use cases. Known limitations include:

  • Unity as a Library only supports full-screen rendering, and doesn’t support rendering on part of the screen.
  • You can’t load or integrate more than one instance of the Unity runtime.
  • You might need to adapt third-party plug-ins (both native and managed) to work with the Unity runtime.
  • Unity as a Library for Android added in 2019.3. NewIn20193

Источник

Native (C++) plug-ins for Android

Unity supports native plug-ins A set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary for Android written in C/C++ and packaged in a shared library (.so) or a static library (.a). When using the IL2CPP scripting backend, you can use C/C++ source files as plug-ins and Unity compiles them along with IL2CPP generated files. This includes all C/C++ source files with extensions .c, .cc, .cpp and .h.

Scripting backend compatibility.

The following table shows which scripting backends A framework that powers scripting in Unity. Unity supports three different scripting backends depending on target platform: Mono, .NET and IL2CPP. Universal Windows Platform, however, supports only two: .NET and IL2CPP. More info
See in Glossary support the different types of native plug-ins A platform-specific native code library that is created outside of Unity for use in Unity. Allows you can access features like OS calls and third-party code libraries that would otherwise not be available to Unity. More info
See in Glossary .

Scripting backend Shared library Static library C/C++ source files
IL2CPP A Unity-developed scripting back-end which you can use as an alternative to Mono when building projects for some platforms. More info
See in Glossary
Yes Yes Yes
Mono Yes No No

Building and using a native plug-in

To build a C++ plug-in for Android, use the Android NDK and get yourself familiar with the steps required to build a shared library. The same applies to static libraries.

If you are using C++ to implement the plug-in, you must ensure the methods are declared with C linkage to avoid name mangling issues. By default, only the C source files that have a .c file extension in the plug-ins have C linkage (not C++).

After building the library, copy the output .so file(s) into your Unity project. In the Inspector A Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary , mark your .so files as compatible with Android, and set the required CPU architecture in the dropdown box:

Native(C++) plug-in import settings as displayed in the Inspector window

To call the methods in your native plug-in from within your C# scripts A piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary , use the following code:

Note that pluginName should not include the prefix (‘lib’) or the extension (‘.so’) of the filename. It is recommended to wrap all the native plug-in method calls with an additional C# code layer. This code checks Application.platform and calls native methods only when the app is running on the actual device; dummy values are returned from the C# code when running in the Editor. Use platform defines to control platform dependent code compilation.

When you use C/C++ source files as plug-ins, you call them from C# in the same way except that you use __Internal for plug-in name, for example:

Native (C++) plug-in Sample

The AndroidNativePlugin.unitypackage zip file contains a simple example of a native code plug-in distributed as Unity package.

The sample shows how to invoke C++ code from a Unity application. The package includes a scene A Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary which displays the sum of two values as calculated by the native plug-in. To compile the plug-in must install the Android NDK.

Источник

Android

This section of the User Manual contains documentation on developing for the Android platform,

Environment setup

Before you can run code on your Android device or an Android emulator, you must set up Unity to support Android development. See Android environment setup.

If you don’t install one or more necessary components during initial setup, Unity prompts you to download missing components when you try to build a Project for Android.

Building your app

Unity lets you configure build and runtime settings for your app. See Building apps for Android.

If you have a Unity Pro subscription, you can customize the splash screen that displays when the game launches. See Customizing an Android splash screen.

Scripting

Unity provides scripting APIs that allow you to access input data and other settings from Android devices. See Android scripting.

You can use plug-ins A set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary to call Android functions written in C/ C++ directly from C# scripts A piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary . You can also call Java functions indirectly. See Building and using plug-ins for Android.

Optimization

Unity includes support for occlusion culling A feature that disables rendering of objects when they are not currently seen by the camera because they are obscured (occluded) by other objects. More info
See in Glossary , which disables rendering The process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. More info
See in Glossary of objects when they’re not currently seen by the camera A component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary because they’re obscured (occluded) by other objects. This is a valuable optimization method for mobile platforms. See Occlusion culling.

Troubleshooting and bug reports

The Android troubleshooting guide helps you discover the cause of bugs as quickly as possible. If, after consulting the guide, you suspect the problem is being caused by Unity, file a bug report following the Unity bug reporting guidelines.

Texture compression

Ericsson Texture Compression 3D Graphics hardware requires Textures to be compressed in specialized formats which are optimized for fast Texture sampling. More info
See in Glossary (ETC) is the standard texture compression A method of storing data that reduces the amount of storage space it requires. See Texture Compression, Animation Compression, Audio Compression, Build Compression.
See in Glossary format on Android.

ETC1 is supported on all current Android devices, but it does not support textures that have an alpha channel. ETC2 is supported on all Android devices that support OpenGL ES 3.0. It provides improved quality for RGB textures, and also supports textures with an alpha channel.

By default, Unity uses ETC1 for compressed RGB textures and ETC2 for compressed RGBA textures. If ETC2 is not supported by an Android device, the texture is decompressed at run time. This has an impact on memory usage, and also affects rendering speed.

DXT, PVRTC, ATC, and ASTC are all support textures with an alpha channel. These formats also support higher compression rates and/or better image quality, but they are only supported on a subset of Android devices.

It is possible to create separate Android distribution archives (.apk) for each of these formats and let the Android Market’s filtering system select the correct archives for different devices.

Movie/Video playback

We recommend you use the Video Player to play video files. This supersedes the earlier Movie Texture feature.

Known video compatibility issues

Not all devices support resolutions greater than 640 × 360. Runtime checks verify device support and don’t play the movie if there’s a failure.

For Android Lollipop (5.0 and 5.1.1) and above, you can use any resolution or number of audio channels, provided the target device supports them.

Unity supports playback from asset bundles for uncompressed bundles, read directly from disk.

Playback support for compressed asset bundles is available for Android 9 and later.

Format compatibility issues are reported in the adb logcat output and are always prefixed with AndroidVideoMedia .

Watch for device-specific error messages located near the error messages in Unity: they’re not available to the engine, but often explain what the compatibility issue is.

Источник

Читайте также:  Где стрелки у андроид
Оцените статью