This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Rex Raphael 106a0a4ea5
Add files via upload
2019-03-27 22:12:04 +01:00
.idea WIP: working example 2019-03-26 22:06:17 +01:00
android WIP: working example 2019-03-26 22:06:17 +01:00
example updated readme 2019-03-27 19:56:47 +01:00
ios WIP: working example 2019-03-26 22:06:17 +01:00
lib WIP: working example 2019-03-26 22:06:17 +01:00
res/values Support for android complete 2019-03-09 16:47:09 +01:00
.gitignore Support for android complete 2019-03-09 16:47:09 +01:00
.metadata Support for android complete 2019-03-09 16:47:09 +01:00
2019_03_27_22_09_38.gif Add files via upload 2019-03-27 22:12:04 +01:00
CHANGELOG.md Support for android complete 2019-03-09 16:47:09 +01:00
LICENSE improved readme 2019-03-27 08:30:12 +01:00
README.md updated readme 2019-03-27 20:01:55 +01:00
Screenshot 2019-03-27 07.31.55.png Add files via upload 2019-03-27 08:22:52 +01:00
Screenshot 2019-03-27 08.13.08.png Add files via upload 2019-03-27 08:22:52 +01:00
flutter_unity_widget.iml Support for android complete 2019-03-09 16:47:09 +01:00
pubspec.yaml android support fuctional 2019-03-10 23:53:08 +01:00

README.md

flutter_unity_widget

version MIT License All Contributors 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.2

Now inside your Dart code you can import it.

import 'package:flutter_graphql/flutter_graphql.dart';

Preview


Setup Project

Add Unity Project

  1. Create an unity project, Example: 'Cube'.
  2. Create a folder named unity in react native 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: UnityDemo App
├── 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.

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,
      backgroundColor: colorBlack,
      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(webController) {
    this._unityWidgetController = webController;
  }
}

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;


  Widget build(BuildContext context) {

    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: colorBlack,
      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(webController) {
    this._unityWidgetController = webController;
  }
}

Known issues and their fix

  • Android Export gragle issues