Created
          January 15, 2016 08:21 
        
      - 
      
- 
        Save sandcastle/192836ab22461f1f69c2 to your computer and use it in GitHub Desktop. 
    Sample TapJoy Unity integration from the TapJoy website.
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | using UnityEngine; | |
| using System.Collections; | |
| using TapjoyUnity; | |
| public class TapjoyIntegration { | |
| public static TJPlacement placementAppLaunch; | |
| public static TJPlacement placementAppClose; | |
| public static GameObject bgmObject; | |
| public static void Init() { | |
| // Connect | |
| Tapjoy.OnConnectSuccess += HandleOnConnectSuccess; | |
| Tapjoy.OnConnectFailure += HandleOnConnectFailure; | |
| // Video | |
| Tapjoy.OnVideoStart += HandleOnVideoStart; | |
| Tapjoy.OnVideoError += HandleOnVideoError; | |
| Tapjoy.OnVideoComplete += HandleOnVideoComplete; | |
| // View | |
| Tapjoy.OnViewWillOpen += HandleOnViewWillOpen; | |
| Tapjoy.OnViewWillClose += HandleOnViewWillClose; | |
| Tapjoy.OnViewDidOpen += HandleOnViewDidOpen; | |
| Tapjoy.OnViewDidClose += HandleOnViewDidClose; | |
| // Currency | |
| Tapjoy.OnGetCurrencyBalanceResponse += HandleOnGetCurrencyBalanceResponse; | |
| //Tapjoy.OnGetCurrencyBalanceResponseFailure += HandleOnGetCurrencyBalanceResponseFailure; | |
| //Tapjoy.OnSpendCurrencyResponse += HandleOnSpendCurrencyResponse; | |
| //Tapjoy.OnSpendCurrencyResponseFailure += HandleOnSpendCurrencyResponseFailure; | |
| //Tapjoy.OnAwardCurrencyResponse += HandleOnAwardCurrencyResponse; | |
| //Tapjoy.OnAwardCurrencyResponseFailure += HandleOnAwardCurrencyResponseFailure; | |
| Tapjoy.OnEarnedCurrency += HandleOnEarnedCurrency; | |
| Tapjoy.OnOffersResponse += HandleOnOffersResponse; | |
| Tapjoy.OnOffersResponseFailure += HandleOnOffersResponseFailure; | |
| // Placement | |
| TJPlacement.OnRequestSuccess += HandleOnPlacementRequestSuccess; | |
| TJPlacement.OnRequestFailure += HandleOnPlacementRequestFailure; | |
| TJPlacement.OnContentReady += HandleOnPlacementContentReady; | |
| TJPlacement.OnContentShow += HandleOnPlacementContentShow; | |
| TJPlacement.OnContentDismiss += HandleOnPlacementContentDismiss; | |
| TJPlacement.OnPurchaseRequest += HandleOnPurchaseRequest; | |
| TJPlacement.OnRewardRequest += HandleOnRewardRequest; | |
| Tapjoy.SetAppDataVersion ("20131901"); | |
| Tapjoy.SetUserID ("234232"); | |
| Tapjoy.SetUserLevel (1); | |
| Tapjoy.SetUserFriendCount (3); | |
| Tapjoy.SetUserCohortVariable (1, "DemoUser"); | |
| } | |
| public static void Destroy() { | |
| TJPlacement.OnContentDismiss -= HandleOnPlacementContentDismiss; | |
| TJPlacement.OnContentReady -= HandleOnPlacementContentReady; | |
| TJPlacement.OnContentShow -= HandleOnPlacementContentShow; | |
| TJPlacement.OnRequestFailure -= HandleOnPlacementRequestFailure; | |
| TJPlacement.OnRequestSuccess -= HandleOnPlacementRequestSuccess; | |
| TJPlacement.OnPurchaseRequest -= HandleOnPurchaseRequest; | |
| TJPlacement.OnRewardRequest -= HandleOnRewardRequest; | |
| } | |
| public static void ShowPlacementAppLaunch() { | |
| if (placementAppLaunch == null) { | |
| placementAppLaunch = TJPlacement.CreatePlacement("APP_LAUNCH"); | |
| } | |
| if (placementAppLaunch != null) { | |
| placementAppLaunch.SetPreload(true); | |
| placementAppLaunch.RequestContent(); | |
| } | |
| } | |
| public static void Quit() { | |
| ShowMessage("placementAppClose == null ?" + | |
| (placementAppClose == null) + " isReady:" + (placementAppClose.IsContentReady())); | |
| if (placementAppClose != null && placementAppClose.IsContentReady ()) { | |
| // Quit on dismissing placementAppClose | |
| placementAppClose.ShowContent (); | |
| } else { | |
| if (Application.platform == RuntimePlatform.IPhonePlayer) { | |
| // Do not use Application.Quit() for iOS | |
| } | |
| else { | |
| Destroy (); | |
| Application.Quit (); | |
| } | |
| } | |
| } | |
| static void HandleOnOffersResponseFailure (string error) | |
| { | |
| ShowMessage("HandleOnOffersResponseFailure:" + error); | |
| } | |
| static void HandleOnOffersResponse () | |
| { | |
| } | |
| static void HandleOnGetCurrencyBalanceResponse (string currencyName, int amount) | |
| { | |
| ShowMessage("Currency Balance: " + currencyName + " " + amount); | |
| } | |
| static void HandleOnEarnedCurrency (string currencyName, int amount) | |
| { | |
| // NOTE: Should I update ruby of Shop ? | |
| /* | |
| int ruby = PlayerPrefs.GetInt ("Ruby", 0); | |
| ruby += amount; | |
| PlayerPrefs.SetInt("Ruby", ruby); | |
| Shop.ruby = ruby; | |
| */ | |
| ShowMessage("Earned: " + currencyName + " " + amount); | |
| } | |
| static void HandleOnViewDidClose (int type) | |
| { | |
| ShowMessage ("HandleOnViewDidClose: " + type); | |
| } | |
| static void HandleOnViewWillClose (int type) | |
| { | |
| ShowMessage ("HandleOnViewWillClose: " + type); | |
| } | |
| static void HandleOnViewDidOpen (int type) | |
| { | |
| ShowMessage ("HandleOnViewDidOpen: " + type); | |
| } | |
| static void HandleOnViewWillOpen (int type) | |
| { | |
| ShowMessage ("HandleOnViewWillOpen: " + type); | |
| } | |
| static void HandleOnVideoComplete () | |
| { | |
| ShowMessage ("HandleOnVideoComplete"); | |
| } | |
| static void HandleOnVideoError (string error) | |
| { | |
| ShowMessage ("HandleOnVideoError"); | |
| } | |
| static void HandleOnVideoStart () | |
| { | |
| ShowMessage ("HandleOnVideoStart"); | |
| } | |
| static void HandleOnConnectSuccess () | |
| { | |
| ShowMessage ("TapjoyIntegration.HandleOnConnectSuccess"); | |
| // CreatePlacement should be called after successful connect | |
| if (placementAppClose == null) { | |
| placementAppClose = TJPlacement.CreatePlacement ("APP_CLOSE"); | |
| if (placementAppClose != null) { | |
| placementAppClose.SetPreload (true); | |
| placementAppClose.RequestContent (); | |
| } | |
| } | |
| } | |
| static void HandleOnConnectFailure () | |
| { | |
| ShowMessage ("HandleOnConnectFailure"); | |
| } | |
| static void HandleOnPlacementRequestSuccess (TJPlacement placement) | |
| { | |
| ShowMessage ("HandleOnPlacementRequestSuccess({0}), isAvailable={1}, isReady={2}", | |
| placement.GetName (), placement.IsContentAvailable(), placement.IsContentReady()); | |
| } | |
| static void HandleOnPlacementRequestFailure (TJPlacement placement, string error) | |
| { | |
| ShowMessage ("HandleOnPlacementRequestFailure({0}, {1})", placement.GetName (), error); | |
| } | |
| static void HandleOnPlacementContentReady (TJPlacement placement) | |
| { | |
| ShowMessage ("HandleOnPlacementContentReady({0})", placement.GetName ()); | |
| if (placementAppLaunch == placement) { | |
| Debug.Log ("loadedLevelName == " + Application.loadedLevelName); | |
| if (!Application.loadedLevelName.Equals("Game")) { | |
| placementAppLaunch.ShowContent(); | |
| } | |
| } | |
| } | |
| static void HandleOnPlacementContentShow (TJPlacement placement) | |
| { | |
| ShowMessage ("HandleOnPlacementContentShow({0})", placement.GetName ()); | |
| if (bgmObject) { | |
| if (bgmObject.audio.isPlaying) { | |
| bgmObject.audio.Pause(); | |
| } | |
| } | |
| } | |
| static void HandleOnPlacementContentDismiss (TJPlacement placement) | |
| { | |
| ShowMessage ("HandleOnPlacementContentDismiss({0})", placement.GetName ()); | |
| if (placement == placementAppClose) { | |
| if (Application.platform == RuntimePlatform.IPhonePlayer) { | |
| // Do not use Application.Quit() for iOS | |
| } | |
| else { | |
| Destroy (); | |
| Application.Quit (); | |
| } | |
| } | |
| else { | |
| if (bgmObject) { | |
| if (!bgmObject.audio.isPlaying) { | |
| bgmObject.audio.Play(); | |
| } | |
| } | |
| } | |
| } | |
| static void HandleOnPurchaseRequest (TJPlacement placement, TJActionRequest actionRequest, string productId) | |
| { | |
| ShowMessage ("OnPurchaseRequest {0} {1} {2}", placement.GetName (), actionRequest.requestID, productId); | |
| Application.LoadLevel ("Shop"); | |
| actionRequest.Completed (); | |
| } | |
| static void HandleOnRewardRequest (TJPlacement placement, TJActionRequest actionRequest, string itemId, int quantity) | |
| { | |
| ShowMessage ("OnRewardRequest {0} {1} {2} {3}", placement.GetName (), actionRequest.requestID, itemId, quantity); | |
| actionRequest.Completed (); | |
| } | |
| public static void ShowMessage (string format, params object[] args) | |
| { | |
| string message = string.Format (format, args); | |
| GUIToast.Add (message); | |
| Debug.Log (message); | |
| } | |
| public static void SetBGMObject(GameObject _bgmObject) | |
| { | |
| bgmObject = _bgmObject; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
http://dev.tapjoy.com/sdk-integration/unity/sample-application/