Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 93cbb6d

Browse files
Add example.md for the example tab on pub.dev.
1 parent 0abfd55 commit 93cbb6d

File tree

2 files changed

+180
-83
lines changed

2 files changed

+180
-83
lines changed

‎README.md

Lines changed: 63 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -608,34 +608,23 @@ class UnityDemoScreen extends StatefulWidget {
608608
}
609609
610610
class _UnityDemoScreenState extends State<UnityDemoScreen> {
611-
static final GlobalKey<ScaffoldState> _scaffoldKey =
612-
GlobalKey<ScaffoldState>();
611+
613612
UnityWidgetController? _unityWidgetController;
614613
615614
@override
616615
Widget build(BuildContext context) {
617616
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,
632621
),
633622
),
634623
);
635624
}
636625
637626
// Callback that connects the created controller to the unity controller
638-
void onUnityCreated(controller) {
627+
void onUnityCreated(UnityWidgetController controller) {
639628
_unityWidgetController = controller;
640629
}
641630
}
@@ -649,79 +638,71 @@ class _UnityDemoScreenState extends State<UnityDemoScreen> {
649638
import 'package:flutter/material.dart';
650639
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
651640
652-
void main() => runApp(const MyApp());
641+
void main() {
642+
runApp(
643+
const MaterialApp(
644+
home: UnityDemoScreen(),
645+
),
646+
);
647+
}
653648
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);
656651
657652
@override
658-
State<MyApp> createState() => _MyAppState();
653+
State<UnityDemoScreen> createState() => _UnityDemoScreenState();
659654
}
660655
661-
class _MyAppState extends State<MyApp> {
662-
static final GlobalKey<ScaffoldState> _scaffoldKey =
663-
GlobalKey<ScaffoldState>();
656+
class _UnityDemoScreenState extends State<UnityDemoScreen> {
664657
UnityWidgetController? _unityWidgetController;
665658
double _sliderValue = 0.0;
666659
667-
@override
668-
void initState() {
669-
super.initState();
670-
}
671-
672660
@override
673661
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,
685672
),
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+
],
720701
),
721702
),
722-
],
703+
),
723704
),
724-
),
705+
],
725706
),
726707
);
727708
}
@@ -735,16 +716,16 @@ class _MyAppState extends State<MyApp> {
735716
);
736717
}
737718
738-
// Communication from Unity to Flutter
739-
void onUnityMessage(message) {
740-
print('Received message from unity: ${message.toString()}');
741-
}
742-
743719
// Callback that connects the created controller to the unity controller
744-
void onUnityCreated(controller) {
720+
void onUnityCreated(UnityWidgetController controller) {
745721
_unityWidgetController = controller;
746722
}
747723
724+
// Communication from Unity to Flutter
725+
void onUnityMessage(dynamic message) {
726+
print('Received message from unity: ${message.toString()}');
727+
}
728+
748729
// Communication from Unity when new scene is loaded to Flutter
749730
void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
750731
if (sceneInfo != null) {
@@ -754,7 +735,6 @@ class _MyAppState extends State<MyApp> {
754735
}
755736
}
756737
}
757-
758738
```
759739

760740
## Props

‎example/example.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Example
2+
3+
4+
5+
This example **requires** you to first follow the Readme setup and make an export in Unity.
6+
An example Unity project can be found in `example/unity/DemoApp`.
7+
8+
For Android and iOS we recommended to run this on a real device. Emulator support is very limited.
9+
10+
## Flutter
11+
12+
13+
```dart
14+
import 'package:flutter/material.dart';
15+
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
16+
17+
void main() {
18+
runApp(
19+
const MaterialApp(
20+
home: UnityDemoScreen(),
21+
),
22+
);
23+
}
24+
25+
class UnityDemoScreen extends StatefulWidget {
26+
const UnityDemoScreen({Key? key}) : super(key: key);
27+
28+
@override
29+
State<UnityDemoScreen> createState() => _UnityDemoScreenState();
30+
}
31+
32+
class _UnityDemoScreenState extends State<UnityDemoScreen> {
33+
UnityWidgetController? _unityWidgetController;
34+
double _sliderValue = 0.0;
35+
36+
@override
37+
Widget build(BuildContext context) {
38+
return Scaffold(
39+
appBar: AppBar(
40+
title: const Text('Unity Flutter Demo'),
41+
),
42+
body: Stack(
43+
children: <Widget>[
44+
// This plugin's widget.
45+
UnityWidget(
46+
onUnityCreated: onUnityCreated,
47+
onUnityMessage: onUnityMessage,
48+
onUnitySceneLoaded: onUnitySceneLoaded,
49+
),
50+
51+
// Flutter UI Stacked on top of Unity to demo Flutter -> Unity interactions.
52+
// On web this requires a PointerInterceptor widget.
53+
Positioned(
54+
bottom: 0,
55+
// <You need a PointerInterceptor here on web>
56+
child: SafeArea(
57+
child: Card(
58+
elevation: 10,
59+
child: Column(
60+
children: <Widget>[
61+
const Padding(
62+
padding: EdgeInsets.only(top: 20),
63+
child: Text("Rotation speed:"),
64+
),
65+
Slider(
66+
onChanged: (value) {
67+
setState(() {
68+
_sliderValue = value;
69+
});
70+
// Send value to Unity
71+
setRotationSpeed(value.toString());
72+
},
73+
value: _sliderValue,
74+
min: 0.0,
75+
max: 1.0,
76+
),
77+
],
78+
),
79+
),
80+
),
81+
),
82+
],
83+
),
84+
);
85+
}
86+
87+
// Callback that connects the created controller to the unity controller
88+
void onUnityCreated(UnityWidgetController controller) {
89+
_unityWidgetController = controller;
90+
}
91+
92+
// Communcation from Flutter to Unity
93+
void setRotationSpeed(String speed) {
94+
// Set the rotation speed of a cube in our example Unity project.
95+
_unityWidgetController?.postMessage(
96+
'Cube',
97+
'SetRotationSpeed',
98+
speed,
99+
);
100+
}
101+
102+
// Communication from Unity to Flutter
103+
void onUnityMessage(dynamic message) {
104+
print('Received message from unity: ${message.toString()}');
105+
}
106+
107+
// Communication from Unity when new scene is loaded to Flutter
108+
void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
109+
if (sceneInfo != null) {
110+
print('Received scene loaded from unity: ${sceneInfo.name}');
111+
print(
112+
'Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
113+
}
114+
}
115+
}
116+
117+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /