赞
踩
并在该项目文件下新建一个unity文件夹
1.新建项目
1.去github https://github.com/juicycleff/flutter-unity-view-widget 将unityPackages下载下来
2.在unity中导入unitypackage.
这里看自己情况导入v4或者v4.1.0(我也不知道有什么区别,当练手试试水)
导入成功后Assets会多出一个FlutterUnityIntegration文件夹,unity菜单栏会有一个flutter选项
3.给3D模型绑定脚本(这里用的是demo里面的现成脚本),将rotate.cs拖到模型上
在unity菜单栏 file>building settings,切换Platform为Android,打开Player Settings
在导出过程中报错
根据错误提示,打开build.cs
在DoBuildAndroid方法中
//将var options = BuildOptions.AcceptExternalModificationsToPlayer替换
var options = BuildOptions.AllowDebugging;
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
在DoBuildIOS方法中
//将var options = BuildOptions.AcceptExternalModificationsToPlayer替换
var options = BuildOptions.AllowDebugging;
然后保存一下,重新导出就可以了
导出成功后会在flutter的Android文件下生成一个unityLibrary
然后用AS打开Android文件,在build的过程中会报错提示
在gradle.properties文件里加上
unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService-Info.plist
然后重新build,build成功。再接着运行一下程序看是否有问题
在运行的时候又报错了!!!!
根据错误提示,minSdkVersion 16 改成19,接着运行!然后结合报错!!
提示NDK is not install
这个时候打开local.properties,设置ndk,需要注意的是其中的版本要换成你自己unity的版本,我的是2020.3.30f1c1
ndk.dir=/Applications/Unity/Hub/Editor/2020.3.30f1c1/PlaybackEngines/AndroidPlayer/NDK
再接着运行!终于运行成功!!
引入插件,pub get,https://pub.flutter-io.cn/packages/flutter_unity_widget查看最新的插件版本
flutter_unity_widget: ^2020.3.25
接着运行一下flutter项目
然后报错!!!!
按照提示,将ext.kotlin_version = ‘1.6.0’
再接着build,运行,然后又又又报错!!!T^T
根据提示包冲突了,那我就在所有引用unity-classes包的地方,将implementation改成compileOnly
再运行一下,终于好了!!T^T
import 'package:flutter/material.dart'; import 'package:flutter_unity_widget/flutter_unity_widget.dart'; class UnityTestPage extends StatefulWidget { const UnityTestPage({Key? key}) : super(key: key); @override _UnityTestPageState createState() => _UnityTestPageState(); } class _UnityTestPageState extends State<UnityTestPage> { UnityWidgetController? _unityWidgetController; double _sliderValue = 0.0; @override void initState() { super.initState(); } @override void dispose() { _unityWidgetController!.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Test Unity')), body: Container( width: double.infinity, child: Stack( children: [ ///这里需要多看看源码里面的参数 UnityWidget( onUnityCreated: onUnityCreated, onUnityMessage: onUnityMessage, onUnitySceneLoaded: onUnitySceneLoaded, fullscreen: false, ), Positioned( bottom: 10, left: 20, child: Container( width: 500, child: Column( children: [ 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: Text("Pause"), ), MaterialButton( onPressed: () { _unityWidgetController!.resume(); }, child: Text("Resume"), ), ], ), ], ), )), ], ), ), ), ); } void setRotationSpeed(String speed) { //这里需要看看给3d模型绑定的脚本 _unityWidgetController!.postMessage( 'Cube', 'SetRotationSpeed', speed, ); } void onUnityMessage(message) { print('Received message from unity: ${message.toString()}'); } void onUnitySceneLoaded(scene) { print('Received scene loaded from unity: ${scene.name}'); print('Received scene loaded from unity buildIndex: ${scene.buildIndex}'); } // Callback that connects the created controller to the unity controller void onUnityCreated(controller) { this._unityWidgetController = controller; } }
运行程序,看看效果
test
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。