@@ -608,34 +608,23 @@ class UnityDemoScreen extends StatefulWidget {
608
608
}
609
609
610
610
class _UnityDemoScreenState extends State<UnityDemoScreen> {
611
- static final GlobalKey<ScaffoldState> _scaffoldKey =
612
- GlobalKey<ScaffoldState>();
611
+
613
612
UnityWidgetController? _unityWidgetController;
614
613
615
614
@override
616
615
Widget build(BuildContext context) {
617
616
return Scaffold(
618
- key: _scaffoldKey,
619
- body: SafeArea(
620
- bottom: false,
621
- child: WillPopScope(
622
- onWillPop: () async {
623
- // Pop the category page if Android back button is pressed.
624
- return true;
625
- },
626
- child: Container(
627
- color: Colors.yellow,
628
- child: UnityWidget(
629
- onUnityCreated: onUnityCreated,
630
- ),
631
- ),
617
+ body: Container(
618
+ color: Colors.yellow,
619
+ child: UnityWidget(
620
+ onUnityCreated: onUnityCreated,
632
621
),
633
622
),
634
623
);
635
624
}
636
625
637
626
// Callback that connects the created controller to the unity controller
638
- void onUnityCreated(controller) {
627
+ void onUnityCreated(UnityWidgetController controller) {
639
628
_unityWidgetController = controller;
640
629
}
641
630
}
@@ -649,79 +638,71 @@ class _UnityDemoScreenState extends State<UnityDemoScreen> {
649
638
import 'package:flutter/material.dart';
650
639
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
651
640
652
- void main() => runApp(const MyApp());
641
+ void main() {
642
+ runApp(
643
+ const MaterialApp(
644
+ home: UnityDemoScreen(),
645
+ ),
646
+ );
647
+ }
653
648
654
- class MyApp extends StatefulWidget {
655
- const MyApp ({Key? key}) : super(key: key);
649
+ class UnityDemoScreen extends StatefulWidget {
650
+ const UnityDemoScreen ({Key? key}) : super(key: key);
656
651
657
652
@override
658
- State<MyApp > createState() => _MyAppState ();
653
+ State<UnityDemoScreen > createState() => _UnityDemoScreenState ();
659
654
}
660
655
661
- class _MyAppState extends State<MyApp> {
662
- static final GlobalKey<ScaffoldState> _scaffoldKey =
663
- GlobalKey<ScaffoldState>();
656
+ class _UnityDemoScreenState extends State<UnityDemoScreen> {
664
657
UnityWidgetController? _unityWidgetController;
665
658
double _sliderValue = 0.0;
666
659
667
- @override
668
- void initState() {
669
- super.initState();
670
- }
671
-
672
660
@override
673
661
Widget build(BuildContext context) {
674
- return MaterialApp(
675
- home: Scaffold(
676
- key: _scaffoldKey,
677
- appBar: AppBar(
678
- title: const Text('Unity Flutter Demo'),
679
- ),
680
- body: Card(
681
- margin: const EdgeInsets.all(8),
682
- clipBehavior: Clip.antiAlias,
683
- shape: RoundedRectangleBorder(
684
- borderRadius: BorderRadius.circular(20.0),
662
+ return Scaffold(
663
+ appBar: AppBar(
664
+ title: const Text('Unity Flutter Demo'),
665
+ ),
666
+ body: Stack(
667
+ children: <Widget>[
668
+ UnityWidget(
669
+ onUnityCreated: onUnityCreated,
670
+ onUnityMessage: onUnityMessage,
671
+ onUnitySceneLoaded: onUnitySceneLoaded,
685
672
),
686
- child: Stack(
687
- children: <Widget>[
688
- UnityWidget(
689
- onUnityCreated: onUnityCreated,
690
- onUnityMessage: onUnityMessage,
691
- onUnitySceneLoaded: onUnitySceneLoaded,
692
- fullscreen: false,
693
- ),
694
- Positioned(
695
- bottom: 20,
696
- left: 20,
697
- right: 20,
698
- // <You need a PointerInterceptor here on web>
699
- child: Card(
700
- elevation: 10,
701
- child: Column(
702
- children: <Widget>[
703
- const Padding(
704
- padding: EdgeInsets.only(top: 20),
705
- child: Text("Rotation speed:"),
706
- ),
707
- Slider(
708
- onChanged: (value) {
709
- setState(() {
710
- _sliderValue = value;
711
- });
712
- setRotationSpeed(value.toString());
713
- },
714
- value: _sliderValue,
715
- min: 0,
716
- max: 20,
717
- ),
718
- ],
719
- ),
673
+
674
+ // Flutter UI Stacked on top of Unity to demo Flutter -> Unity interactions.
675
+ // On web this requires a PointerInterceptor widget.
676
+ Positioned(
677
+ bottom: 0,
678
+ // <You need a PointerInterceptor here on web>
679
+ child: SafeArea(
680
+ child: Card(
681
+ elevation: 10,
682
+ child: Column(
683
+ children: <Widget>[
684
+ const Padding(
685
+ padding: EdgeInsets.only(top: 20),
686
+ child: Text("Rotation speed:"),
687
+ ),
688
+ Slider(
689
+ onChanged: (value) {
690
+ setState(() {
691
+ _sliderValue = value;
692
+ });
693
+ // Send value to Unity
694
+ setRotationSpeed(value.toString());
695
+ },
696
+ value: _sliderValue,
697
+ min: 0.0,
698
+ max: 1.0,
699
+ ),
700
+ ],
720
701
),
721
702
),
722
- ] ,
703
+ ) ,
723
704
),
724
- ) ,
705
+ ] ,
725
706
),
726
707
);
727
708
}
@@ -735,16 +716,16 @@ class _MyAppState extends State<MyApp> {
735
716
);
736
717
}
737
718
738
- // Communication from Unity to Flutter
739
- void onUnityMessage(message) {
740
- print('Received message from unity: ${message.toString()}');
741
- }
742
-
743
719
// Callback that connects the created controller to the unity controller
744
- void onUnityCreated(controller) {
720
+ void onUnityCreated(UnityWidgetController controller) {
745
721
_unityWidgetController = controller;
746
722
}
747
723
724
+ // Communication from Unity to Flutter
725
+ void onUnityMessage(dynamic message) {
726
+ print('Received message from unity: ${message.toString()}');
727
+ }
728
+
748
729
// Communication from Unity when new scene is loaded to Flutter
749
730
void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
750
731
if (sceneInfo != null) {
@@ -754,7 +735,6 @@ class _MyAppState extends State<MyApp> {
754
735
}
755
736
}
756
737
}
757
-
758
738
```
759
739
760
740
## Props
0 commit comments