如何将Unity嵌入到Flutter中

问题描述 投票:0回答:1

我在让 Flutter 与 Unity 一起工作方面遇到了非常困难的时期。 pub.dev 上的 xraph.com 有一个名为 flutter_unity_widget 的库,我正在努力使其工作。

规格:

  • Android,不使用模拟器/模拟器,仅使用我的真实/物理手机(Android v14 API 34 上的三星 Galaxy Z Fold 4)

  • Unity 2022.3.30f1。目前正在下载并试用 2022.2.0f1 作为当前的 fuw-XXXX.unitypackage 文件,可在 unitypackages 文件夹中找到。 “尝试使用最新的unitypackage。”是dev写的,github文件夹中的当前版本是fuw-2022.2.0、fuw-2022.1.7f1、fuw-2022.1.1-v2。版本与Unity版本相对应。

  • 颤动

flutter doctor -v
  \[√\] Flutter (Channel stable, 3.19.2, on Microsoft Windows \[Version 10.0.19045.4780\], locale en-CA)
  • Flutter version 3.19.2 on channel stable at C:\\Users\\plazm\\flutter
  • Upstream repository https://github.com/flutter/flutter.git
  • Framework revision 7482962148 (7 months ago), 2024-02-27 16:51:22 -0500
  • Engine revision 04817c99c9
  • Dart version 3.3.0
  • DevTools version 2.31.1

\[√\] Windows Version (Installed version of Windows is version 10 or higher)

\[√\] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at C:\\Users\\plazm\\AppData\\Local\\Android\\sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: C:\\Program Files\\Android\\Android Studio\\jbr\\bin\\java
• Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)
• All Android licenses accepted.

\[√\] Chrome - develop for the web
• Chrome at C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe

\[!\] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.6)
• Visual Studio at C:\\Program Files\\Microsoft Visual Studio\\2022\\Community
• Visual Studio Community 2022 version 17.9.34728.123
X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for
the "Desktop development with C++" workload, and include these components:
MSVC v142 - VS 2019 C++ x64/x86 build tools
\- If there are multiple build tool versions available, install the latest
C++ CMake tools for Windows
Windows 10 SDK

\[√\] Android Studio (version 2023.2)
• Android Studio at C:\\Program Files\\Android\\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)

\[√\] VS Code (version 1.93.0)
• VS Code at C:\\Users\\plazm\\AppData\\Local\\Programs\\Microsoft VS Code
• Flutter extension version 3.96.0

\[√\] Connected device (4 available)
• SM F936U (mobile) • RFCW41NBL4H • android-arm64  • Android 14 (API 34)
• Windows (desktop) • windows     • windows-x64    • Microsoft Windows \[Version
10.0.19045.4780\]
• Chrome (web)      • chrome      • web-javascript • Google Chrome 128.0.6613.114
• Edge (web)        • edge        • web-javascript • Microsoft Edge 128.0.2739.42

\[√\] Network resources
• All expected network resources are available.

! Doctor found issues in 1 category.
  • 颤振配置:
static int compileSdkVersion = 34
static int minSdkVersion = 24
static int targetSdkVersion = 33
static String ndkVersion = "23.1.7779620"

This is all in C:\Users\MyPC\flutter\packages\flutter_tools\gradle\src\main\groovy\flutter.groovy
  • 这是我的main.dart
import 'package:flutter/material.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';

void main() {
  runApp(
    const MaterialApp(
      home: UnityDemoScreen(),
    ),
  );
}

class UnityDemoScreen extends StatefulWidget {
  const UnityDemoScreen({Key? key}) : super(key: key);

  @override
  State<UnityDemoScreen> createState() => _UnityDemoScreenState();
}

class _UnityDemoScreenState extends State<UnityDemoScreen> {
  static final GlobalKey<ScaffoldState> _scaffoldKey =
  GlobalKey<ScaffoldState>();
  UnityWidgetController? _unityWidgetController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        bottom: false,
        child: WillPopScope(
          onWillPop: () async {
            // Pop the category page if Android back button is pressed.
            return true;
          },
          child: Container(
            color: Colors.yellow,
            child: UnityWidget(
              onUnityCreated: onUnityCreated,
            ),
          ),
        ),
      ),
    );
  }

  // Callback that connects the created controller to the unity controller
  void onUnityCreated(controller) {
    _unityWidgetController = controller;
  }
}

  • pubspec.yaml
name: flutter_unity
description: "A new Flutter project."
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.3.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.6
  flutter_unity_widget: ^2022.2.1

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^3.0.0

flutter:
  uses-material-design: true
  • IDE
Android Studio Iguana | 2023.2.1 Patch 1
Build #AI-232.10300.40.2321.11567975, built on March 13, 2024
Runtime version: 17.0.9+0--11185874 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 8
Registry:
    ide.experimental.ui=true
    ide.images.show.chessboard=true

Non-Bundled Plugins:
    Dart (232.10313)
    io.flutter (79.0.1)

我遵循了主 github (https://github.com/juicycleff/flutter-unity-view-widget/tree/master) 以及 pub.dev () 上 Android 文档页面上的所有说明https://pub.dev/packages/flutter_unity_widget)和一堆 Youtube 视频教程和在线文章,这些教程和在线文章都执行了相互冲突的步骤,已经过时,并且不起作用。

当我尝试自己执行此操作,除了启动并显示它之外没有其他功能时,我收到以下错误消息,该消息无限重复,并且手机屏幕上没有显示任何内容。

Full output trimmed as it was too long

...
E/BufferQueueProducer(21658): [ImageReader-904x2190f22m4-21658-0](id:549a00000002,api:1,p:1619,c:21658) dequeueBuffer: BufferQueue has been abandoned
I/ViewRootImpl@a411167[MainActivity](21658): onDisplayChanged oldDisplayState=2 newDisplayState=2
E/BufferQueueProducer(21658): [ImageReader-904x2190f22m4-21658-0](id:549a00000002,api:1,p:1619,c:21658) dequeueBuffer: BufferQueue has been abandoned
E/BufferQueueProducer(21658): [ImageReader-904x2190f22m4-21658-0](id:549a00000002,api:1,p:1619,c:21658) dequeueBuffer: BufferQueue has been abandoned

在已知问题下,它说 Unity 在 Android 上冻结并崩溃,请使用 OpenGL3 作为图形 API。 我试过这样做,没有区别。

我手动停止那里的程序,因为“BufferQueue已被放弃”消息无限重复,并且屏幕上只是充满黄色的页面。

flutter unity-game-engine
1个回答
0
投票

我也在 Github Issues 上问了这个问题,并从迄今为止为我工作的开发者 timbotimbo 那里得到了回复/解决方案。我将把这个留给其他有同样问题的人。

Github 问题页面上也有指向问题的链接,因此请查看:https://github.com/juicycleff/flutter-unity-view-widget/issues/985

“您遇到的这个问题是由 Flutter 3.19 中的错误引起的。 这应该在 Flutter 3.22 中得到修复。

如果您必须使用 Flutter 3.19,您唯一的选择是使用 useAndroidViewSurface: true 参数。这会将 Flutter 嵌入模式更改为不受此特定错误影响的替代方案。

如果您升级到 Flutter 3.24,则必须使用此问题中的解决方法,因为该版本需要对 pub.dev 上尚未提供的插件进行小修复。

对于 Unity 版本,请使用 2022.3.x 。" -timbotimbo

© www.soinside.com 2019 - 2024. All rights reserved.