-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40c8c01
commit a13ccf4
Showing
397 changed files
with
81,537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"version": "1.0", | ||
"components": [ | ||
"Microsoft.VisualStudio.Workload.ManagedGame" | ||
] | ||
} |
194 changes: 194 additions & 0 deletions
194
Samples/Unity/PlayFabEconomyV2/Assets/AndroidIAPExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
using PlayFab; | ||
using PlayFab.ClientModels; | ||
using PlayFab.EconomyModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using UnityEditor.PackageManager.Requests; | ||
using UnityEngine; | ||
using UnityEngine.Purchasing; | ||
|
||
public class AndroidIAPExample : MonoBehaviour, IStoreListener | ||
{ | ||
/** | ||
* True if the Store Controller, extensions, and Catalog are set. | ||
*/ | ||
public bool IsInitialized | ||
{ | ||
get | ||
{ | ||
return m_StoreController != null && Catalog != null; | ||
} | ||
} | ||
|
||
/** | ||
* Returns false as this is just sample code. | ||
* | ||
* @todo Implement this functionality for your game. | ||
*/ | ||
public bool UserHasExistingSave | ||
{ | ||
get | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
// Items list, configurable via inspector. | ||
private List<PlayFab.EconomyModels.CatalogItem> Catalog; | ||
|
||
private static IStoreController m_StoreController; | ||
|
||
public void BuyProductByID(string productID) | ||
{ | ||
if (!IsInitialized) throw new Exception("IAP Service is not initialized!"); | ||
|
||
m_StoreController.InitiatePurchase(productID); | ||
} | ||
|
||
private void InitializePurchasing() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/** | ||
* Attempts to log the player in via the Android Device ID. | ||
*/ | ||
private void Login() | ||
{ | ||
// Best practice is to soft-login with a unique ID, then prompt the player to finish | ||
// creating a PlayFab account in order to retrive cross-platform saves or other benefits. | ||
if (UserHasExistingSave) | ||
{ | ||
// @todo Integrate this with the save system. | ||
LoginWithPlayFabRequest loginWithPlayFabRequest = new() | ||
{ | ||
Username = "", | ||
Password = "" | ||
}; | ||
PlayFabClientAPI.LoginWithPlayFab(loginWithPlayFabRequest, OnRegistration, OnPlayFabError); | ||
return; | ||
} | ||
|
||
// AndroidDeviceID will prompt for permissions on newer devices. | ||
// Using a non-device specific GUID and saving to a local file | ||
// is a better approach. PlayFab does allow you to link multiple | ||
// Android device IDs to a single PlayFab account. | ||
PlayFabClientAPI.LoginWithAndroidDeviceID(new LoginWithAndroidDeviceIDRequest() | ||
{ | ||
CreateAccount = true, | ||
AndroidDeviceId = SystemInfo.deviceUniqueIdentifier | ||
}, result => { | ||
RefreshIAPItems(); | ||
}, error => Debug.LogError(error.GenerateErrorReport())); | ||
} | ||
|
||
public void OnGUI() | ||
{ | ||
// Support high-res devices. | ||
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(3, 3, 3)); | ||
|
||
if (!IsInitialized) | ||
{ | ||
GUILayout.Label("Initializing IAP and logging in..."); | ||
return; | ||
} | ||
|
||
// Draw a purchase menu for each catalog item. | ||
foreach (var item in Catalog) | ||
{ | ||
if (GUILayout.Button("Buy " + item.Description)) | ||
{ | ||
BuyProductByID(item.AlternateIds.FirstOrDefault(item => item.Type == "GooglePlay").Value); | ||
} | ||
} | ||
} | ||
|
||
private void OnRegistration(LoginResult result) | ||
{ | ||
PlayFabSettings.staticPlayer.ClientSessionTicket = result.SessionTicket; | ||
} | ||
|
||
public void OnInitialized(IStoreController controller, IExtensionProvider extensions) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void OnInitializeFailed(InitializationFailureReason error) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void OnInitializeFailed(InitializationFailureReason error, string message) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
private void OnPlayFabError(PlayFabError error) | ||
{ | ||
Debug.LogError(error.GenerateErrorReport()); | ||
} | ||
|
||
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
private async void RefreshIAPItems() | ||
{ | ||
SearchItemsRequest request = new() | ||
{ | ||
Count = 50, | ||
Store = new StoreReference() { Id = "Default" } | ||
}; | ||
SearchItemsResponse response = new(); | ||
PlayFabEconomyAPIAsync asyncAPI = new(); | ||
do | ||
{ | ||
response = await asyncAPI.searchItemsAsync(request); | ||
Catalog.AddRange(response.Items); | ||
} while (response.ContinuationToken != ""); | ||
} | ||
|
||
private void SearchAllItems(SearchItemsResponse response) | ||
{ | ||
|
||
|
||
} | ||
|
||
// Start is called before the first frame update | ||
public void Start() | ||
{ | ||
Login(); | ||
} | ||
|
||
// Update is called once per frame | ||
public void Update() | ||
{ | ||
|
||
} | ||
} | ||
|
||
|
||
// Utility classes for the sample. | ||
|
||
public class PlayFabEconomyAPIAsync | ||
{ | ||
private TaskCompletionSource<SearchItemsResponse> searchItemsAsyncTaskSource = new(); | ||
|
||
public void OnSearchItemsRequestComplete(SearchItemsResponse response) | ||
{ | ||
searchItemsAsyncTaskSource.SetResult(response); | ||
} | ||
|
||
public Task<SearchItemsResponse> searchItemsAsync(SearchItemsRequest request) { | ||
PlayFabEconomyAPI.SearchItems(request, OnSearchItemsRequestComplete, error => Debug.LogError(error.GenerateErrorReport())); | ||
return searchItemsAsyncTaskSource.Task; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Samples/Unity/PlayFabEconomyV2/Assets/AndroidIAPExample.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Samples/Unity/PlayFabEconomyV2/Assets/PlayFabEditorExtensions.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Samples/Unity/PlayFabEconomyV2/Assets/PlayFabEditorExtensions/Editor.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.