learntoflutter / flutter_embed_unity

17 stars 3 forks source link

Unable to receive Unity messages. #19

Open kgh4770 opened 3 months ago

kgh4770 commented 3 months ago

Is there an existing issue for this?

Unity version

Common issues

Flutter version

3.22.1

Description

Unity

SendToFlutter.Send("setLoadScene:" + sceneNm);

Flutter EmbedUnity( onMessageFromUnity: (String message) { print('unityMessage : ${message}'); } )

Unable to receive Unity messages.

Minimum reproducible example (MRE)

unity

using UnityEngine;

public class FlutterConnection : MonoBehaviour {

void Awake(){
    Debug.Log("FlutterConnection Awake");
    SendToFlutter.Send("FlutterConnectionInit");
}

public void setLoadScene(string sceneNm) {
    Debug.Log("main setLoadScene sceneNm = " + sceneNm);
    SceneMangerControl.LoadScene(sceneNm);
    SendToFlutter.Send("setLoadScene:" + sceneNm);
}

}

============================ flutter

import 'package:flutter_embed_unity/flutter_embed_unity.dart';

EmbedUnity( onMessageFromUnity: (String message) { print('unity Msg : ${message}'); } ),

What platforms are you seeing the problem on?

Android

Devices

android 14

Anything else?

doesn't exist

jamesncl commented 3 months ago

Thanks for the bug report. However, I am unable to reproduce your issue using the example project. I added this to SendToFlutterSceneLoaded.cs:

    void Awake(){
        Debug.Log("FlutterConnection Awake");
        SendToFlutter.Send("FlutterConnectionInit");
    }

And added this to the example android project main.dart:

EmbedUnity(
    onMessageFromUnity: (String data) {
        print("Received from Unity $data");
        ...
    },
)

Then exported the example Unity project to the example android project flutter_embed_unity_2022_3_android/example, ran it, and received this in console:

I/flutter (19710): Received from Unity FlutterConnectionInit I/flutter (19710): Received from Unity scene_loaded

So, I think there must be something wrong with how you have setup your project. Some ideas to investigate:

kgh4770 commented 3 months ago

Thanks for your reply.

We are still testing.

iOS will return to normal.

The screen returns to normal.

You can send from Flutter to Unity.

From Unity to Flutter Message cannot be sent.

jamesncl commented 3 months ago

Maybe check SendToFlutter.java - if somehow this class method name is changed / obfuscated during your build, message passing will break. However the @Keep annotation should prevent this

kgh4770 commented 3 months ago

===Unity =====

I/Unity (29383): FlutterConnection Awake I/Unity (29383): FlutterConnection:Awake() I/Unity (29383): SendToFlutter:Send(String)

==== Flutter ==== SendToFlutter.java

I/System.out(29383): unityMsg :FlutterConnectionInit <<=== A message arrives

=================================================

SendToFlutter.java

if(methodChannel != null) { System.out.println("unityMsg :"+data); methodChannel.invokeMethod(methodNameSendToFlutter, data); }

====================================

If you look here, the message is delivered normally up to this point.

It doesn't seem to have progressed since then.

Is there anything else I should check?

kgh4770 commented 3 months ago

[√] Flutter (Channel stable, 3.22.1, on Microsoft Windows [Version 10.0.22631.3593], locale ko-KR) • Flutter version 3.22.1 on channel stable at C:\AndroidStudio\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision a14f74ff3a (9 days ago), 2024-05-22 11:08:21 -0500 • Engine revision 55eae6864b • Dart version 3.4.1 • DevTools version 2.34.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at C:\Users\admin\AppData\Local\Android\sdk • Platform android-34, build-tools 34.0.0 • Java binary at: C:\Program Files\Android\Android Studio1\jbr\bin\java • Java version OpenJDK Runtime Environment (build 17.0.10+0--11572160) • All Android licenses accepted.

[√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.10.1) • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community • Visual Studio Community 2022 version 17.10.34928.147 • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2023.3) • Android Studio at C:\Program Files\Android\Android Studio1 • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.10+0--11572160)

[√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22631.3593] • Chrome (web) • chrome • web-javascript • Google Chrome 125.0.6422.113 • Edge (web) • edge • web-javascript • Microsoft Edge 125.0.2535.79

[√] Network resources • All expected network resources are available.

kgh4770 commented 3 months ago

flutter_unity_widget

I used it.

but

flutter_embed_unity

There is no need to add Unity sources to Flutter.

I am very excited about the project.

Thanks for your help.

:)

jamesncl commented 3 months ago

The next place the message arrives should be unity_message_listeners.dart line 27 - try putting a debugging breakpoint there, see if it hits, and then step through

kgh4770 commented 3 months ago

unity_message_listeners.dart line 27

iOS connects normally. But it doesn't work on android

Android System.out.print => unityMsg data : FlutterConnectionInit / methodNameSendToFlutter : sendToFlutter / methodChannel : io.flutter.plugin.common.MethodChannel@1c6029c

Is it correct for the methodChannel to work like this?

jamesncl commented 3 months ago

You should debug and step through unity_message_listeners.dart and see what happens. For example see my comments below:


  Future<dynamic> _methodCallHandler(MethodCall call) async {
    // 1. Does it reach here? What is the content of call.method and call.arguments?
    if (call.method == FlutterEmbedConstants.methodNameSendToFlutter) {
      // 2. Does it reach here?
      final data = call.arguments.toString();
      switch (EmbedUnityPreferences.messageFromUnityListeningBehaviour) {
        // 3. Does it reach here?
        case MessageFromUnityListeningBehaviour.allWidgetsReceiveMessages:
          {
            // 4. There should be at least 1 listener here - what is the value of _listeners.length?
            for (var listener in _listeners) {
              listener.onMessageFromUnity(data);
            }
          }
        case MessageFromUnityListeningBehaviour.onlyMostRecentlyCreatedWidgetReceivesMessages:
          {
            if (_listeners.isNotEmpty) {
              _listeners.last.onMessageFromUnity(data);
            }
          }
      }
    }
  }
Azis202017 commented 3 months ago

Hello same here, i dont get the data from message

jamesncl commented 3 months ago

@kgh4770 @Azis202017 Can you please confirm what version of Unity you are using (including the hotfix version, eg 2023.3.31f), the version of gradle your android project is using (the distributionUrl in grade-wrapper.properties) and the output of flutter doctor

jamesncl commented 3 months ago

I have created a new Flutter project (using flutter 3.22), and followed all the steps in the README using the example Unity project, and messaging appears to work for me. This means there must be something about how you are setting up your project which is causing the issue.

So I will need more information in order to help you. You either need to:

  1. Create a minimal reproducible example of the problem
  2. Follow the debugging steps in my previous comment and let me know the results