Android Fragment shows Unity scene

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 as Android
file --> buildingSettings

Select Android and click switch platform.

Build the project

Check export project, building, create a new UnityScene folder to save, the file structure after export is roughly as follows
Insert picture description here

Introduced into the Android project

The following files are placed in the corresponding location in the Android project (I only export arm-v7)
Insert picture description here
unity-classes.jar, armeabi-v7a are placed in the app/libs directory of the Android project; the assets folder is placed in the app/src/main folder , Same level as java. The Android Studio file structure is as follows
Insert picture description here

Added in gradle (same level as buildTypes)

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

add in defaultConfig

   ndk {
    
    
        abiFilters "armeabi-v7a"
    }

Configure ndk in local.properties (replace your own ndk)

ndk.dir=.../Android/sdk/ndk/xxxx

Should be seen in the Android architecture

Android Fragment shows Unity scene

Android Project

Create Fragment

Create UnityScene.java

package com.example.androidproject.unity;

import com.unity3d.player.UnityPlayer;

public class UnityScene {
    
    
    public static UnityPlayer mUnityPlayer;
    public UnityScene(){
    
    

    }

}

Create an empty Fragment, 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();
        //具体参数 跟自己公司Unity开发人员协商
        //第一个参数是unity那边的挂载脚本名字
        //第二个参数是 unity提供的方法名
        //第三个参数是 自己要给unity传的值
//        UnityScene.mUnityPlayer.UnitySendMessage("Main Camera","Id","1");

        return playerView;

    }

Rewrite MainActivity

Add in 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();
}

MainAcivity rewrites other methods

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


@Override
protected void onResume() {
    
    
    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);
}

Increase in gradle

    implementation "androidx.fragment:fragment:1.2.1"
activity_main.xml如下

<?xml version="1.0" encoding="utf-8"?>
<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>

add in string.xml

Game View

After
running the project, you can see that the unity scene is nested in the fragment

Android Fragment shows Unity scene

Finally, I have compiled the learning materials related to Android advanced, audio and video with the big guys. You can privately message me: materials, get them for free
Insert picture description here

Guess you like

Origin blog.csdn.net/A_pyf/article/details/113846569