Closed jakeeind closed 4 years ago
Probably controller.Start is called before IonicComms.OnMessageReceivedFromIonic is called. Try adding a 1-frame delay to controller.Start like this:
IEnumerator Start()
{
yield return null;
unity_text.text = ionic.MessageFromIonic;
}
Alternatively, you can give IonicComms a negative order in Edit-Project Settings-Script Execution Order.
still can't pass the message :(
IonicComms.cs
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class IonicComms: MonoBehaviour
{
private static IonicComms Instance = null;
#if !UNITY_EDITOR && UNITY_IOS
[System.Runtime.InteropServices.DllImport( "__Internal" )]
private static extern void uHideUnity();
[System.Runtime.InteropServices.DllImport( "__Internal" )]
private static extern void uNotifyUnityReady();
[System.Runtime.InteropServices.DllImport( "__Internal" )]
private static extern void uSendMessageToIonic( string message );
[System.Runtime.InteropServices.DllImport( "__Internal" )]
private static extern void uSendResultToIonic( string result );
#endif
public Text ionic_text;
public string MessageFromIonic { get; private set; }
[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.AfterSceneLoad )]
static void Init()
{
if ( Instance == null )
{
Instance = new GameObject( "IonicComms" ).AddComponent<IonicComms>();
DontDestroyOnLoad( Instance.gameObject );
}
}
void Awake()
{
if( Instance == null )
{
Instance = this;
gameObject.name = "IonicComms";
DontDestroyOnLoad( gameObject );
}
else if( this != Instance )
Destroy( this );
}
void Start()
{
ionic_text.text = "hello ionic";
MessageFromIonic = "no message from ionic";
if ( Instance != this )
return;
#if !UNITY_EDITOR && UNITY_ANDROID
using( AndroidJavaClass activityClass = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
using( AndroidJavaObject activity = activityClass.GetStatic<AndroidJavaObject>( "currentActivity" ) )
{
string data = activity.Get<string>( "commStr" );
OnMessageReceivedFromIonic( data );
}
#elif !UNITY_EDITOR && UNITY_IOS
uNotifyUnityReady();
#endif
}
private void OnMessageReceivedFromIonic( string message )
{
if (string.IsNullOrEmpty(message))
message = "no message from ionic";
// Process message here
MessageFromIonic = message;
}
public static void SendMessageToIonic( string message )
{
if( message == null || message.Length == 0 )
return;
#if !UNITY_EDITOR && UNITY_ANDROID
using( AndroidJavaClass activityClass = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
using( AndroidJavaObject activity = activityClass.GetStatic<AndroidJavaObject>( "currentActivity" ) )
{
activity.Call( "sendMessageToIonic", message );
}
#elif !UNITY_EDITOR && UNITY_IOS
uSendMessageToIonic( message );
#endif
}
public static void FinishActivity( string returnMessage = null )
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
return;
#endif
if (returnMessage == null)
returnMessage = "";
if( Instance == null )
{
Application.Quit();
}
else
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
using( AndroidJavaClass activityClass = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
using( AndroidJavaObject activity = activityClass.GetStatic<AndroidJavaObject>( "currentActivity" ) )
{
activity.Set<string>( "commStr", returnMessage );
activity.Call( "closeApp" );
}
#elif UNITY_IOS
uSendResultToIonic( returnMessage );
uHideUnity();
#else
Application.Quit();
#endif
}
}
}
controller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class controller: MonoBehaviour
{
IonicComms ionic = new IonicComms();
public Text unity_text;
// Start is called before the first frame update
IEnumerator Start()
{
yield return null;
unity_text.text = ionic.MessageFromIonic;
}
// Update is called once per frame
void Update()
{
unity_text.text = ionic.MessageFromIonic;
}
}
and I try an alternative way that you suggest
still not work
Simply make MessageFromIonic public static and access it like this: unity_text.text = IonicComms.MessageFromIonic;
. Don't create a new IonicComms instance with new IonicComms()
.
I can do it now! Thank you so much
I try to passing string message to another class like this
IonicComms.cs
controller.cs
but the message still not show in UI