当前位置:网站首页>Detailed explanation of shuttle unity interworking principle
Detailed explanation of shuttle unity interworking principle
2022-07-03 19:18:00 【jieqiang3】
List of articles
The whole idea
This article mainly introduces a flutter-unity Principle of open source framework for interoperability , Address github, The use process is very clear in the warehouse , We mainly look at how to make a unity The scene is displayed quickly and excellently in flutter Developed app in .
ok, First, let's talk about the overall design idea .
From the result theory, we can deduce , We are going to put a unity The scene is transformed into a flutter Of widget, Want to get a flutter Of widget, The best solution is naturally to create a native native Level of view perhaps UIView Convert to flutter Of widget. So we're going to put native Level of unityPlayerActivity perhaps viewController Convert to native Layer of view perhaps UIView. and unityPlayerActivity and viewController By unity In Engineering export Android and export iOS Export the corresponding generated . Here is a difficulty in how we put the original unity There are only export android apk and export ios bundle Two button options , Normal to be flutter Words used , You have to turn it into module Or a pod library .
This is the whole idea , It's simple , In order to figure out how to realize the difficulties , Let's sort it out first unity pack Android and iOS Structure .
tips: This article untiy Version is 2019.4.3f1, It is recommended to use this version ,2020 I've tried this version. There will be various packaging problems .
The principle is mainly explained here Android 了 ,iOS The basic process is similar .
unity pack

We directly build Out android apk Then decompile to see the structure , Attach the figure of some key points .
This is decompilation apk The directory structure ,assets The lower level corresponds to the scene unity resources , It contains unity Most resources in the scene will be integrated data.unity3d Binary ,lib What's going down is unity dependent so file . It is obvious that we will take module We all need these documents for formal quotation , Let's see AndroidManifest.xml in , Obviously a normal one unity Of the scene apk load unity The scene is here UnityPlayerActivity in , Let's see UnityPlayerActivity.smali Code for 

Here we can see , It is equivalent to creating a new UnityPlayer object , And then directly setContentView Went in. , So it's very simple , We can take this UnityPlayer Understand as a view 了 .
unity The scene is flutter Medium display principle
export
First step , We need to solve how to unity export apk convert to android module The problem of .
Look up unity Official documents we found , pack api by BuildPipeline.BuildPlayer(), See documentation unity documentation.unity Support custom adding projects menu item, stay Build.cs Add logic to the file .
Here we can actually do such operations , Type it out apk Next assets In the catalog unity Related resource files ,AndroidManifest.xml,build.gradle Wait for the file to be copied to a module Under the table of contents , Then modify it in the file , For example build.gradle Under the com.android.application Change to com.android.library Change its declaration to a library module Wait for the operation . It can be understood as directly modifying a file form with code phone module Change to library module. And in android native layer settings.gradle This is in the document library module Quote in , These are all android Basic knowledge of .
The code is as follows :
Pay attention here , In consideration of generality , So that it can be modified directly through the relative path , Need to put unity Move the project to your flutter Under the project of unity Under the table of contents .
ok, After this step, we can play a game like this module 了 .
thus android native The work of the layer is done , This part of the work is mainly cumbersome , The logic is clear .
flutter module
native After the work of layer is completed, we will build a flutter plugin, to flutter project Used .
First realize registerWith Method , Code is located FlutterUnityWidgetPlugin.kt in , Here to LifecycleOwner Made special treatment and judgment , Yes LifecycleOwner Unfamiliar students can watch here .
The next step is to create FlutterUnityWidgetFactory Integrate PlatformViewFactory, To achieve its create Method to create UnityView.
Let's turn to FlutterUnityWidgetController.kt, It inherited the PlatformView.


Here we create and load our UnityView. Here we should pay attention to the need to introduce unity-class.jar, Otherwise flutter It is not available in the project , The principle here and native Layer de loading UnityPlayerActivity Empathy .
Here we also need to pay attention to onMethodCall Method 
Respectively in UnityPlayerUtils Class implements several related lifecycle methods for flutter call .
Next we just have to flutter plugin Layer to create corresponding UnityWidget That's it . Code in unity_view.dart in .
data communication
Data communication , We can see mainly through native turnover ,flutter and native Communication between ,native And again unity To communicate 
ok, Basically, the principle is like this .
边栏推荐
- Common text labels
- “google is not defined” when using Google Maps V3 in Firefox remotely
- I study database at station B (4): DQL
- Free hand account sharing in September - [cream Nebula]
- I didn't cancel
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
- EGO Planner代码解析bspline_optimizer部分(1)
- The most valuable thing
- Web Security (VIII) what is CSRF attack? Why can token prevent csdf attacks?
- Valentine's Day - make an exclusive digital collection for your lover
猜你喜欢
![[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD](/img/51/209e35e0b94a51b3b406a184459475.png)
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD

Dart JSON编码器和解码器剖析

Flutter network and data storage framework construction-b1

This Chinese numpy quick look-up table is too easy!

【光学】基于matlab涡旋光产生【含Matlab源码 1927期】

How to build an efficient information warehouse
![[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]](/img/cb/ee696d5a7d6bef96fe0db89e8478ed.jpg)
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]

Buuctf's different flags and simplerev

Summary of composition materials for 2020 high-frequency examination center of educational resources

Yolov3 network model building
随机推荐
During MySQL installation, the download interface is empty, and the components to be downloaded are not displayed. MySQL installer 8.0.28.0 download interface is empty solution
Succession of flutter
Sentinel source code analysis part I sentinel overview
application
PyTorch中在反向传播前为什么要手动将梯度清零?
Flask generates swagger documents
A green plug-in that allows you to stay focused, live and work hard
Ego planner code parsing Bspline_ Optimizer section (2)
HOW TO WRITE A DAILY LAB NOTE?
I study database at station B (4): DQL
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
Free year-end report summary template Welfare Collection
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
FBI warning: some people use AI to disguise themselves as others for remote interview
Read the paper glodyne global topology preserving dynamic network embedding
[wallpaper] (commercially available) 70 wallpaper HD free
01. Preparation for automated office (free guidance, only three steps)
Summary of composition materials for 2020 high-frequency examination center of educational resources
Max of PHP FPM_ Some misunderstandings of children
Pecan — @expose()