Updated Android sample to showcase "Flutter to Unity" and "Unity to Flutter" functionality

This commit is contained in:
Thomas Stockx 2019-10-07 16:29:02 +02:00
parent fa7a82b03f
commit c49ae527ab
6 changed files with 88 additions and 40 deletions

View File

@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android { android {
compileSdkVersion 27 compileSdkVersion 28
lintOptions { lintOptions {
disable 'InvalidPackage' disable 'InvalidPackage'
@ -34,8 +34,8 @@ android {
defaultConfig { defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.rexraphael.flutterunitywidgetexample" applicationId "com.rexraphael.flutterunitywidgetexample"
minSdkVersion 16 minSdkVersion 19
targetSdkVersion 27 targetSdkVersion 28
versionCode flutterVersionCode.toInteger() versionCode flutterVersionCode.toInteger()
versionName flutterVersionName versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

View File

@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.enableR8=true

View File

@ -1,5 +1,4 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_unity_widget/flutter_unity_widget.dart'; import 'package:flutter_unity_widget/flutter_unity_widget.dart';
void main() => runApp(MyApp()); void main() => runApp(MyApp());
@ -13,7 +12,7 @@ class _MyAppState extends State<MyApp> {
static final GlobalKey<ScaffoldState> _scaffoldKey = static final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>(); GlobalKey<ScaffoldState>();
UnityWidgetController _unityWidgetController; UnityWidgetController _unityWidgetController;
bool paused = false; double _sliderValue = 0.0;
@override @override
void initState() { void initState() {
@ -28,41 +27,65 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar( appBar: AppBar(
title: const Text('Unity Flutter Demo'), title: const Text('Unity Flutter Demo'),
), ),
body: Container( body: Card(
child: Stack( margin: const EdgeInsets.all(8),
children: <Widget>[ clipBehavior: Clip.antiAlias,
UnityWidget( shape: RoundedRectangleBorder(
onUnityViewCreated: onUnityCreated, borderRadius: BorderRadius.circular(20.0),
), ),
Positioned( child: Stack(
bottom: 40.0, children: <Widget>[
left: 80.0, UnityWidget(
right: 80.0, onUnityViewCreated: onUnityCreated,
child: MaterialButton( isARScene: false,
onPressed: () { onUnityMessage: onUnityMessage,
if(paused) {
_unityWidgetController.resume();
setState(() {
paused = false;
});
} else {
_unityWidgetController.pause();
setState(() {
paused = true;
});
}
},
color: Colors.blue[500],
child: Text(paused ? 'Start Game' : 'Pause Game'),
), ),
), Positioned(
], bottom: 20,
)), left: 20,
right: 20,
child: Card(
elevation: 10,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20),
child: Text("Rotation speed:"),
),
Slider(
onChanged: (value) {
setState(() {
_sliderValue = value;
});
setRotationSpeed(value.toString());
},
value: _sliderValue,
min: 0,
max: 20,
),
],
),
),
),
],
),
),
), ),
); );
} }
void setRotationSpeed(String speed) {
_unityWidgetController.postMessage(
'Cube',
'SetRotationSpeed',
speed,
);
}
void onUnityMessage(controller, message) {
print('Received message from unity: ${message.toString()}');
}
// Callback that connects the created controller to the unity controller // Callback that connects the created controller to the unity controller
void onUnityCreated(controller) { void onUnityCreated(controller) {
this._unityWidgetController = controller; this._unityWidgetController = controller;

View File

@ -1,8 +1,8 @@
using System.Collections; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems;
public class Rotate : MonoBehaviour public class Rotate : MonoBehaviour, IEventSystemHandler
{ {
[SerializeField] [SerializeField]
Vector3 RotateAmount; Vector3 RotateAmount;
@ -10,12 +10,35 @@ public class Rotate : MonoBehaviour
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
RotateAmount = new Vector3(10, 10, 10); RotateAmount = new Vector3(0, 0, 0);
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
gameObject.transform.Rotate(RotateAmount * Time.deltaTime * 10); gameObject.transform.Rotate(RotateAmount * Time.deltaTime * 10);
for (int i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase.Equals(TouchPhase.Began))
{
var hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit))
{
// This method is used to send data to Flutter
UnityMessageManager.Instance.SendMessageToFlutter("The cube feels touched.");
}
}
}
}
// This method is called from Flutter
public void SetRotationSpeed(String message)
{
float value = float.Parse(message);
RotateAmount = new Vector3(value, value, value);
} }
} }

View File

@ -1 +1,2 @@
m_EditorVersion: 2018.3.9f1 m_EditorVersion: 2019.3.0a8
m_EditorVersionWithRevision: 2019.3.0a8 (8ea4afdbfa47)