Skip to content

Instantly share code, notes, and snippets.

@gegagome
Created February 19, 2018 21:34
Show Gist options
  • Save gegagome/bf58d08f4e26669bcada5f0d087ebdab to your computer and use it in GitHub Desktop.
Save gegagome/bf58d08f4e26669bcada5f0d087ebdab to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
// set these so purchase is fulfilled or fails
// OnPurchaseComplete > FullAppUnlock ()
// OnPurchaseFailed > ParentalGate.FailPurchase();
public delegate void KIDpediaIAP ();
public class IAPManager : MonoBehaviour {
// sends to SceneIcon.cs
public static event KIDpediaIAP UnlockFullContent;
public static event KIDpediaIAP PurchaseFail;
public bool _isPremium = false;
const string FILE_PATH = "/playerInfo.dat";
public static IAPManager _iapManager;
void Awake () {
if (_iapManager == null) {
DontDestroyOnLoad (this.gameObject);
_iapManager = this;
} else if (_iapManager != this) {
Destroy (this.gameObject);
}
}
void Start () {
//if (true) {
// DeleteFile();
//}
Load ();
}
public void FullAppUnlock () {
_isPremium = true;
Save ();
UnlockFullContent ();
}
public void Save () {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + FILE_PATH);
PlayerData data = new PlayerData ();
data.isPremium = _isPremium;
bf.Serialize (file, data);
file.Close ();
}
public void Load () {
// the next block won't work on web
if (File.Exists ((Application.persistentDataPath + FILE_PATH))) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + FILE_PATH, FileMode.Open);
PlayerData data = bf.Deserialize (file) as PlayerData;
file.Close ();
_isPremium = data.isPremium;
}
}
public void DeleteFile () {
if (File.Exists ((Application.persistentDataPath + FILE_PATH))) {
File.Delete((Application.persistentDataPath + FILE_PATH));
}
}
public bool IsPremium () {
return _isPremium;
}
public void PurchaseFailed ()
{
PurchaseFail ();
}
void Update() {
if (Input.GetKeyUp(KeyCode.A)) {
}
}
}
[Serializable]
class PlayerData {
public bool isPremium;
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using HedgehogTeam.EasyTouch;
using DG.Tweening;
[RequireComponent (typeof(BoxCollider2D))]
public class SceneIcon : MonoBehaviour {
public string sceneToLoad;
public int orderInList;
Vector3 deltaPosition;
int fingerIndex;
bool isDragged = false;
// IAPManager.cs should set this value to allow users to access this scene
bool isUnlocked;
SpriteRenderer spriteRenderer;
Color lockedColor = new Color(1f, 1f, 1f, 0.5f);
Color unlockedColor = new Color(1f, 1f, 1f, 1f);
public ResizerOfSceneSeletor AResizerOfSceneSelector;
readonly Vector2 ANGLE = Vector2.zero;
void Awake () {
this.gameObject.layer = 16;
// Debug.Log ("SceneIcon # " + orderInList + rg);
spriteRenderer = GetComponent<SpriteRenderer> ();
spriteRenderer.color = lockedColor;
}
void Start () {
if (!IAPManager._iapManager.IsPremium ()) {
if (orderInList == 0 || orderInList > 3) {
UnlockThisScene ();
IncreaseOpacityOfIcon ();
}
} else {
UnlockThisScene ();
IncreaseOpacityOfIcon ();
}
}
void OnEnable () {
EasyTouch.On_TouchUp += On_TouchUp;
IAPManager.UnlockFullContent += UnlockPremiumContent;
IAPManager.PurchaseFail += NoPurchaseResult;
}
void OnDisable () {
this.transform.position = Vector2.zero;
UnsubscribeEvent ();
}
void OnDestroy () {
UnsubscribeEvent ();
}
void UnsubscribeEvent () {
EasyTouch.On_TouchUp -= On_TouchUp;
IAPManager.UnlockFullContent -= UnlockPremiumContent;
IAPManager.PurchaseFail -= NoPurchaseResult;
}
void On_TouchUp (Gesture gesture) {
if (gesture.pickedObject == gameObject) {
if (isUnlocked)
{
SoundManager.PlaySoundForSceneIconsEndDrag();
// rg.bodyType = RigidbodyType2D.Kinematic;
LoadingScreen._instance.LoadScene(sceneToLoad);
//StartCoroutine("FrameIconBeforeLoadingScene");
}
}
}
// stopped usage on Nov 13th when made SceneIcons buttons
IEnumerator FrameIconBeforeLoadingScene ()
{
transform.DORotate(ANGLE, 0.5f, RotateMode.Fast);
yield return new WaitForSeconds(1.5f);
LoadingScreen._instance.LoadScene(sceneToLoad);
}
void UnlockThisScene ()
{
isUnlocked = true;
}
void LockThisScene ()
{
isUnlocked = false;
}
void IncreaseOpacityOfIcon ()
{
spriteRenderer.color = unlockedColor;
}
void DecreaseOpacityOfIcon () {
spriteRenderer.color = lockedColor;
}
void UnlockPremiumContent ()
{
if (orderInList != 0) {
spriteRenderer.DOColor (unlockedColor, 0.5f);
UnlockThisScene ();
}
}
void LockOPremiumContent () {
if (orderInList != 0)
{
DecreaseOpacityOfIcon();
LockThisScene();
}
}
void DestroyPromos ()
{
if (orderInList > 3) {
Destroy (this.gameObject);
}
}
public void NoPurchaseResult ()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment