Updated Android sample to showcase "Flutter to Unity" and "Unity to Flutter" functionality
This commit is contained in:
parent
fa7a82b03f
commit
c49ae527ab
|
@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
|
|||
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
|
||||
|
||||
android {
|
||||
compileSdkVersion 27
|
||||
compileSdkVersion 28
|
||||
|
||||
lintOptions {
|
||||
disable 'InvalidPackage'
|
||||
|
@ -34,8 +34,8 @@ android {
|
|||
defaultConfig {
|
||||
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
|
||||
applicationId "com.rexraphael.flutterunitywidgetexample"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 27
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 28
|
||||
versionCode flutterVersionCode.toInteger()
|
||||
versionName flutterVersionName
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
org.gradle.jvmargs=-Xmx1536M
|
||||
android.enableR8=true
|
||||
|
|
Binary file not shown.
|
@ -1,5 +1,4 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
@ -13,7 +12,7 @@ class _MyAppState extends State<MyApp> {
|
|||
static final GlobalKey<ScaffoldState> _scaffoldKey =
|
||||
GlobalKey<ScaffoldState>();
|
||||
UnityWidgetController _unityWidgetController;
|
||||
bool paused = false;
|
||||
double _sliderValue = 0.0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
@ -28,41 +27,65 @@ class _MyAppState extends State<MyApp> {
|
|||
appBar: AppBar(
|
||||
title: const Text('Unity Flutter Demo'),
|
||||
),
|
||||
body: Container(
|
||||
body: Card(
|
||||
margin: const EdgeInsets.all(8),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20.0),
|
||||
),
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
UnityWidget(
|
||||
onUnityViewCreated: onUnityCreated,
|
||||
isARScene: false,
|
||||
onUnityMessage: onUnityMessage,
|
||||
),
|
||||
Positioned(
|
||||
bottom: 40.0,
|
||||
left: 80.0,
|
||||
right: 80.0,
|
||||
child: MaterialButton(
|
||||
onPressed: () {
|
||||
|
||||
if(paused) {
|
||||
_unityWidgetController.resume();
|
||||
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(() {
|
||||
paused = false;
|
||||
_sliderValue = value;
|
||||
});
|
||||
} else {
|
||||
_unityWidgetController.pause();
|
||||
setState(() {
|
||||
paused = true;
|
||||
});
|
||||
}
|
||||
setRotationSpeed(value.toString());
|
||||
},
|
||||
color: Colors.blue[500],
|
||||
child: Text(paused ? 'Start Game' : 'Pause Game'),
|
||||
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
|
||||
void onUnityCreated(controller) {
|
||||
this._unityWidgetController = controller;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class Rotate : MonoBehaviour
|
||||
public class Rotate : MonoBehaviour, IEventSystemHandler
|
||||
{
|
||||
[SerializeField]
|
||||
Vector3 RotateAmount;
|
||||
|
@ -10,12 +10,35 @@ public class Rotate : MonoBehaviour
|
|||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
RotateAmount = new Vector3(10, 10, 10);
|
||||
RotateAmount = new Vector3(0, 0, 0);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
m_EditorVersion: 2018.3.9f1
|
||||
m_EditorVersion: 2019.3.0a8
|
||||
m_EditorVersionWithRevision: 2019.3.0a8 (8ea4afdbfa47)
|
||||
|
|
Reference in New Issue