Skip to content

Commit 9c50b9d

Browse files
committed
Replace broken readme image with new communicating instructions.
1 parent fcbcccc commit 9c50b9d

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,25 +564,57 @@ If you computer does not have an ARM processor, like most computers running on I
564564

565565
## Communicating
566566

567-
### Flutter-Unity
567+
### Flutter -> Unity
568568

569569
1. On a `UnityWidget` widget, get the `UnityWidgetController` received by the `onUnityCreated` callback.
570570

571+
```dart
572+
// Assign such a function to onUnityCreated of UnityWidget.
573+
void _onUnityCreated(UnityWidgetController controller) {
574+
_unityWidgetController = controller;
575+
}
576+
```
577+
571578
2. Use the method `postMessage` to send a string, using the GameObject name and the name of a behaviour method that should be called.
572579

573-
### Unity-Flutter
574580

575-
1. Select the GameObject that should execute the communication and go to **Inspector > Add Component > Unity Message Manager**.
581+
```dart
582+
// Snippet of postMessage usage in the example project.
583+
_unityWidgetController?.postMessage(
584+
'Cube', // GameObject name
585+
'SetRotationSpeed', // function in attached C# script
586+
speed, // Function parameter (string)
587+
);
588+
```
576589

577-
<img src="https://i.stack.imgur.com/1gSOy.png" width="400" />
590+
### Unity -> Flutter
578591

579-
2. Create a new `MonoBehaviour` subclass and add to the same GameObject as a script.
592+
1. Add a `UnityMessageManager` component to you scene.
593+
- Either click `Add Component` -> `UnityMessageManager` on a GameObject in you scene.
594+
- Or call AddComponent in a MonoBehaviour script.
595+
`gameObject.AddComponent<UnityMessageManager>();`
580596

581-
3. On this new behaviour, call `GetComponent<UnityMessageManager>()` to get a `UnityMessageManager`.
597+
2. `UnityMessageManager` only needs to be added once. It is a Singleton which can be called from any other script in the scene. Just use `UnityMessageManager.Instance`.
582598

583-
4. Use the method `SendMessageToFlutter` to send a string. Receive this message using the `onUnityMessage` callback of a `UnityWidget`.
584599

600+
3. Use the method `SendMessageToFlutter` to send a string. Receive this message using the `onUnityMessage` callback of a `UnityWidget`.
585601

602+
```C#
603+
// Send a basic string to Flutter
604+
UnityMessageManager.Instance.SendMessageToFlutter("Hello there!");
605+
```
606+
4. If you want to send multiple parameters or objects, you will have to use a JSON string.
607+
```C#
608+
// This is a random object serialized to JSON using Json.net.
609+
JObject o = JObject.FromObject(new
610+
{
611+
id = 1,
612+
name = "Object 1",
613+
whatever = 12
614+
});
615+
UnityMessageManager.Instance.SendMessageToFlutter(o.ToString());
616+
// You will need to deserialize this in Flutter after receiving it in OnUnityMessage.
617+
```
586618

587619

588620
## Examples

0 commit comments

Comments
 (0)