Android integrates Unity to achieve 3D viewing effect

Primer

A few days ago, someone asked me if I could put the 3D model into Unity, then put Unity into the Android APP, and realize the function of viewing house furniture in the APP. This time, I will share it. If If you still need to understand the knowledge of Android integrated Unity, you can turn to other articles on my homepage

Demonstration effect

Android3D watch house furniture effect demo

resource preparation

Unity 3D

Unity 3D official website

Android Studio

Android Studio official website

House Furniture 3D Model

3D model official website

Unity project

1. Create a Unity project

As shown in the figure below: I used the 2021 long-term support version of the Unity 3D compiler to create a 3D project named UnityDemo 

As shown in the figure below: This is the created UnityDemo 3D empty project

2. Import house furniture 3D model resources

As shown in the figure below: we first open the Package Manager window, and then we can see the resources we downloaded in the Unity resource store, find the 3D model resources we need, and then click import to import the resources into the UnityDemo project

As shown in the figure below: This is after importing model resources

As shown in the figure below: We open the Free Set scene, and the scene effect picture is as follows

3. Add script event

Next, we need to add a touch screen rotation event to it, so as to achieve the effect of our display of 3D viewing 

CameraController script:

using UnityEngine;

public class CameraController : MonoBehaviour
{
    private float rotateSpeed = 0.01f;

    private Camera cam;
    private Vector3 lastPosition;

    void Start()
    {
        cam = GetComponent<Camera>();
    }

    [System.Obsolete]
    void Update()
    {
        // 旋转
        if (Input.GetMouseButton(0))
        {
            Vector3 delta = Input.mousePosition - lastPosition;
            cam.transform.RotateAround(Vector3.up, delta.x * rotateSpeed);
            cam.transform.RotateAround(cam.transform.right, -delta.y * rotateSpeed);
        }

        lastPosition = Input.mousePosition;
    }
}

4. Unity running demonstration effect

Unity 3D viewing house furniture effect

5. Modify Unity packaging configuration information

As shown in the figure below, before packaging the Unity project into an Android project, we need to modify some configurations. We first set it to the horizontal screen effect of the mobile phone

As shown in the figure below, let's set the minimum sdk support and the best SDK support, and then check ARMv7 and ARM64, and check x86 and x86-64 by the way, so that it can be run on the emulator later

6. Unity package Android project

As shown in the figure below, we choose a good packaging platform for packaging

As shown in the figure below, this is the folder structure after packaging the Unity project into an Android project

7. Modify Android project configuration information

As shown in the figure below, we first use Android Studio to open the project just packaged by Unity, and modify the gradle.properties file

As shown below

8. Generate aar file

As shown in the figure below, the editor will generate an aar file from the Android project packaged by our Unity 

Android project

1. Create a new Android project

The operation is shown in the figure below:

2. Put the aar into the Android project

3. Add string configuration

<string name="game_view_content_description">Game View</string>

4. Write the Activity page

As shown below

MainActivity.kt

package com.example.unityandroiddemo

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var btn:Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn = findViewById(R.id.btn)

        btn.setOnClickListener {
            val intent: Intent = Intent(this, UnityActivity::class.java)
            startActivity(intent)
        }
    }
}

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">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="观看3D模型"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

UnityActivity.kt

package com.example.unityandroiddemo

import com.unity3d.player.UnityPlayerActivity

class UnityActivity : UnityPlayerActivity() {

}

AndroidMainfest.xml

So far, we have completed the initial stage, and we can run it to the real machine to see the effect

Demonstration effect

Android3D watch house furniture effect demo

If you have any questions, you can add me on WeChat yf1553653788

Guess you like

Origin blog.csdn.net/Ai1114/article/details/132446793