diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 5d80b8b..4a67054 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -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" diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 8bd86f6..7be3d8b 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1 +1,2 @@ org.gradle.jvmargs=-Xmx1536M +android.enableR8=true diff --git a/example/android/unity-classes/unity-classes.jar b/example/android/unity-classes/unity-classes.jar index da2032e..c11e53d 100644 Binary files a/example/android/unity-classes/unity-classes.jar and b/example/android/unity-classes/unity-classes.jar differ diff --git a/example/lib/main.dart b/example/lib/main.dart index 36d5501..d2e5a58 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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 { static final GlobalKey _scaffoldKey = GlobalKey(); UnityWidgetController _unityWidgetController; - bool paused = false; + double _sliderValue = 0.0; @override void initState() { @@ -28,41 +27,65 @@ class _MyAppState extends State { appBar: AppBar( title: const Text('Unity Flutter Demo'), ), - body: Container( - child: Stack( - children: [ - 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'), + body: Card( + margin: const EdgeInsets.all(8), + clipBehavior: Clip.antiAlias, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20.0), + ), + child: Stack( + children: [ + UnityWidget( + onUnityViewCreated: onUnityCreated, + isARScene: false, + onUnityMessage: onUnityMessage, ), - ), - ], - )), + Positioned( + bottom: 20, + left: 20, + right: 20, + child: Card( + elevation: 10, + child: Column( + children: [ + 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 void onUnityCreated(controller) { this._unityWidgetController = controller; diff --git a/example/unity/DemoApp/Assets/Rotate.cs b/example/unity/DemoApp/Assets/Rotate.cs index aed78f9..ab1e665 100644 --- a/example/unity/DemoApp/Assets/Rotate.cs +++ b/example/unity/DemoApp/Assets/Rotate.cs @@ -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); } } diff --git a/example/unity/DemoApp/ProjectSettings/ProjectVersion.txt b/example/unity/DemoApp/ProjectSettings/ProjectVersion.txt index 1d73517..a572b7b 100644 --- a/example/unity/DemoApp/ProjectSettings/ProjectVersion.txt +++ b/example/unity/DemoApp/ProjectSettings/ProjectVersion.txt @@ -1 +1,2 @@ -m_EditorVersion: 2018.3.9f1 +m_EditorVersion: 2019.3.0a8 +m_EditorVersionWithRevision: 2019.3.0a8 (8ea4afdbfa47)