// // Copyright (C) 2014 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #if UNITY_ANDROID namespace GooglePlayGames { using System; using System.Collections; using GooglePlayGames.OurUtils; using UnityEngine; #if UNITY_2017_2_OR_NEWER using UnityEngine.Networking; #endif using UnityEngine.SocialPlatforms; /// /// Represents a Google Play Games user profile. Implements the Unity's IUserProfile /// interface and is used as a base class for . /// public class PlayGamesUserProfile : IUserProfile { /// /// The user's display name. /// private string mDisplayName; /// /// The user's unique player ID. /// private string mPlayerId; /// /// The URL for the user's avatar image. /// private string mAvatarUrl; /// /// A flag indicating if this user is a friend of the local user. /// private bool mIsFriend; /// /// A flag to prevent multiple concurrent image loading coroutines. /// private volatile bool mImageLoading = false; /// /// The cached user avatar image. /// private Texture2D mImage; /// /// Initializes a new instance of the class. /// /// The user's display name. /// The user's player ID. /// The URL of the user's avatar. internal PlayGamesUserProfile(string displayName, string playerId, string avatarUrl) { mDisplayName = displayName; mPlayerId = playerId; setAvatarUrl(avatarUrl); mImageLoading = false; mIsFriend = false; } /// /// Initializes a new instance of the class. /// /// The user's display name. /// The user's player ID. /// The URL of the user's avatar. /// A flag indicating if the user is a friend. internal PlayGamesUserProfile(string displayName, string playerId, string avatarUrl, bool isFriend) { mDisplayName = displayName; mPlayerId = playerId; mAvatarUrl = avatarUrl; mImageLoading = false; mIsFriend = isFriend; } /// /// Resets the user's identity with new information. If the avatar URL has changed, /// the old image is discarded. /// /// The new display name. /// The new player ID. /// The new avatar URL. protected void ResetIdentity(string displayName, string playerId, string avatarUrl) { mDisplayName = displayName; mPlayerId = playerId; mIsFriend = false; if (mAvatarUrl != avatarUrl) { mImage = null; setAvatarUrl(avatarUrl); } mImageLoading = false; } #region IUserProfile implementation /// /// Gets the user's display name. /// /// The name of the user. public string userName { get { return mDisplayName; } } /// /// Gets the user's unique player ID. /// /// The player ID. public string id { get { return mPlayerId; } } /// /// Gets the user's game-specific identifier. In this implementation, it is the same as the player ID. /// public string gameId { get { return mPlayerId; } } /// /// Gets a value indicating whether this user is a friend of the local user. /// /// true if this user is a friend; otherwise, false. public bool isFriend { get { return mIsFriend; } } /// /// Gets the user's current state. In this implementation, it always returns 'Online'. /// public UserState state { get { return UserState.Online; } } /// /// Gets the user's avatar image as a . /// The image is loaded asynchronously. Returns null until the image has been loaded. /// /// The user's avatar image. public Texture2D image { get { if (!mImageLoading && mImage == null && !string.IsNullOrEmpty(AvatarURL)) { OurUtils.Logger.d("Starting to load image: " + AvatarURL); mImageLoading = true; PlayGamesHelperObject.RunCoroutine(LoadImage()); } return mImage; } } #endregion /// /// Gets the URL of the user's avatar. /// public string AvatarURL { get { return mAvatarUrl; } } /// /// An enumerator that asynchronously loads the user's avatar image from the . /// /// An to be used with a coroutine. internal IEnumerator LoadImage() { // The URL can be null if the user does not have an avatar configured. if (!string.IsNullOrEmpty(AvatarURL)) { #if UNITY_2017_2_OR_NEWER UnityWebRequest www = UnityWebRequestTexture.GetTexture(AvatarURL); www.SendWebRequest(); #else WWW www = new WWW(AvatarURL); #endif while (!www.isDone) { yield return null; } if (www.error == null) { #if UNITY_2017_2_OR_NEWER this.mImage = DownloadHandlerTexture.GetContent(www); #else this.mImage = www.texture; #endif } else { mImage = Texture2D.blackTexture; OurUtils.Logger.e("Error downloading image: " + www.error); } mImageLoading = false; } else { OurUtils.Logger.e("No URL found."); mImage = Texture2D.blackTexture; mImageLoading = false; } } /// /// Determines whether the specified is equal to the current . /// Equality is based on the player ID. /// /// The to compare with the current . /// true if the specified object is equal to the current object; otherwise, false. public override bool Equals(object obj) { if (obj == null) { return false; } if (ReferenceEquals(this, obj)) { return true; } PlayGamesUserProfile other = obj as PlayGamesUserProfile; if (other == null) { return false; } return StringComparer.Ordinal.Equals(mPlayerId, other.mPlayerId); } /// /// Serves as a hash function for a object. /// /// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table. public override int GetHashCode() { return typeof(PlayGamesUserProfile).GetHashCode() ^ mPlayerId.GetHashCode(); } /// /// Returns a that represents the current . /// /// A string representation of the object. public override string ToString() { return string.Format("[Player: '{0}' (id {1})]", mDisplayName, mPlayerId); } /// /// Sets the avatar URL, ensuring it uses HTTPS. /// /// The avatar URL to set. private void setAvatarUrl(string avatarUrl) { mAvatarUrl = avatarUrl; if (!string.IsNullOrEmpty(mAvatarUrl) && !mAvatarUrl.StartsWith("https") && mAvatarUrl.StartsWith("http")) { mAvatarUrl = mAvatarUrl.Insert(4, "s"); } } } } #endif