### Enable 64-bit Architecture (Unity Editor) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-distribution-google-play This guide outlines the steps to enable 64-bit architecture support for Android builds within the Unity editor. This is a mandatory requirement for applications published on Google Play. It requires setting the scripting backend to IL2CPP. ```text 1. Open the Build Settings window (menu: File > Build Settings). 2. Select Android from the list of platforms or create a new build profile for Android. 3. Select Player Settings. 4. Navigate to Other Settings > Configuration. 5. Enable the ARM64 checkbox. Note: This option is only available if your project's Scripting backend is set to IL2CPP. ``` -------------------------------- ### Displaying Activity Indicator in Unity Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-API This example demonstrates how to use Handheld.StartActivityIndicator to display a built-in activity indicator on mobile operating systems. This is useful for providing visual feedback to the user during slow operations. ```csharp using UnityEngine; public class ActivityIndicatorManager : MonoBehaviour { public void ShowActivityIndicator() { // Displays the operating system's built-in activity indicator. // This is useful for indicating that a process is running in the background. Handheld.StartActivityIndicator(); Debug.Log("Activity indicator started."); } public void HideActivityIndicator() { // Hides the activity indicator. Handheld.StopActivityIndicator(); Debug.Log("Activity indicator stopped."); } // Example usage: void Update() { if (Input.GetKeyDown(KeyCode.A)) { ShowActivityIndicator(); } if (Input.GetKeyDown(KeyCode.S)) { HideActivityIndicator(); } } } ``` -------------------------------- ### Android Java/Kotlin Code for Unity C# Interaction Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-datatypes-java-kotlin-csharp This Java/Kotlin code provides example methods that can be called from Unity's C# scripts. It includes both static and instance methods, demonstrating how to receive parameters and return values. These classes should be placed in your Android project's source directory. ```java package com.example; import android.util.Log; public class MyJavaClass { public static int staticMethod(int a, int b) { Log.d("Unity", "Static method called with: " + a + ", " + b); return a + b; } } public class MyJavaObject { private String data; public MyJavaObject(String initialValue) { this.data = initialValue; Log.d("Unity", "MyJavaObject created with: " + initialValue); } public void instanceMethod(String parameter) { Log.d("Unity", "Instance method called with: " + parameter); this.data = parameter; } public String getData() { return this.data; } } ``` ```kotlin package com.example import android.util.Log class MyKotlinClass { companion object { @JvmStatic fun staticMethod(a: Int, b: Int): Int { Log.d("Unity", "Static Kotlin method called with: $a, $b") return a * b } } } class MyKotlinObject(initialValue: String) { private var data: String = initialValue init { Log.d("Unity", "MyKotlinObject created with: $initialValue") } fun instanceMethod(parameter: String) { Log.d("Unity", "Instance Kotlin method called with: $parameter") this.data = parameter } fun getData(): String { return this.data } } ``` -------------------------------- ### Configure Custom Asset Pack Delivery Mode (Gradle) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-asset-packs-create-custom This snippet shows how to configure a custom asset pack's delivery mode using a `build.gradle` file within the asset pack directory. It sets the delivery type to 'fast-follow', which enables Google Play to download the pack after app installation. Ensure the `packName` matches the asset pack directory name. ```gradle apply plugin: 'com.android.asset-pack' assetPack { packName = "MyAssets1" dynamicDelivery { deliveryType = "fast-follow" } } ``` -------------------------------- ### Pass '-systemallocator' Argument to Unity App via ADB Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-custom-activity-command-line This example demonstrates how to pass the `-systemallocator` command-line argument to a Unity application using ADB. This argument typically influences memory allocation strategies within the Unity engine. Ensure the package and activity names are correct for your project. ```bash adb shell am start -n "com.Company.MyGame/com.unity3d.player.UnityPlayerActivity" -e unity "-systemallocator" ``` -------------------------------- ### Install Custom Crash Handlers in Unity Android (Java) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-handle-crashes This Java code snippet demonstrates how to install custom signal handlers for crash reporting in a Unity Android application. It shows the placement of the `InstallCustomSignalHandlersFromJava()` call within the `onCreate` method of `UnityPlayerActivity.java`, ensuring the handler is active before the Unity player initializes. ```java public class UnityPlayerActivity extends Activity { // ... public native void InstallCustomSignalHandlersFromJava(); static { System.loadLibrary("il2cpp"); } // Setup activity layout @Override protected void onCreate(Bundle savedInstanceState) { InstallCustomSignalHandlersFromJava(); // ... mUnityPlayer = new UnityPlayer(this, this); setContentView(mUnityPlayer); mUnityPlayer.requestFocus(); } // ... } ``` -------------------------------- ### Retrieve Application Cache Directory from C# (Unity) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-high-level-api-code-examples This C# code example shows how to get the current application's cache directory on Android using the AndroidApplication class and Java's 'getCacheDir' and 'getCanonicalPath' methods. It utilizes UnityEngine and UnityEngine.Android namespaces. ```csharp using UnityEngine; using UnityEngine.Android; public class JavaExamples { public static string GetApplicationCacheDirectory() { using var javaFile = AndroidApplication.currentActivity.Call("getCacheDir"); var cacheDirectory = javaFile.Call("getCanonicalPath"); return cacheDirectory; } } ``` -------------------------------- ### Configure Texture Compression Formats via API (C#) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-distribution-google-play This snippet demonstrates how to programmatically assign required texture compression formats for Android builds using the `PlayerSettings.Android.textureCompressionFormats` API in Unity. This is an alternative to manually configuring these settings in the Player Settings window. ```csharp using UnityEditor; public class TextureCompressionSettings { [UnityEditor.MenuItem("Tools/Set Android Texture Compression Formats")] public static void SetTextureCompressionFormats() { // Example: Assigning ETC2 and ASTC as texture compression formats. // The order matters; the first format is the default. PlayerSettings.Android.textureCompressionFormats = new AndroidTextureFormat[] { AndroidTextureFormat.ETC2, AndroidTextureFormat.ASTC, AndroidTextureFormat.ASTC_HDR }; UnityEngine.Debug.Log("Android texture compression formats set."); } } ``` -------------------------------- ### Set Custom Activity as Application Entry Point (Android Manifest) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-custom-activity This Android Manifest XML snippet demonstrates how to set a custom activity as the application's entry point. It involves modifying the `` tag to include the fully qualified class name of the custom activity and setting the `MAIN` action and `LAUNCHER` category for the intent filter. ```xml ``` -------------------------------- ### Get Java String Hash Code from C# (Unity) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-high-level-api-code-examples This C# code snippet demonstrates how to create a Java String object using AndroidJavaObject and retrieve its hash code by calling the Java 'hashCode' method. It requires the UnityEngine namespace. ```csharp using UnityEngine; public class JavaExamples { public static int GetJavaStringHashCode(string text) { using (AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", text)) { int hash = jo.Call("hashCode"); return hash; } } } ``` -------------------------------- ### Set Custom Activity with GameActivity as Entry Point (Android Manifest) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-custom-activity This Android Manifest XML snippet shows how to set a custom activity, specifically utilizing GameActivity, as the application's entry point. It includes the `android:name` attribute for the custom activity, intent filters, and an additional `` tag for the library name, common when extending GameActivity. ```xml ``` -------------------------------- ### Create and Use Kotlin Source Plug-in in Unity Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/6000.3/Documentation/Manual/AndroidJavaSourcePlugins This snippet demonstrates creating a Kotlin source file as a plug-in and accessing its static method from a C# script in Unity. It requires placing the Kotlin file in the Assets folder, enabling the Android platform in the Inspector, and using AndroidJavaClass to call the static method. ```kotlin  object KotlinStringHelper { @JvmStatic fun getString(): String { return "Hello from Kotlin" } } ``` ```csharp  using UnityEngine; public class KotlinExamples : MonoBehaviour { void Start() { using (AndroidJavaClass cls = new AndroidJavaClass("KotlinStringHelper")) { string value = cls.CallStatic("getString"); Debug.Log($"KotlinStringHelper.getString returns {value}"); } } } ``` -------------------------------- ### Launch Unity App with Command-Line Arguments via ADB Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-custom-activity-command-line This command structure allows you to launch a Unity application on an Android device via ADB and pass custom command-line arguments. Replace ``, ``, and `` with your specific values. This method is useful for debugging and configuring application behavior at startup. ```bash adb shell am start -n "/" -e unity "" ``` -------------------------------- ### Configure Android Library build.gradle in Unity Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-library-plugin-create This Gradle script configures an Android Library plug-in for Unity. It specifies dependencies, SDK versions, and build settings like minSdk, targetSdk, and ABI filters. It also includes release build configurations for minification andproguard. ```gradle apply plugin: 'com.android.library' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) } android { namespace "com.company.myfeature" compileSdk getProperty("unity.compileSdkVersion") as int buildToolsVersion = getProperty("unity.buildToolsVersion") compileOptions { sourceCompatibility JavaVersion.valueOf(getProperty("unity.javaCompatabilityVersion")) targetCompatibility JavaVersion.valueOf(getProperty("unity.javaCompatabilityVersion")) } defaultConfig { minSdk getProperty("unity.minSdkVersion") as int targetSdk getProperty("unity.targetSdkVersion") as int ndk { abiFilters.addAll(getProperty("unity.abiFilters").tokenize(',')) debugSymbolLevel getProperty("unity.debugSymbolLevel") } versionCode getProperty("unity.versionCode") as int versionName getProperty("unity.versionName") } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } ``` -------------------------------- ### Accessing Android Device Information in Unity Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-API This snippet shows how to access various device-specific properties using the SystemInfo class in Unity. It covers retrieving the unique device identifier, device name, model, and operating system information, which are particularly relevant for Android development. ```csharp using UnityEngine; public class DeviceInfoReader : MonoBehaviour { void Start() { LogDeviceInfo(); } void LogDeviceInfo() { // SystemInfo.deviceUniqueIdentifier: Returns the md5 of ANDROID_ID. Debug.Log("Device Unique Identifier: " + SystemInfo.deviceUniqueIdentifier); // SystemInfo.deviceName: Tries to read 'device_name' and 'bluetooth_name'. Returns '' if not found. Debug.Log("Device Name: " + SystemInfo.deviceName); // SystemInfo.deviceModel: Returns the device model (e.g., "LGE Nexus 5"). Debug.Log("Device Model: " + SystemInfo.deviceModel); // SystemInfo.operatingSystem: Returns the OS name and version (e.g., "Android 10"). Debug.Log("Operating System: " + SystemInfo.operatingSystem); } } ``` -------------------------------- ### Access System Language in Unity Android (C#) Source: https://docs.unity3d.com/6000.3/Documentation/Manual/android/-call-java-kotlin-code-best-practices Demonstrates how to access the system's default language in a Unity Android application using C#. It utilizes 'using' statements for optimal resource management of AndroidJavaObject and AndroidJavaClass, minimizing JNI calls for improved performance. ```csharp using UnityEngine; public class LocaleExample : MonoBehaviour { void Start() { using (AndroidJavaClass cls = new AndroidJavaClass("java.util.Locale")) using (AndroidJavaObject locale = cls.CallStatic("getDefault")) { if (locale != null) { Debug.Log("current lang = " + locale.Call("getDisplayLanguage")); } } } } ```