Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Google Sign In : How to get the profile picture and set it as a source image?

Discussion in 'Scripting' started by ndmrzk, Feb 19, 2020.

  1. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    35
    Hello. I am using Unity 2019.3.0f3 and I integrated the Google Sign In Unity into my project. I've already successfully used for logging in and getting users name. But i also want to get the profile picture. I tried :

    Code (CSharp):
    1. Uri profilepic;
    2.  
    3. profilepic = task.Result.ImageUrl;
    4.  
    5. UnityWebRequest uwr = new UnityWebRequest(profilepic);
    6. yield return uwr.SendWebRequest();
    7.  
    8. Texture2D tex = new Texture2D(1, 1);
    9. tex = ((DownloadHandlerTexture)uwr.downloadHandler).texture;
    10. Sprite image = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
    11.  
    12. ProfilePicture.GetComponent<Image>().sprite = image;
    The uwr is in an IEnumerator. And i get this in the console

    Code (CSharp):
    1. Null ReferenceException: Object reference not set to an instance of an object  at UnityEngine.Networking.UnityWebRequest.set uri
    What am i doing wrong?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    u can post full code?
     
  3. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    35
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Threading.Tasks;
    6. using Google;
    7. using TMPro;
    8. using UnityEngine;
    9. using UnityEngine.Networking;
    10. using UnityEngine.UI;
    11.  
    12. public class LoginGoogle : MonoBehaviour
    13. {
    14.  
    15.     //public Text statusText;
    16.  
    17.     public TMP_Text FirstName, DisplayName, FullName;//, EmailAddress, UserID;
    18.  
    19.     public string webClientId = "<your client id here>";
    20.  
    21.     private GoogleSignInConfiguration configuration;
    22.  
    23.     public GameObject LoginPage, HomeScreen, ProfilePicture, MenuBar;
    24.  
    25.     string dpname, email, userid;
    26.  
    27.     Uri profilepic;
    28.  
    29.     // Defer the configuration creation until Awake so the web Client ID
    30.     // Can be set via the property inspector in the Editor.
    31.     void Awake()
    32.     {
    33.         configuration = new GoogleSignInConfiguration
    34.         {
    35.             WebClientId = webClientId,
    36.             RequestIdToken = true,
    37.             RequestEmail = true
    38.         };
    39.     }
    40.  
    41.     public void Login()
    42.     {
    43.         GoogleSignIn.Configuration = configuration;
    44.         GoogleSignIn.Configuration.UseGameSignIn = false;
    45.         GoogleSignIn.Configuration.RequestIdToken = true;
    46.         GoogleSignIn.Configuration.RequestEmail = true;
    47.  
    48.         AddStatusText("Calling SignIn");
    49.  
    50.         GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
    51.             OnAuthenticationFinished);
    52.     }
    53.  
    54.     public void Logout()
    55.     {
    56.         AddStatusText("Calling SignOut");
    57.         GoogleSignIn.DefaultInstance.SignOut();
    58.  
    59.         StartCoroutine(LeaveProfilePage());
    60.     }
    61.  
    62.     public void OnDisconnect()
    63.     {
    64.         AddStatusText("Calling Disconnect");
    65.         GoogleSignIn.DefaultInstance.Disconnect();
    66.     }
    67.  
    68.     internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
    69.     {
    70.         if (task.IsFaulted)
    71.         {
    72.             using (IEnumerator<System.Exception> enumerator =
    73.                     task.Exception.InnerExceptions.GetEnumerator())
    74.             {
    75.                 if (enumerator.MoveNext())
    76.                 {
    77.                     GoogleSignIn.SignInException error =
    78.                             (GoogleSignIn.SignInException)enumerator.Current;
    79.                     AddStatusText("Got Error: " + error.Status + " " + error.Message);
    80.                 }
    81.                 else
    82.                 {
    83.                     AddStatusText("Got Unexpected Exception?!?" + task.Exception);
    84.                 }
    85.             }
    86.         }
    87.         else if (task.IsCanceled)
    88.         {
    89.             AddStatusText("Canceled");
    90.         }
    91.         else
    92.         {
    93.             AddStatusText("Welcome: " + task.Result.DisplayName + "!");
    94.  
    95.             dpname = task.Result.DisplayName;
    96.             email = task.Result.Email;
    97.             userid = task.Result.UserId;
    98.             profilepic = task.Result.ImageUrl;
    99.  
    100.             Debug.Log(dpname);
    101.             Debug.Log(email);
    102.             Debug.Log(userid);
    103.         }
    104.     }
    105.  
    106.     public void OnSignInSilently()
    107.     {
    108.         GoogleSignIn.Configuration = configuration;
    109.         GoogleSignIn.Configuration.UseGameSignIn = false;
    110.         GoogleSignIn.Configuration.RequestIdToken = true;
    111.         AddStatusText("Calling SignIn Silently");
    112.  
    113.         GoogleSignIn.DefaultInstance.SignInSilently()
    114.                 .ContinueWith(OnAuthenticationFinished);
    115.     }
    116.  
    117.     public void OnGamesSignIn()
    118.     {
    119.         GoogleSignIn.Configuration = configuration;
    120.         GoogleSignIn.Configuration.UseGameSignIn = true;
    121.         GoogleSignIn.Configuration.RequestIdToken = false;
    122.  
    123.         AddStatusText("Calling Games SignIn");
    124.  
    125.         GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
    126.             OnAuthenticationFinished);
    127.     }
    128.  
    129.     private List<string> messages = new List<string>();
    130.  
    131.     void AddStatusText(string text)
    132.     {
    133.         if (messages.Count == 5)
    134.         {
    135.             messages.RemoveAt(0);
    136.         }
    137.         messages.Add(text);
    138.         string txt = "";
    139.         foreach (string s in messages)
    140.         {
    141.             txt += "\n" + s;
    142.         }
    143.  
    144.         StartCoroutine(ChangesPage());
    145.     }
    146.  
    147.     IEnumerator ChangesPage()
    148.     {
    149.         yield return new WaitForEndOfFrame();
    150.  
    151.         //LoginPage.SetActive(false);
    152.         HomeScreen.SetActive(true);
    153.         MenuBar.SetActive(true);
    154.  
    155.         if (dpname != null)
    156.         {
    157.             string[] thename = dpname.Split(' ');
    158.             for (int i = 0; i < thename.Length; i++)
    159.             {
    160.                 string firstname = thename[0];
    161.                 string lastname = thename[1];
    162.  
    163.                 FirstName.text = firstname;
    164.                 DisplayName.text = firstname + " " + lastname;
    165.                 FullName.text = firstname + " " + lastname;
    166.             }
    167.         }
    168.         else
    169.         {
    170.             FirstName.text = "Your Name";
    171.             DisplayName.text = "Your Name";
    172.         }
    173.  
    174.         UnityWebRequest uwr = new UnityWebRequest(profilepic);
    175.         yield return uwr.SendWebRequest();
    176.  
    177.         Texture2D tex = new Texture2D(1, 1);
    178.         tex = ((DownloadHandlerTexture)uwr.downloadHandler).texture;
    179.         Sprite image = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
    180.  
    181.         ProfilePicture.GetComponent<Image>().sprite = image;
    182.  
    183.         /*if (email != null)
    184.         {
    185.             EmailAddress.text = email;
    186.         }
    187.         else
    188.         {
    189.             EmailAddress.text = "Your email address";
    190.         }
    191.  
    192.         UserID.text = userid;*/
    193.     }
    194.  
    195.     IEnumerator LeaveProfilePage()
    196.     {
    197.         yield return new WaitForEndOfFrame();
    198.        
    199.         //ProfilePage.SetActive(false);
    200.  
    201.         DisplayName.text = "";
    202.         //EmailAddress.text = "";
    203.         //UserID.text = "";
    204.     }
     
  4. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    35
    Nvm. im using RawImage instead of Image so i changed the uwr like :

    Code (CSharp):
    1. using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(profilepic))
    2. {
    3.     yield return uwr.SendWebRequest();
    4.  
    5.     if (uwr.isNetworkError || uwr.isHttpError)
    6.     {
    7.         Debug.Log(uwr.error);
    8.     }
    9.     else
    10.     {
    11.         Texture2D tex = new Texture2D(2, 2);
    12.          tex = DownloadHandlerTexture.GetContent(uwr);
    13.  
    14.          ProfilePicture.GetComponent<RawImage>().texture = tex;
    15.     }
    16. }
     
  5. Mohammad_Hunain

    Mohammad_Hunain

    Joined:
    Aug 6, 2019
    Posts:
    3
    hi , how you add using Google; in unity