實現(xiàn)方案:采用flutter開源組件flutter_unity_widget
環(huán)境搭建Unity
1、創(chuàng)建flutter項目flutter_unity_demo
2、在pubspec.paml文件dependencies添加flutter_unity_widget: ^2022.2.0,執(zhí)行Pub?get導(dǎo)入組件
3、在工程目錄下創(chuàng)建unity文件夾
4、在unity目錄下創(chuàng)建unity_demo的3D工程
5、下載flutter_unity_widget提供的unity插件
在unity_demo項目Assets右鍵導(dǎo)入(import?package)插件unitypackages(注:我導(dǎo)入的是FlutterUnityIntegration-v4-WithDemo)會得到以下目錄,這個時候Flutter打包的方式如下圖
7、修改Assets/FlutterUnityIntegration/Editor/Build.cs文件
DoBuildAndroid方法:
var options = BuildOptions.AcceptExternalModificationsToPlayer;
替換為:
var options = BuildOptions.AllowDebugging;
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
BuildIOS?方法:
var options = BuildOptions.AcceptExternalModificationsToPlayer;
替換為:
var options = BuildOptions.AllowDebugging;
8、為了方便測試創(chuàng)建Cube,綁定Assets/FlutterUnityIntegration/Demo/Rotote.cs腳本并調(diào)整好攝像頭對準(zhǔn)新創(chuàng)建的Cube
Flutter加載unity
- 創(chuàng)建UnityPage頁面實現(xiàn)以下代碼
import 'package:flutter/material.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
class UnityPage extends StatefulWidget {
??const UnityPage({super.key});
??@override
??_UnityPageState createState() => _UnityPageState();
}
class _UnityPageState extends State<UnityPage> {
??UnityWidgetController? _unityWidgetController;
??double _sliderValue = 0.0;
??@override
??void initState() {
????// TODO: implement initState
????super.initState();
??}
??@override
??void dispose() {
????// TODO: implement dispose
????_unityWidgetController?.dispose();
????super.dispose();
??}
??@override
??Widget build(BuildContext context) {
????// TODO: implement build
????return Scaffold(
??????appBar: AppBar(
????????title: const Text("this Unity"),
??????),
??????body: Container(
????????child: Stack(
??????????children: [
????????????UnityWidget(
????????????????onUnityCreated: onUnityCreated,
????????????????onUnityMessage: onUnityMessage,
????????????????onUnitySceneLoaded: onUnitySceneLoaded,
????????????????fullscreen: false),
????????????Positioned(
????????????????bottom: 10,
????????????????left: 15,
????????????????right: 15,
????????????????child: Container(
??????????????????width: MediaQuery.of(context).size.width,
??????????????????child: Column(
????????????????????children: [
??????????????????????Padding(
????????????????????????padding: const EdgeInsets.all(8.0),
????????????????????????child: Slider(
??????????????????????????onChanged: (value) {
????????????????????????????setState(() {
??????????????????????????????_sliderValue = value;
????????????????????????????});
????????????????????????????setRotationSpeed(value.toString());
??????????????????????????},
??????????????????????????value: _sliderValue,
??????????????????????????min: 0,
??????????????????????????max: 20,
????????????????????????),
??????????????????????),
??????????????????????Row(
????????????????????????mainAxisAlignment: MainAxisAlignment.spaceBetween,
????????????????????????children: [
??????????????????????????MaterialButton(
????????????????????????????onPressed: () {
??????????????????????????????_unityWidgetController!.pause();
????????????????????????????},
????????????????????????????child: const Text("Pause"),
??????????????????????????),
??????????????????????????// Expanded(child: SizedBox()),
??????????????????????????MaterialButton(
????????????????????????????onPressed: () {
??????????????????????????????_unityWidgetController!.resume();
????????????????????????????},
????????????????????????????child: const Text("Resume"),
??????????????????????????),
????????????????????????],
??????????????????????),
????????????????????],
??????????????????),
????????????????)),
??????????],
????????),
??????),
????);
??}
??void setRotationSpeed(String speed) {
????//這里需要看看給3d模型綁定的腳本
????_unityWidgetController!.postMessage(
??????'Cube',
??????'SetRotationSpeed',
??????speed,
????);
??}
??void onUnitySceneLoaded(scene) {
????print('Received scene loaded from unity: ${scene.name}');
????print('Received scene loaded from unity buildIndex: ${scene.buildIndex}');
??}
??void onUnityMessage(message) {}
??void onUnityCreated(controller) {
????this._unityWidgetController = controller;
??}
}
2、在main.dart修改flutter啟動頁面為UnityPage
3、等待原生集成unity后運(yùn)行
iOS集成
1、打開unity項目?file?-?Build?Setings選擇iOS(iOS打包插件沒安裝點擊右側(cè)按鈕安裝)設(shè)置需要的配置信息
?
- 使用flutter?-?export?ios生成iOS工程文件,文件會自動創(chuàng)建到flutter項目下ios文件夾內(nèi)
?
- 打開flutter項目內(nèi)iOS工程Runner.xcworkspace,在終端cd?到flutter項目內(nèi)iOS目錄下。執(zhí)行pod?install?導(dǎo)入flutter引用的組件
導(dǎo)入UnityLibrary工程,在Runner工程目錄下方空白右鍵Add?Files?to?“Runner”,選擇UnityLibrary/Unity-iPhone.xcodeproj,點擊Add添加,會得到如下工程目錄結(jié)構(gòu)
5、綁定一下Unity-iPhone?Data資源,編譯一下Unity-iPhone
6、在Runner工程中添加UnityFramework.framework
7、通過flutter_unity_widget初始化unity項目
swift:打開ios/Runner/AppDelegate.swift?添加一下代碼
???? import UIKit
?? ? import Flutter
+? ? import flutter_unity_widget
?? ? @UIApplicationMain
?? ? @objc class AppDelegate: FlutterAppDelegate {
?? ? ? ? override func application(
?? ? ? ? ? ? _ application: UIApplication,
?? ? ? ? ? ? didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
?? ? ? ? ) -> Bool {
+? ? ? ? ? ? InitUnityIntegrationWithOptions(argc: CommandLine.argc, argv: CommandLine.unsafeArgv, launchOptions)
?? ? ? ? ? ? GeneratedPluginRegistrant.register(with: self)
?? ? ? ? ? ? return super.application(application, didFinishLaunchingWithOptions: launchOptions)
?? ? ? ? }
?? ? }
Object-C:打開ios/Runner/main.m?添加一下代碼
+ ???#import "flutter_unity_widget.swift.h"
?????int main(int argc, char * argv[]) {
??????????@autoreleasepool {
+ ????????????InitUnityIntegration(argc, argv);
??????????????return?UIApplicationMain(argc,argv,nil, NSStringFromClass([AppDelegate class]));
??????????}
?????}
8、在Runner項目中ios/Runner/Info.plist添加flutterH5橋梁
<dict>
+ ???????<key>io.flutter.embedded_views_preview</key>
+ ???????<string>YES</string>
</dict>
運(yùn)行flutter至iOS得到以下界面:
Android集成
1、打開unity項目?file?-?Build?Setings選擇Android(Android打包插件沒安裝點擊右側(cè)按鈕安裝)設(shè)置需要的配置信息
2、使用flutter?-?export?Android生成Android工程文件,文件會自動創(chuàng)建到flutter項目下android文件夾內(nèi),也會自動導(dǎo)入Android項目
?
3、配置NDK信息打開android/local.properties添加ndk.dir=/Applications/Unity/Hub/Editor/2020.3.19f1/PlaybackEngines/AndroidPlayer/NDK??注:2020.3.19f1替換為自己安裝的unity版本號
- 打開android/app/build.gradle增加以下代碼
?dependencies {
+ ???????implementation project(':unityLibrary')
?????}
?????buildTypes {
?????????release {
?????????????signingConfig signingConfigs.debug
?????????}
+ ???????debug {
+ ???????????signingConfig signingConfigs.debug
+ ???????}
+ ???????profile {
+ ???????????signingConfig signingConfigs.debug
+ ???????}
+ ???????innerTest {
+ ???????????matchingFallbacks = ['debug', 'release']
+ ???????}
}
- 打開gradle.properties添加:
unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService-Info.plist
注:運(yùn)行遇到minSdkVersion版本報錯,修改/flutter/packages/flutter_tools/gradle/flutter.gradle里的minSdkVersion
運(yùn)行Flutter到Android設(shè)備得到:文章來源:http://www.zghlxwxcb.cn/news/detail-673259.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-673259.html
到了這里,關(guān)于Flutter通過flutter_unity_widget嵌入Unity3D的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!