flutter_unity_widget 0.1.3+3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 82

flutter_unity_widget #

version MIT License PRs Welcome

Watch on GitHub Star on GitHub

Flutter unity 3D widget for embedding unity in flutter. Add a Flutter widget to show unity. Works on Android, iOS in works.

Installation #

First depend on the library by adding this to your packages pubspec.yaml:

dependencies:
  flutter_unity_widget: ^0.1.3+2

Now inside your Dart code you can import it.

import 'package:flutter_unity_widget/flutter_unity_widget.dart';

Preview #

gif


Setup Project #

Add Unity Project #

  1. Create an unity project, Example: 'UnityDemoApp'.
  2. Create a folder named unity in flutter project folder.
  3. Move unity project folder to unity folder.

Now your project files should look like this.

.
├── android
├── ios
├── lib
├── test
├── unity
│   └── <Your Unity Project>    // Example: UnityDemoApp
├── pubspec.yml
├── README.md

Configure Player Settings #

  1. First Open Unity Project.

  2. Click Menu: File => Build Settings => Player Settings

  3. Change Product Name to Name of the Xcode project, You can find it follow ios/${XcodeProjectName}.xcodeproj.

    Android Platform:

    1. Change Scripting Backend to IL2CPP.

    2. Mark the following Target Architectures :

      • ARMv7 ✅
      • ARM64 ✅
      • x86 ✅

IOS Platform: 1. Other Settings find the Rendering part, uncheck the Auto Graphics API and select only OpenGLES2. 2. Depending on where you want to test or run your app, (simulator or physical device), you should select the appropriate SDK on Target SDK.

  <img src="https://raw.githubusercontent.com/snowballdigital/flutter-unity-view-widget/master/Screenshot%202019-03-27%2007.31.55.png" width="400" />

Add Unity Build Scripts and Export #

Copy Build.cs and XCodePostBuild.cs to unity/<Your Unity Project>/Assets/Scripts/Editor/

Open your unity project in Unity Editor. Now you can export unity project with Flutter/Export Android or Flutter/Export IOS menu.

Android will export unity project to android/UnityExport.

IOS will export unity project to ios/UnityExport.


Android Platform Only

  1. After exporting the unity game, open Android Studio and and add the Unity Player Java .jar file as a module to the unity project. You just need to do this once if you are exporting from the same version of Unity everytime. The .jar file is located in the <Your Flutter Project>/android/UnityExport/lib folder
  2. Next open build.gradle of flutter_unity_widget module and replace the dependencies with
    dependencies {
        implementation project(':UnityExport') // The exported unity project
        implementation project(':unity-player') // the unity player module you added from step 1
    }
  1. Next open build.gradle of UnityExport module and replace the dependencies with
    dependencies {
        implementation project(':unity-player') // the unity player module you added from step 1
    }
  1. Next open build.gradle of UnityExport module and remove these
    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }

Add UnityMessageManager Support #

Copy UnityMessageManager.cs to your unity project.

Copy this folder JsonDotNet to your unity project.

Copy link.xml to your unity project.


Examples #

Simple Example #

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';

class UnityDemoScreen extends StatefulWidget {

  UnityDemoScreen({Key key}) : super(key: key);

  @override
  _UnityDemoScreenState createState() => _UnityDemoScreenState();
}

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

  Widget build(BuildContext context) {

    return Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        bottom: false,
        child: WillPopScope(
          onWillPop: () {
            // Pop the category page if Android back button is pressed.
          },
          child: Container(
            color: colorYellow,
            child: UnityWidget(
              onUnityViewCreated: onUnityCreated,
            ),
          ),
        ),
      ),
    );
  }

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

Communicating with and from Unity #

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';

class UnityDemoScreen extends StatefulWidget {

  UnityDemoScreen({Key key}) : super(key: key);

  @override
  _UnityDemoScreenState createState() => _UnityDemoScreenState();
}

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


  Widget build(BuildContext context) {

    return Scaffold(
      key: _scaffoldKey,
      body: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: const Text('Unity Flutter Demo'),
        ),
        body: Container(
            child: Stack(
          children: <Widget>[
            UnityWidget(
              onUnityViewCreated: onUnityCreated,
            ),
            Positioned(
              bottom: 40.0,
              left: 80.0,
              right: 80.0,
              child: MaterialButton(
                onPressed: () {

                  if(paused) {
                    _unityWidgetController.resume();
                    setState(() {
                      paused = false;
                    });
                  } else {
                    _unityWidgetController.pause();
                    setState(() {
                      paused = true;
                    });
                  }
                },
                color: Colors.blue[500],
                child: Text(paused ? 'Start Game' : 'Pause Game'),
              ),
            ),
          ],
        )),
      ),
    );
  }

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

API #

  • pause()

Known issues and their fix #

  • Android Export gradle issues

0.0.1 #

  • TODO: Describe initial release.

example/README.md

flutter_unity_widget_example #

Demonstrates how to use the flutter_unity_widget plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_unity_widget: ^0.1.3+3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_unity_widget/flutter_unity_widget.dart';
  
Version Uploaded Documentation Archive
0.1.3+3 Mar 29, 2019 Go to the documentation of flutter_unity_widget 0.1.3+3 Download flutter_unity_widget 0.1.3+3 archive
0.1.3+2 Mar 29, 2019 Go to the documentation of flutter_unity_widget 0.1.3+2 Download flutter_unity_widget 0.1.3+2 archive
0.1.3+1 Mar 29, 2019 Go to the documentation of flutter_unity_widget 0.1.3+1 Download flutter_unity_widget 0.1.3+1 archive
0.1.3 Mar 28, 2019 Go to the documentation of flutter_unity_widget 0.1.3 Download flutter_unity_widget 0.1.3 archive
0.1.2 Mar 10, 2019 Go to the documentation of flutter_unity_widget 0.1.2 Download flutter_unity_widget 0.1.2 archive
0.1.1 Mar 9, 2019 Go to the documentation of flutter_unity_widget 0.1.1 Download flutter_unity_widget 0.1.1 archive
0.1.0 Mar 9, 2019 Go to the documentation of flutter_unity_widget 0.1.0 Download flutter_unity_widget 0.1.0 archive
0.0.6 Mar 9, 2019 Go to the documentation of flutter_unity_widget 0.0.6 Download flutter_unity_widget 0.0.6 archive
0.0.5 Mar 9, 2019 Go to the documentation of flutter_unity_widget 0.0.5 Download flutter_unity_widget 0.0.5 archive
0.0.4 Mar 9, 2019 Go to the documentation of flutter_unity_widget 0.0.4 Download flutter_unity_widget 0.0.4 archive

All 12 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
66
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
94
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jun 12, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.1
  • pana: 0.12.17
  • Flutter: 1.5.4-hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/flutter_unity_widget.dart. (-0.50 points)

Analysis of lib/flutter_unity_widget.dart reported 1 hint:

line 11 col 7: The value of the field '_id' isn't used.

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Maintenance suggestions

The package description is too short. (-6 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8