flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

In IOS, the onUnityMessage cannot be executed when entering the page for the second times

Open sohubill opened this issue 4 years ago • 10 comments

Such as the title

sohubill avatar Jan 27 '21 11:01 sohubill

The temporary solution is as follows:

Modify plugin in controller.dart File. In the first line of the method:_connectStreams (int unityId) add Platform.isIOS?0 :unityId; And import ' dart:io ';

sohubill avatar Jan 27 '21 11:01 sohubill

I'm having exactly this issue, I tried to implement your temporary solution but I couldn't implemented it, could you post more clear instructions on how do this? thanks in advance

Update: I think I resolve the issue What i did exactly using your instructions is:

  1. On file flutter_unity_widget.dart I add the import 'dart:io'; on the top part
  2. then on controller.dart I add on method _connectStreams the following line starting the method unityId = Platform.isIOS?0:unityId;

Darkmax avatar Jan 27 '21 19:01 Darkmax

The reason for this problem is that each time you enter the unity page, you will return a new unityid. However, the onunitymessage with unityid = 0 still works on the IOS side.

If you are Android studio, you can find the plug-in source file by pressing the left mouse button of Ctrl + unitywidgetcontroller controller.dart .then on controller.dart I add on method _connectStreams the following line starting the method unityId = Platform.isIOS?0:unityId;

sohubill avatar Jan 28 '21 01:01 sohubill

I Will make a release with a fix for this soon, with port to kotlin and swift

juicycleff avatar Jan 28 '21 16:01 juicycleff

There is a bug in objc code, i forked a native project to resolve it.

chenenyu avatar Feb 01 '21 07:02 chenenyu

even with the fix from @Darkmax, it still doesn't work for second time. Any idea?

dantetwc avatar Mar 11 '21 10:03 dantetwc

Ok, just figured it out it. In stead of always 0 for ios, try 1 for unityID

dantetwc avatar Mar 12 '21 03:03 dantetwc

Ok, just figured it out it. In stead of always 0 for ios, try 1 for unityID

I have the same issue as yours, "In stead of always 0 for ios, try 1 for unityID" as you say, do you mean change the unity ID every time when enter the same page?

LuoLuoDev avatar Apr 21 '22 07:04 LuoLuoDev

@dantetwc @juicycleff I have found the reason about the case you met. See https://github.com/juicycleff/flutter-unity-view-widget/blob/master/ios/Classes/FLTUnityWidgetController.swift You can see the globalChannel always be the newest channel, and unity instance only sends message to globalChannel, so when you come back to the old page with unity widget controller, globalChannel still be the latest created channel and not the old page channel. I solve the issue by re-foucsing the old channel with unityId when the old page appears.

LuoLuoDev avatar Apr 21 '22 10:04 LuoLuoDev

I didn't see this error in the current version, but @juicycleff @Ahmadre can you check if it's fixed?

dawiddszewczyk avatar Jul 18 '22 09:07 dawiddszewczyk