How to embed Unity in Flutter

I am having a very difficult time with making Flutter work with Unity. There is a library called flutter_unity_widget by xraph.com on pub.dev which I am trying to make work.

Specs:

  • Android, not using emulators/simulators and just my real/physical phone (Samsung Galaxy Z Fold 4 on Android v14 API 34)

  • Unity 2022.3.30f1. Currently downloading to try on 2022.2.0f1 as the current fuw-XXXX.unitypackage file, found in the unitypackages folder. “Try to use the most recent unitypackage available.” is what the dev wrote, and the current versions in the github folder is fuw-2022.2.0, fuw-2022.1.7f1, fuw-2022.1.1-v2. The versions correspond to Unity Version.

  • Flutter

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.
  • flutter configurations:
static int compileSdkVersion = 34
static int minSdkVersion = 24
static int targetSdkVersion = 33
static String ndkVersion = "23.1.7779620"

This is all in C:UsersMyPCflutterpackagesflutter_toolsgradlesrcmaingroovyflutter.groovy
  • This is my 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)

I followed all the instructions on the documentation page for Android on the main github (https://github.com/juicycleff/flutter-unity-view-widget/tree/master) as well as the pub.dev (https://pub.dev/packages/flutter_unity_widget) and bunch of Youtube video tutorials and online articles which all did conflicting steps, were outdated, and did not work.

When I try to do it myself with no other functionality other than to just start it and display it, I get the following error message which is infinitely repeated and nothing shows up on the phone screen.

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

Under known issues, it says Unity freezes and crashes on Android, please use OpenGL3 as Graphics API.
I have tried doing this, no difference.

I manually stop the program up there as the “BufferQueue has been abandoned” message gets repeated infinitely and on the screen is just the page full of yellow.

3

I asked this question on the Github Issues as well and got back a reply/solution from the dev timbotimbo that is working for me so far. I will leave this here for anyone else having the same problems.

There are also links to the issues on the Github Issues page, so check it out: https://github.com/juicycleff/flutter-unity-view-widget/issues/985

“You are running into this issue caused by a bug in Flutter 3.19.
This should be fixed in Flutter 3.22.

If you have to use Flutter 3.19, your only option is to use the useAndroidViewSurface: true parameter. This changes the Flutter embedding mode to an alternative that isn’t affected by this specific bug.

If you upgrade to Flutter 3.24, you have to use the workaround in this issue as that version requires a small fix in the plugin that isn’t on pub.dev yet.

For the Unity version, use 2022.3.x .” -timbotimbo

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật