Skip to content

Instantly share code, notes, and snippets.

@canujohann
Last active August 29, 2015 14:06
Show Gist options
  • Save canujohann/5af17dc2de1ca6e03f7d to your computer and use it in GitHub Desktop.
Save canujohann/5af17dc2de1ca6e03f7d to your computer and use it in GitHub Desktop.
Plugin unity

Assets/Plugins/Androidプラグイン下に[Appmart_Plugin.cs]:

using UnityEngine;
using System.Collections;

public class Appmart_Plugin : MonoBehaviour {
	
	static AndroidJavaObject m_appmart;
	static GameObject m_instance ;
	
	public static string APPMART_DEVELOPER_ID = "xxxxx";
	public static string APPMART_LICENSE_KEY = "xxxx";
	public static string APPMART_PUBLIC_KEY = "xxxx";
	public static string APPMART_APP_ID = "xxxxx";
	
	//return values for callback
	public static int LABEL_FIRST_STEP_VALIDATED = 5 ;
	public static int LABEL_SUCCESS_PURCHASE = 1 ;
	public static int LABEL_ERRROR_PURCHASE = -1 ;
	public static int LABEL_EXCEPTION = -10 ;
	public static int LABEL_RETURN_KEY = 6 ;
	
	//parameters updated by the constructor
	public static string objEvent = "" ;
	public static string handlerRecievedResultAppMart = "ReceivedResultAppMart";
	

	//Awake is called when the script instance is being loaded.
	public void Awake(){
		
		m_instance = gameObject;
		
		objEvent = gameObject.name;
			
		//Makes the object target not be destroyed automatically when loading a new scene.
		DontDestroyOnLoad(transform);
		
		#if UNITY_ANDROID
		inst();
		#endif
	}
	
	
	void inst ()
	{
		#if UNITY_ANDROID
		try {
			m_appmart = new AndroidJavaObject ("jp.app_mart.plugin.PNUnityPlugin_AppMart", new object[]{objEvent,handlerRecievedResultAppMart,
				APPMART_DEVELOPER_ID,APPMART_LICENSE_KEY,APPMART_PUBLIC_KEY,APPMART_APP_ID});
		} catch{}
		#endif
	}
	
	
	
	//buy an item
	public void BuyItem (string idItem)
	{
		Debug.Log("enter in 'BuyItem' method" );

		#if UNITY_ANDROID
		
		// If network is not reachable.
		if (Application.internetReachability == NetworkReachability.NotReachable) {
			Debug.Log("network is not reachable.");
		}else{ 
			if (m_appmart == null)
				inst ();
			try {
				m_appmart.Call ("Click", new object[]{idItem});
			} catch { }
		}
		#endif
	}
	
	
	// Callback from appmart (settlement screen)
	public void ReceivedResultAppMart (int result)
	{
		#if UNITY_ANDROID
		
		Debug.Log("RecievedResultAppMart called : " + result);
		
		switch (result)
		{
		case LABEL_FIRST_STEP_VALIDATED:	//return from payment screen
			//TODO provide content to user
			m_appmart.Call("confirmPayment");
			break;
		case LABEL_SUCCESS_PURCHASE: //purchase ok
			Debug.Log("RecievedResultAppMart called : payment validated !!" );
			break;
		case LABEL_ERRROR_PURCHASE:	//purchase not ok
			Debug.Log("RecievedResultAppMart called : payment not validated !!" );
			break;
		case LABEL_EXCEPTION:	//exception occurs !
			Debug.Log("an exception occured !!" );
			break;
		case LABEL_RETURN_KEY:	// back key pressed
			Debug.Log("Back key was pressed" );
			break;
		default:
			Debug.Log("RecievedResultAppMart called : unknow method called" );
			break;
		}
		
		
		#endif
	}
	
}

呼び出し:

using UnityEngine;
using System.Collections;

public class scriptc : MonoBehaviour {
	
	void OnMouseDown()
	{

		var pluginGameObject=new GameObject("Appmart Plugin GameObject");
		pluginGameObject.AddComponent<Appmart_Plugin>();

		Appmart_Plugin appmart_instance = pluginGameObject.FindObjectOfType (typeof(Appmart_Plugin)) as Appmart_Plugin;
		appmart_instance.BuyItem("tudo04");

	}
}

http://stackoverflow.com/questions/25804555/create-android-plugin-with-unity-c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment