From a5d4fae805f97d3f8819070a678b7f57682aaad7 Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Thu, 31 Oct 2024 18:50:32 +0500 Subject: [PATCH] Added Profile Pic Managemnt --- Assets/PipeOut/Scenes/GameScene.unity | 3 +- Assets/PipeOut/Scenes/Loading.unity | 49 +++++++++-------- Assets/Scripts/Dev/LeaderboardUIScreen.cs | 18 ++++++- Assets/Scripts/Dev/PlayFab/PlayFabInit.cs | 34 +++++++++++- Assets/Scripts/Dev/PlayFab/PlayFabManager.cs | 22 +++++++- Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs | 52 +++++++++++++++++++ .../Dev/PlayFab/PlayFabUserData.cs.meta | 11 ++++ Assets/Scripts/GameScene.cs | 12 ++++- Assets/Scripts/HomeScene.cs | 28 ++++++++++ 9 files changed, 202 insertions(+), 27 deletions(-) create mode 100644 Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs create mode 100644 Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs.meta diff --git a/Assets/PipeOut/Scenes/GameScene.unity b/Assets/PipeOut/Scenes/GameScene.unity index 92036818..db8d1481 100644 --- a/Assets/PipeOut/Scenes/GameScene.unity +++ b/Assets/PipeOut/Scenes/GameScene.unity @@ -7676,7 +7676,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: 46.75, y: -8.17} + m_AnchoredPosition: {x: 46.75, y: -8.169998} m_SizeDelta: {x: -149.53, y: -58.42} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1975395084 @@ -13147,6 +13147,7 @@ MonoBehaviour: leaderboardYouPanel: {fileID: 722839593} levelLeaderboardPopup: {fileID: 706426006} levelLeaderboardLoading: {fileID: 1359513525} + levelTopperPic: {fileID: 2077189957} debugHints: 0 --- !u!114 &1790415148045805191 MonoBehaviour: diff --git a/Assets/PipeOut/Scenes/Loading.unity b/Assets/PipeOut/Scenes/Loading.unity index c2896fa7..37491d3e 100644 --- a/Assets/PipeOut/Scenes/Loading.unity +++ b/Assets/PipeOut/Scenes/Loading.unity @@ -465,6 +465,17 @@ PrefabInstance: propertyPath: m_AnchoredPosition.y value: 0 objectReference: {fileID: 0} + - target: {fileID: 5869893064589555812, guid: 31c71d95d8fbeb7479842253d3964958, + type: 3} + propertyPath: m_Enabled + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5869893064589555812, guid: 31c71d95d8fbeb7479842253d3964958, + type: 3} + propertyPath: AchievementTemplate + value: + objectReference: {fileID: 1144969350263548, guid: 52f308551e6b1a347a3a0d7de8089fa2, + type: 3} - target: {fileID: 5869893064832991959, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: m_Name @@ -480,6 +491,11 @@ PrefabInstance: propertyPath: States.Array.size value: 7 objectReference: {fileID: 0} + - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, + type: 3} + propertyPath: DisplayAchievements + value: 0 + objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: AchievementList.Array.size @@ -488,12 +504,12 @@ PrefabInstance: - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: States.Array.data[0].Progress - value: 4 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: States.Array.data[6].Progress - value: 40 + value: 3 objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} @@ -538,17 +554,17 @@ PrefabInstance: - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: States.Array.data[6].LastProgressGoal - value: 45 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: States.Array.data[0].LastProgressUpdate - value: 2 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: States.Array.data[6].LastProgressUpdate - value: 19 + value: 1 objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} @@ -693,7 +709,7 @@ PrefabInstance: - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} propertyPath: AchievementList.Array.data[6].ProgressGoal - value: 45 + value: 5 objectReference: {fileID: 0} - target: {fileID: 5869893064832991960, guid: 31c71d95d8fbeb7479842253d3964958, type: 3} @@ -1188,23 +1204,12 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: playFabInit: {fileID: 0} + avatarImages: + - {fileID: 21300000, guid: ea3cab172bfebad4997f192ba43df8e3, type: 3} + - {fileID: 21300000, guid: fb208f21b2e399f45a7648d064e6fb69, type: 3} + - {fileID: 21300000, guid: 5e7d7e6aa85dfd04aa5ce01cc4f42b5c, type: 3} playFabLeaderboards: {fileID: 0} - myProfile: - AdCampaignAttributions: [] - AvatarUrl: - ContactEmailAddresses: [] - DisplayName: - ExperimentVariants: [] - LinkedAccounts: [] - Locations: [] - Memberships: [] - PlayerId: - PublisherId: - PushNotificationRegistrations: [] - Statistics: [] - Tags: [] - TitleId: - ValuesToDate: [] + playFabUserDataManager: {fileID: 0} --- !u!1 &1341897756 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Dev/LeaderboardUIScreen.cs b/Assets/Scripts/Dev/LeaderboardUIScreen.cs index 97924d95..0692902d 100644 --- a/Assets/Scripts/Dev/LeaderboardUIScreen.cs +++ b/Assets/Scripts/Dev/LeaderboardUIScreen.cs @@ -38,11 +38,19 @@ public class LeaderboardUIScreen : MonoBehaviour private void PopulateLbItem(PlayerLeaderboardEntry lbEntry) { - bool isSelf = lbEntry.Profile.PlayerId == PlayFabManager.Instance.myProfile.PlayerId; + bool isSelf = lbEntry.Profile.PlayerId == PlayFabManager.Instance.playFabUserDataManager.myProfile.PlayerId; LBEntryItem lbItem = Instantiate(isSelf ? lbItemSelfPrefab : lbItemPrefab, content).GetComponent(); lbItem.nameText.text = lbEntry.DisplayName; lbItem.rankText.text = (lbEntry.Position + 1).ToString(); lbItem.scoreText.text = lbEntry.StatValue.ToString(); + PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, (sprite) => + { + lbItem.profilePic.sprite = sprite; + }, + (s) => + { + Debug.Log("Couldnt get pic"); + }); } private void PopulatePedestalItem(PlayerLeaderboardEntry lbEntry) @@ -50,6 +58,14 @@ public class LeaderboardUIScreen : MonoBehaviour LBPedestalItem pedestalItem = _lbPedestalItems[lbEntry.Position]; pedestalItem.nameText.text = lbEntry.DisplayName??lbEntry.PlayFabId; pedestalItem.scoreText.text = lbEntry.StatValue.ToString(); + PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, (sprite) => + { + pedestalItem.profilePic.sprite = sprite; + }, + (s) => + { + Debug.Log("Could'nt get pic"); + }); } private void OnLeaderboardFetchFailure(PlayFabError obj) diff --git a/Assets/Scripts/Dev/PlayFab/PlayFabInit.cs b/Assets/Scripts/Dev/PlayFab/PlayFabInit.cs index de9dc5cd..f92e1d8b 100644 --- a/Assets/Scripts/Dev/PlayFab/PlayFabInit.cs +++ b/Assets/Scripts/Dev/PlayFab/PlayFabInit.cs @@ -43,7 +43,39 @@ public class PlayFabInit : MonoBehaviour { Debug.Log("Login successful!"); GetPlayerProfile(); + GetCustomUserData(); } + + private void GetCustomUserData() + { + GetPlayerProfileWithUserData(); + } + + public void GetPlayerProfileWithUserData() + { + var request = new GetPlayerCombinedInfoRequest + { + PlayFabId = PlayFabSettings.staticPlayer.PlayFabId, // Use the logged-in player's PlayFab ID + InfoRequestParameters = new GetPlayerCombinedInfoRequestParams + { + GetUserData = true // This will include User Data like AvatarID in the response + } + }; + + PlayFabClientAPI.GetPlayerCombinedInfo(request, OnGetCombinedInfoSuccess, OnGetCombinedInfoFailure); + } + + private void OnGetCombinedInfoSuccess(GetPlayerCombinedInfoResult result) + { + var userData = result.InfoResultPayload.UserData; + PlayFabManager.Instance.playFabUserDataManager.myCustomData = userData; + } + + private void OnGetCombinedInfoFailure(PlayFabError error) + { + Debug.LogError("Failed to get combined player info: " + error.GenerateErrorReport()); + } + public void GetPlayerProfile() { var request = new GetPlayerProfileRequest @@ -67,7 +99,7 @@ public class PlayFabInit : MonoBehaviour private void OnGetProfileSuccess(GetPlayerProfileResult obj) { Debug.Log("Player profile fetching successful"); - PlayFabManager.Instance.myProfile = obj.PlayerProfile; + PlayFabManager.Instance.playFabUserDataManager.myProfile = obj.PlayerProfile; } private void OnLoginFailure(PlayFabError error) diff --git a/Assets/Scripts/Dev/PlayFab/PlayFabManager.cs b/Assets/Scripts/Dev/PlayFab/PlayFabManager.cs index a51be47e..7d7605de 100644 --- a/Assets/Scripts/Dev/PlayFab/PlayFabManager.cs +++ b/Assets/Scripts/Dev/PlayFab/PlayFabManager.cs @@ -1,5 +1,7 @@ +using System; using System.Collections; using System.Collections.Generic; +using PlayFab; using PlayFab.ClientModels; using UnityEngine; @@ -7,8 +9,23 @@ public class PlayFabManager : MonoBehaviour { public static PlayFabManager Instance { get; private set; } public PlayFabInit playFabInit; + public List avatarImages; public PlayFabLeaderboards playFabLeaderboards; - public PlayerProfileModel myProfile; + public PlayFabUserData playFabUserDataManager; + private int myAvatarIndex + { + get + { + if (playFabUserDataManager.myCustomData != null && playFabUserDataManager.myCustomData.ContainsKey("AvatarID")) + { + return int.Parse(playFabUserDataManager.myCustomData["AvatarID"].Value); + } + + return 0; + } + } + + public Sprite myDisplayPicture => avatarImages[myAvatarIndex]; private void Awake() { if (Instance != null && Instance != this) @@ -23,6 +40,9 @@ public class PlayFabManager : MonoBehaviour playFabInit = gameObject.AddComponent(); playFabLeaderboards = gameObject.AddComponent(); + playFabUserDataManager = gameObject.AddComponent(); } + + } diff --git a/Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs b/Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs new file mode 100644 index 00000000..830c1d96 --- /dev/null +++ b/Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using PlayFab; +using PlayFab.ClientModels; +using UnityEngine; + +public class PlayFabUserData : MonoBehaviour +{ + + public PlayerProfileModel myProfile; + public Dictionary myCustomData; + + public void GetPlayerUserData(string playfabID, Action> onCallback, Action onFallback) + { + var request = new GetPlayerCombinedInfoRequest + { + PlayFabId = playfabID, // Use the logged-in player's PlayFab ID + InfoRequestParameters = new GetPlayerCombinedInfoRequestParams + { + GetUserData = true // This will include User Data like AvatarID in the response + } + }; + + PlayFabClientAPI.GetPlayerCombinedInfo(request, result => onCallback(result.InfoResultPayload.UserData), result=>onFallback(result.ErrorMessage)); + } + + public void GetPlayerAvatarImage(string playfabID, Action onCallback, Action onFallback) + { + var request = new GetPlayerCombinedInfoRequest + { + PlayFabId = playfabID, // Use the logged-in player's PlayFab ID + InfoRequestParameters = new GetPlayerCombinedInfoRequestParams + { + GetUserData = true // This will include User Data like AvatarID in the response + } + }; + + PlayFabClientAPI.GetPlayerCombinedInfo(request, result => + { + if (result.InfoResultPayload.UserData != null && result.InfoResultPayload.UserData.ContainsKey("AvatarID")) + { + Sprite dp = PlayFabManager.Instance.avatarImages[ + int.Parse(result.InfoResultPayload.UserData["AvatarID"].Value)]; + onCallback(dp); + } + else + onCallback(PlayFabManager.Instance.avatarImages[0]); + }, result=>onFallback(result.ErrorMessage)); + } + +} diff --git a/Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs.meta b/Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs.meta new file mode 100644 index 00000000..4a54ea7b --- /dev/null +++ b/Assets/Scripts/Dev/PlayFab/PlayFabUserData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ee4dea21bf118984281a95b0829061bd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GameScene.cs b/Assets/Scripts/GameScene.cs index 2ef6899c..4d421684 100644 --- a/Assets/Scripts/GameScene.cs +++ b/Assets/Scripts/GameScene.cs @@ -47,6 +47,7 @@ public class GameScene : MonoBehaviour public GameObject leaderboardYouPanel; public Popup levelLeaderboardPopup; public GameObject levelLeaderboardLoading; + [SerializeField] private Image levelTopperPic; public static GameScene instance; @@ -98,6 +99,7 @@ public class GameScene : MonoBehaviour PlayFabManager.Instance.playFabLeaderboards.GetLeaderboardByKey(statisticName, OnLBSuccess, OnLBFail); } + private void OnLBSuccess(List obj) { if (obj.Count > 0) @@ -105,8 +107,16 @@ public class GameScene : MonoBehaviour leaderboardNameText.text = obj[0].DisplayName; float time = obj[0].StatValue / 1000f; timeText.text = time + " secs"; - leaderboardYouPanel.SetActive(obj[0].PlayFabId == PlayFabManager.Instance.myProfile.PlayerId); + leaderboardYouPanel.SetActive(obj[0].PlayFabId == PlayFabManager.Instance.playFabUserDataManager.myProfile.PlayerId); levelLeaderboardLoading.SetActive(false); + PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(obj[0].PlayFabId, sprite => + { + levelTopperPic.sprite = sprite; + }, + (s) => + { + Debug.Log("Couldnt get pic"); + }); } } diff --git a/Assets/Scripts/HomeScene.cs b/Assets/Scripts/HomeScene.cs index ec86270a..2e62a26e 100644 --- a/Assets/Scripts/HomeScene.cs +++ b/Assets/Scripts/HomeScene.cs @@ -15,6 +15,8 @@ U should buy the asset from home store if u use it in your project! using MS; using System.Collections.Generic; using DG.Tweening; +using PlayFab; +using PlayFab.ClientModels; using PlayFab.MultiplayerModels; using TMPro; using UnityEngine; @@ -144,8 +146,34 @@ public class HomeScene : MonoBehaviour PlayerPrefs.SetInt(GameConstants.AvatarSelectedCheckKey, 1); PlayerPrefs.SetInt(GameConstants.AvatarSelectedIndex, AvatarID); AvatarSelectedSprite = AvatarSprites[AvatarID]; + SetAvatarID(AvatarID); //NameEnterPanel.Close(); } + + public void SetAvatarID(int avatarID) + { + var request = new UpdateUserDataRequest + { + Data = new Dictionary + { + { "AvatarID", avatarID.ToString() } // Convert int to string for storage + } + }; + + PlayFabClientAPI.UpdateUserData(request, OnDataUpdateSuccess, OnDataUpdateFailure); + } + + private void OnDataUpdateSuccess(UpdateUserDataResult result) + { + Debug.Log("Avatar ID updated successfully."); + } + + private void OnDataUpdateFailure(PlayFabError error) + { + Debug.LogError("Failed to update Avatar ID: " + error.GenerateErrorReport()); + } + + private void SetupSettingToggle() {