{"id":2423,"date":"2016-05-28T01:30:53","date_gmt":"2016-05-28T01:30:53","guid":{"rendered":"http:\/\/www.cornflex.org\/?p=2279"},"modified":"2021-04-12T01:19:31","modified_gmt":"2021-04-12T01:19:31","slug":"mobilekit-for-ios-android","status":"publish","type":"post","link":"https:\/\/quentinlengele.com\/index.php\/2016\/05\/28\/mobilekit-for-ios-android\/","title":{"rendered":"MobileKit for iOS &#038; Android"},"content":{"rendered":"<p>Taking a picture\u00a0from your camera to apply on a Material\u00a0or sharing your best score with a screenshot of your game are certainly common features many developers want to handle quickly and easily.<!--more--><br \/>\nMobileKit offers a clean and easy way to open native\u00a0camera roll, image gallery and share features on iOS &amp; Android devices.<\/p>\n<p>AVAILABLE ON <a href=\"https:\/\/assetstore.unity.com\/packages\/tools\/integration\/mobilekit-63705\" target=\"_blank\" rel=\"noopener noreferrer\">UNITY ASSET STORE<\/a><\/p>\n<p>The plugin returns a bitmap file path back to Unity and the share functionality can send a message with a texture and opens a native dialog to choose the desired app according the platform.<\/p>\n<hr \/>\n<h1>HOW TO START<\/h1>\n<p>First, set your project for mobile build, iOS or Android.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2285\" src=\"https:\/\/www.quentinlengele.com\/wp-content\/uploads\/2016\/05\/buildsettings.jpg\" alt=\"buildsettings\" width=\"630\" height=\"321\" \/><\/p>\n<hr \/>\n<h1>ANDROID<\/h1>\n<p>In\u200b \u200border\u200b \u200bto\u200b \u200bget\u200b \u200bthe\u200b \u200bplugin\u200b \u200bcompatible\u200b \u200bwith\u200b \u200ball\u200b \u200bthe\u200b \u200bAndroid\u200b \u200bversions\u200b \u200bsince\u200b \u200b4.1,\u200b \u200byou\u200b \u200bwill\u200b \u200bneed\u200b \u200bto\u200b \u200badd\u00a0 Google\u200b \u200bJAR\/AAR\u200b \u200bResolver\u200b \u200bto\u200b \u200byour\u200b \u200bproject.\u200b \u200bTo\u200b \u200bdo\u200b \u200bthat,\u200b \u200byou\u200b \u200bcan\u200b \u200bdownload\u200b \u200bthe\u200b \u200bcorresponding\u00a0 Unity\u200b \u200bpackage\u200b \u200bhere:\u200b \u200b\u200b<a href=\"https:\/\/github.com\/googlesamples\/unity-jar-resolver\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/googlesamples\/unity-jar-resolver<\/a><\/p>\n<p>Current\u200b \u200bVersion:\u200b \u200b\u200bPlay-services-resolver-1.2.59.0.unitypackage<\/p>\n<p>Once\u200b \u200bthis\u200b \u200bpackage\u200b \u200bis\u200b \u200binstalled\u200b \u200bin\u200b \u200byour\u200b \u200bproject, be sure your Android settings looks like this,\u00a0Assets\/Play\u200b \u200bServices Resolver\/Android\u200b \u200bResolver\/Settings :<br \/>\n(I don&#8217;t like to use Automatic Resolution&#8230; it can be quite slow sometimes)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-18295\" src=\"https:\/\/www.quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot2-1.jpg\" alt=\"\" width=\"350\" height=\"277\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot2-1.jpg 350w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot2-1-300x237.jpg 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Then, you\u2019ll\u200b \u200bneed\u200b \u200bto\u200b \u200bcall\u200b \u200bthe\u200b \u200bResolve\u200b \u200bmethod,\u200b \u200bfrom\u00a0 the\u200b \u200bmenu:\u200b \u200bAssets\/Play\u200b \u200bServices Resolver\/Android\u200b \u200bResolver\/Resolve, here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-18290\" src=\"https:\/\/www.quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot1.jpg\" alt=\"\" width=\"1920\" height=\"1170\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot1.jpg 1920w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot1-300x183.jpg 300w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot1-768x468.jpg 768w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot1-1024x624.jpg 1024w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/p>\n<p>The\u200b \u200bResolver\u200b \u200bwill\u200b \u200badd\u200b \u200bthe\u200b \u200bcorresponding\u00a0 dependencies\u200b \u200bfrom\u200b \u200bAndroid\u200b \u200bSDK\u200b \u200bto\u200b \u200byour project\u200b \u200band\u200b \u200byou\u200b \u200bshould\u200b \u200bsee\u200b \u200ba\u200b \u200bnew \u201cPlugin\/Android\u201d\u200b \u200bfolder with additional libs:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-18291\" src=\"https:\/\/www.quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot2.jpg\" alt=\"\" width=\"302\" height=\"280\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot2.jpg 302w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mkshot2-300x278.jpg 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/p>\n<p>According your Android Build Tools &amp; SDK version, you can change the SDK version of the dependencies in \/MobileKit\/Editor\/MobileKitDependencies.xml (here 26.0.0) before doing a &#8220;Resolve&#8221;.<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;dependencies&gt;\r\n  &lt;androidPackages&gt;\r\n    &lt;androidPackage spec=\"com.android.support:support-v4:26.0.0\" \/&gt;\r\n  &lt;\/androidPackages&gt;\r\n&lt;\/dependencies&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h1>iOS<\/h1>\n<p>The iOS library (MobileKit.a) is compiled for iOS\u00a06.0 and above and is automatically packed when your\u00a0XCode project is generated.<\/p>\n<p>Sinde iOS 10, you must enter Usage Description for Camera and PhotoLibrary access.<br \/>\nYou can do it partially under Player Settings or directly inside your info.plist under XCode.<br \/>\nBe sure these 2 entries are set in info.plist before compiling:<\/p>\n<p>NSPhotoLibraryUsageDescription : &#8220;Your text&#8221;<br \/>\nNSCameraUsageDescription : &#8220;YourText&#8221;<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<h1>DEMO SCENE<\/h1>\n<p>Open the demo scene,\u00a0set your project for mobile build, Android or iOS, and press Build &amp; Run.<br \/>\nThen take a look at\u00a0MobileKitDemo.cs, it is quite\u00a0easy to implement:<\/p>\n<p><a href=\"https:\/\/www.quentinlengele.com\/wp-content\/uploads\/2016\/05\/folder.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-18071\" src=\"https:\/\/www.quentinlengele.com\/wp-content\/uploads\/2016\/05\/folder.jpg\" alt=\"folder\" width=\"112\" height=\"158\" \/><\/a><\/p>\n<pre class=\"height-set:true height-mode:2 lang:default decode:true\">using UnityEngine;\r\nusing UnityEngine;\r\nusing UnityEngine.UI;\r\nusing System.Collections;\r\nusing System.IO;\r\nusing System;\r\nusing CornFlex;\r\n\r\npublic class MobileKitDemo : MonoBehaviour \r\n{\r\n    public Button cameraButton;\r\n    public Button galleryButton;\r\n    public Button shareButton;\r\n    public Toggle cropToggle;\r\n    public Material cubeMaterial;    \r\n\r\n    public Transform cube;\r\n\r\n\tvoid Start() \r\n    {\r\n        cameraButton.onClick.AddListener(OnCameraButton);\r\n        galleryButton.onClick.AddListener(OnGalleryButton);\r\n        shareButton.onClick.AddListener(OnShareButton);\r\n\r\n        MobileKit.Init();\r\n\t}\r\n\r\n    \/\/ ******************************************************************************************\r\n    \/\/ OPEN IMAGE PICKER\r\n    \/\/ ******************************************************************************************\r\n\t\r\n    void OnCameraButton()\r\n    {\r\n        MobileKitImageFormat format = cropToggle.isOn ? MobileKitImageFormat.Cropped : MobileKitImageFormat.Full;\r\n\r\n        MobileKit.OpenImagePicker(MobileKitAction.Camera, format, OnImagePicked);\r\n    }\r\n\r\n    void OnGalleryButton()\r\n    {\r\n        MobileKitImageFormat format = cropToggle.isOn ? MobileKitImageFormat.Cropped : MobileKitImageFormat.Full;\r\n\r\n        MobileKit.OpenImagePicker(MobileKitAction.Gallery, format, OnImagePicked);\r\n    }\r\n\r\n    void OnImagePicked(string filepath)\r\n    {\r\n        try\r\n        {\r\n            if (filepath.Contains(\"http:\/\/\") || filepath.Contains(\"https:\/\/\"))\r\n            {\r\n                StartCoroutine(LoadFromWWW(filepath));\r\n            }\r\n            else\r\n            {\r\n                Texture2D tex = new Texture2D(200, 200);\r\n                tex.LoadImage(File.ReadAllBytes(filepath));\r\n                cubeMaterial.mainTexture = tex;\r\n            }\r\n\r\n        }\r\n        catch (Exception e)\r\n        {\r\n            Debug.Log(e.Message);\r\n        }\r\n    }\r\n\r\n    IEnumerator LoadFromWWW(string url)\r\n    {\r\n        WWW imgreq = new WWW(url);\r\n\r\n        yield return imgreq;\r\n\r\n        Texture2D tex = new Texture2D(200, 200);\r\n        imgreq.LoadImageIntoTexture(tex);\r\n        cubeMaterial.mainTexture = tex;\r\n    }\r\n\r\n    \/\/ ******************************************************************************************\r\n    \/\/ SHARE SCREENSHOT + MESSAGE\r\n    \/\/ ******************************************************************************************\r\n\r\n    void OnShareButton()\r\n    {\r\n        \/\/ share simple text message (with link)\r\n\r\n        \/\/MobileKit.Share(\"Share\", \"Check this out: https:\/\/www.quentinlengele.com\");   \r\n\r\n        \/\/ take screenshot and share it\r\n\r\n        StartCoroutine(TakeScreenShot()); \r\n    }\r\n\r\n\r\n    public IEnumerator TakeScreenShot()\r\n    {\r\n        yield return new WaitForEndOfFrame();\r\n\r\n        Texture2D shot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);\r\n        shot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);\r\n        shot.Apply();\r\n\r\n        byte[] bytes = shot.EncodeToJPG();\r\n\r\n        string path = Application.persistentDataPath + \"\/Screenshot.jpg\";\r\n\r\n        File.WriteAllBytes(path, bytes);\r\n         \r\n        MobileKit.Share(\"Share\", \"Hello World!\", path);    \/\/ share text message + image\r\n    }\r\n\r\n\r\n\r\n\tvoid Update()\r\n    {\r\n        cube.Rotate(new Vector3(0.3f, 0.1f, 0.25f));\r\n\t}\r\n}\r\n<\/pre>\n<hr \/>\n<h1>SCREENSHOTS<\/h1>\n<div class=\"row list-container\"><div class=\"wp-caption col-md-4\"><a class=\"thumbnail magnific-gallery\" href=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png\" data-links=\" https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png \"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"800\" src=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png\" class=\"attachment-rect-image size-rect-image\" alt=\"\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png 480w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1-180x300.png 180w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><p class=\"wp-caption-text\">\t\t\t\t\t\t<\/p><\/div><div class=\"wp-caption col-md-4\"><a class=\"thumbnail magnific-gallery\" href=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png\" data-links=\" https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png \"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"800\" src=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png\" class=\"attachment-rect-image size-rect-image\" alt=\"\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png 480w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3-180x300.png 180w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><p class=\"wp-caption-text\">\t\t\t\t\t\t<\/p><\/div><div class=\"wp-caption col-md-4\"><a class=\"thumbnail magnific-gallery\" href=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png\" data-links=\" https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png \"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"800\" src=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png\" class=\"attachment-rect-image size-rect-image\" alt=\"\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png 480w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4-180x300.png 180w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><p class=\"wp-caption-text\">\t\t\t\t\t\t<\/p><\/div><div class=\"wp-caption col-md-4\"><a class=\"thumbnail magnific-gallery\" href=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png\" data-links=\" https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png \"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"800\" src=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png\" class=\"attachment-rect-image size-rect-image\" alt=\"\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png 480w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6-180x300.png 180w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><p class=\"wp-caption-text\">\t\t\t\t\t\t<\/p><\/div><div class=\"wp-caption col-md-4\"><a class=\"thumbnail magnific-gallery\" href=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png\" data-links=\" https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png \"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"800\" src=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png\" class=\"attachment-rect-image size-rect-image\" alt=\"\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png 480w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7-180x300.png 180w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><p class=\"wp-caption-text\">\t\t\t\t\t\t<\/p><\/div><div class=\"wp-caption col-md-4\"><a class=\"thumbnail magnific-gallery\" href=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png\" data-links=\" https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit7.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit6.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit4.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit3.png,https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit1.png \"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"1334\" src=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png\" class=\"attachment-rect-image size-rect-image\" alt=\"\" srcset=\"https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11.png 750w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11-169x300.png 169w, https:\/\/quentinlengele.com\/wp-content\/uploads\/2016\/05\/mobilekit11-576x1024.png 576w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><p class=\"wp-caption-text\">\t\t\t\t\t\t<\/p><\/div><\/div>\n<p>&nbsp;<\/p>\n<h1><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>Taking a picture\u00a0from your camera to apply on a Material\u00a0or sharing your best score with a screenshot of your game are certainly common features many developers want to handle quickly and easily.<\/p>\n","protected":false},"author":1,"featured_media":18937,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/posts\/2423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/comments?post=2423"}],"version-history":[{"count":6,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/posts\/2423\/revisions"}],"predecessor-version":[{"id":18938,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/posts\/2423\/revisions\/18938"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/media\/18937"}],"wp:attachment":[{"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/media?parent=2423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/categories?post=2423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/quentinlengele.com\/index.php\/wp-json\/wp\/v2\/tags?post=2423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}