Hello, I’m tring to integrate the google sign system in my project. Everythings are working clearly but i cannot load and show user profile image.
When i tried to call the StartCoroutine with a button, it is working, loading and showing the user image.
But when i tried to call StartCoroutine inside from another function, it not working, it is not even enter function inside. Here is my code;
After google sign, calling this function and it is working:
private void GoogleFirebaseAuthOnSignInAction (bool obj) {
Debug.Log("OBJ :"+obj);
StartCoroutine (showImage ());
}
//getting OBJ: True
And here is IEnumarator :
IEnumerator showImage () {
Debug.Log ("show image // not entering here");
UnityWebRequest www = UnityWebRequestTexture.GetTexture (Settings.googleProfileUrl);
yield return www.SendWebRequest ();
if (www.result != UnityWebRequest.Result.Success) {
Debug.Log ("error");
} else {
Texture2D img = DownloadHandlerTexture.GetContent (www);
userImage.sprite = Sprite.Create (img, new Rect (0, 0, 256, 256), Vector2.zero);
}
}
I cannot even get inside the showImage function. Why is this happening?
Does this MonoBehavior stick around long enough to run that coroutine to completion? If you disable this GameObject or change scenes, it won’t continue.
ALSO: the code in showImage() should run all the way up to the first yield before StartCoroutine() even returns, so put another Debug.Log() after the StartCoroutine.
Scene is not changing after google sign process. All process is on the same scene but it is not working anyway. I dont understand why.
Check to make sure that callback is actually called on the main thread.
If it is NOT on the main Unity thread, then you cannot do Unity things with it, such as starting coroutines. You should get a warning if you try.
You may need to set a boolean true somewhere, then observe it from your Update() loop.
My guess is, based on the method name “GoogleFirebaseAuthOnSignInAction”, that this is a callback from a different thread. You can not call StartCoroutine from another thread. It will most likely throw an exception that will terminate that other thread. Unfortunately exceptions on a seperate thread are swallowed silently.
If you need to react to that callback on the main thread, you have to use some sort of threadsafe data transfer. The best solutions for things like that is that you simply wrap the actual API call in a coroutine and simply use a bool flag to signal that the callback has been executed. When the callback is a closure you can simply use a local bool variable.
IEnumerator DoSomeFirebaseAction()
{
bool result = false;
bool hasResult = false;
ExecuteFirebaseRequest((bool aObj)=>{
// callback thread. Only store the result for later use
result = aObj
hasResult = true;
});
// wait until we have a result
while (!hasResult)
yield return null;
Debug.Log("result on main thread:" + result);
// Do any processing here
}
Here is my google sign finished code which is on main treat ;
private void GoogleAuthIsFinished (Task<GoogleSignInUser> task) {
if (task.IsFaulted) {
using (IEnumerator<Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator ()) {
if (enumerator.MoveNext ()) {
GoogleSignIn.SignInException error = (GoogleSignIn.SignInException) enumerator.Current;
Debug.Log ("Google Sign Error: " + error.Status + " " + error.Message);
} else {
Debug.Log ("Error: " + task.Exception);
}
}
} else if (task.IsCanceled) {
Debug.Log ("Google Sign in canceled");
} else {
Debug.Log ("Welcome: " + task.Result.DisplayName);
Settings.googleProfileUrl = task.Result.ImageUrl.ToString ();
Debug.Log(Settings.googleProfileUrl); //**** this is working ********
StartCoroutine (showImage ()); //****************** this is not **********
SignInAction(true);
//SignInFirebaseWithGoogle (task.Result.IdToken);
}
}
IEnumerator showImage () {
Debug.Log ("show image // entered here");
UnityWebRequest www = UnityWebRequestTexture.GetTexture (Settings.googleProfileUrl);
yield return www.SendWebRequest ();
if (www.result != UnityWebRequest.Result.Success) {
Debug.Log ("error");
} else {
Texture2D img = DownloadHandlerTexture.GetContent (www);
userImage.sprite = Sprite.Create (img, new Rect (0, 0, 256, 256), Vector2.zero);
}
}
Hii , is anybody got the solution of this problem . I am getting the exact same error.