Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling a function from flutter to unity3d #31

Closed
bedirhandev opened this issue Aug 13, 2019 · 10 comments
Closed

Calling a function from flutter to unity3d #31

bedirhandev opened this issue Aug 13, 2019 · 10 comments

Comments

@bedirhandev
Copy link

bedirhandev commented Aug 13, 2019

Hi,

Can someone provide an explanation how you can achieve to communicate from flutter to unity3d and back. I have read the documentation and imported the needed files.

At first when I exported it from unity3d to flutter I got this error about NewtonJSON: PrecompiledAssemblyException: Multiple precompiled assemblies with the same name. I then removed the folders Windows and AOT because unity3d found multiple dll's.

After compiling I get the following errror:

Undefined symbols for architecture arm64:
"_onUnityMessage", referenced from:

I try to pause and play unity3d.

@thomas-stockx
Copy link
Collaborator

It should be added to the README that the iOS version currently does not support communication from Unity to Flutter (never did). Flutter to Unity works on both Android and iOS, Unity to Flutter is Android only for now. The easiest way to get your project to compile on iOS is to remove the UnityMessageManager.cs :'(

@bedirhandev
Copy link
Author

@thomas-stockx thanks for clarifying, but for me it also didn't work to communicate from Flutter to Unity. I tested the demo that basically have one button to pause and to resume the game. But unfortunately that did not work.

After that I followed the documentation and imported the needed files, but immediately it prompted an error saying: "PrecompiledAssemblyException: Multiple precompiled assemblies with the same name" about NewtonJson.dll. I then deleted the folders /Standalone and /Windows, and leaving the folder AOT untouched. I exported to Flutter again and it worked without any errors.

After successfully exporting, I then tried to compile it in Xcode but it gives the compilation error:
Undefined symbols for architecture arm64:
"_onUnityMessage", referenced from:

Thank you for your kind help.

@thomas-stockx
Copy link
Collaborator

@bedirhandev The following steps from the documentation are just for Unity to Flutter communcation (and should not be followed for iOS, as it does not work for iOS):

  • Copy UnityMessageManager.cs to your unity project.
  • Copy this folder JsonDotNet to your unity project.
  • Copy link.xml to your unity project.

I think you can resolve your issue by removing UnityMessageManager.cs, the JsonDotNet folder and link.xml.

@bedirhandev
Copy link
Author

bedirhandev commented Aug 14, 2019

@thomas-stockx, ah, thanks! Ok that explains a lot. But in the demo you have one button for pausing and resuming the game. The buttons are not working somehow.

The demo code in Flutter _unityWidgetController.pause(); is not working, because the game is not pausing itself.

Do you know how I can fix this issue, do I have to include include custom code in Unity?

Thank you for your help so far :)

@thomas-stockx
Copy link
Collaborator

Apparently only postMessage has been implemented in the iOS version, which is used for sending message from Flutter to a Unity GameObject:

https://github.com/snowballdigital/flutter-unity-view-widget/blob/master/ios/Classes/FlutterUnityWidgetPlugin.m#L72

@bedirhandev
Copy link
Author

I see could you give an example how to use the method postMessage, pseudo code would help.
After that I will try it out and close this issue as solved.

Thanks.

@juicycleff
Copy link
Owner

@thomas-stockx could we replace the current example with the new one you did in the gif

@juicycleff
Copy link
Owner

Or maybe I make a new one and an updated video tutorial

@thomas-stockx
Copy link
Collaborator

thomas-stockx commented Aug 16, 2019

Hi @bedirhandev,

From your Flutter code, you can use postMessage as follows:

// in Flutter
void unitySetVideoUrl(String url) {
    _unityWidgetController.postMessage(
      'AR Session Origin',
      'SetVideoUrl',
      url,
    );
  }

This will use Unity's message handling system to send a message to a GameObject in your scene, in this case a GameObject called "AR Session Origin", and it will execute a method called "SetVideoUrl", with a string as parameter. (If you want more parameters, I'd recommend using a JSON as string and parsing that in your Unity code.)

To receive these method calls in Unity, you need to implement IEventSystemHandler on a Monobehaviour which is attached to your GameObject, and then add a public method which you want to execute from Flutter:

// make sure your Monobehaviour implements IEventSystemHandler
// add this script to the GameObject you reference in the Flutter code
public class VideoHandler : Monobehaviour, IEventSystemHandler
{
    // add the method you want to call from Flutter
    public void SetVideoUrl(String message)
    {
        Debug.Log("Received url from flutter: " + message);
        // Do something with it
    }
}

@bedirhandev
Copy link
Author

Thanks @juicycleff and @thomas-stockx it now works I have tested it on a iOS device. The communication works from Flutter to Unity, but not the other way. Hopefully, this will be fixed in the near future, but for now I can continue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants