Demo: github.com/Rebecca-yh/…

Unity import Android instructions

New project

First, create a Unity project and an Android project corresponding to the UnityScene and AndroidProject of this demo

Unity Scene

Configure Unity for Android

file –> buildingSettings

Select Android and click Switch Platform.

Build the project

Select Export Project, building and create UnityScene folder to save, and the structure of the exported file is roughly as follows

Into the Android project

Put the following files in the corresponding location in the Android project (I only export ARM-V7)

Unity-classes. jar, armeabi-v7a in the Android project app/libs directory; The assets folder is stored in the app/ SRC /main folder, which is the same as the Java folder. The Android Studio file structure is as follows

Add to Gradle (same as buildTypes)

sourceSets {
    main {
        //unity3D
        jniLibs.srcDirs = ['libs', 'libs-sdk']
    }
}
Copy the code

DefaultConfig add

        ndk {
            abiFilters "armeabi-v7a"
        }
Copy the code

Configure NDK in local.properties (instead of your own NDK)

ndk.dir=... /Android/sdk/ndk/xxxxCopy the code

It should be seen in the Android architecture

Android Project

Create fragments

Create UnityScene. Java

package com.example.androidproject.unity;

import com.unity3d.player.UnityPlayer;

public class UnityScene {
    public static UnityPlayer mUnityPlayer;
    public UnityScene(a){}}Copy the code

Create an empty Fragment and add the following code

 private View playerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        playerView = UnityScene.mUnityPlayer.getView();
        // Negotiate specific parameters with your Unity developers
        // The first parameter is the name of unity's mount script
        // The second argument is the method name provided by Unity
        // The third argument is the value you pass to Unity
// UnityScene.mUnityPlayer.UnitySendMessage("Main Camera","Id","1");

        return playerView;

    }
Copy the code

Rewrite the MainActivity

Inside the onCreate of MainAcivity

UnityScene.mUnityPlayer = new UnityPlayer(getApplicationContext());
getWindow().setFormat(PixelFormat.RGBX_8888);

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .setReorderingAllowed(true)
            .add(R.id.fragment_container_view, UnityFragment.class, null)
            .commit();
}
Copy the code

MainAcivity overrides other methods

@Override
protected void onDestroy(a) {
    UnityScene.mUnityPlayer.quit();
    super.onDestroy();
}
@Override
protected void onPause(a) {
    super.onPause();
    UnityScene.mUnityPlayer.pause();
}


@Override
protected void onResume(a) {
    super.onResume();
    UnityScene.mUnityPlayer.resume();
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    UnityScene.mUnityPlayer.configurationChanged(newConfig);
}


@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    UnityScene.mUnityPlayer.windowFocusChanged(hasFocus);
}


@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
        return UnityScene.mUnityPlayer.injectEvent(event);
    return super.dispatchKeyEvent(event);
}


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    return UnityScene.mUnityPlayer.injectEvent(event);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    return UnityScene.mUnityPlayer.injectEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    return UnityScene.mUnityPlayer.injectEvent(event);
}


public boolean onGenericMotionEvent(MotionEvent event) {
    return UnityScene.mUnityPlayer.injectEvent(event);
}
Copy the code

Increase in the gradle

Implementation "androidx fragments, fragments: 1.2.1."Copy the code

Activity_main. XML is as follows


      
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code

String. The XML to add

<string name="game_view_content_description">Game View</string>
Copy the code

complete

Run the project and see the Unity scene nested in the Fragment