Search Unity

how to properly configure obb and apk expansion simple

Discussion in 'Scripting' started by TSC, Aug 21, 2018.

  1. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    I found this script to help with split binary apk yet it utilizes vuforia, My apk is 163MB now and I'm not able to upload to Google Play, how can I customize this in a simple way to get the game to work if I split the binary?

    I tried splitting the binary before a while back and it didn't get pass the main menu. I understand programming a bit but I'm not a beast just yet, take anyone???


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SplitBinary: MonoBehaviour
    8. {
    9.     //Only use in Preloader Scene for android split APK
    10.     private string nextScene = "Menu_Ingame";
    11.     private bool obbisok = false;
    12.     private bool loading = false;
    13.     private bool replacefiles = false;
    14.     //true if you wish to over copy each time
    15.  
    16.     private string[] paths = {  
    17.         "Vuforia/KVK.dat",
    18.         "Vuforia/KVK.xml"
    19.     };
    20.  
    21.     void Update ()
    22.     {
    23.         if (Application.platform == RuntimePlatform.Android) {
    24.             if (Application.dataPath.Contains (".obb") && !obbisok) {
    25.                 StartCoroutine (CheckSetUp ());
    26.                 obbisok = true;
    27.             }
    28.         } else {
    29.             if (!loading) {
    30.                 StartApp ();
    31.             }
    32.         }
    33.     }
    34.  
    35.     public void StartApp ()
    36.     {
    37.         loading = true;
    38.         SceneManager.LoadSceneAsync(nextScene);
    39.     }
    40.  
    41.     public IEnumerator CheckSetUp ()
    42.     {
    43.         //Check and install!
    44.         for (int i = 0; i < paths.Length; ++i) {
    45.             yield return StartCoroutine (PullStreamingAssetFromObb (paths [i]));
    46.         }
    47.         yield return new WaitForSeconds (1f);
    48.         StartApp ();
    49.     }
    50.  
    51.     //Alternatively with movie files these could be extracted on demand and destroyed or written over
    52.     //saving device storage space, but creating a small wait time.
    53.     public IEnumerator PullStreamingAssetFromObb (string sapath)
    54.     {
    55.         if (!File.Exists (Application.persistentDataPath + sapath) || replacefiles) {
    56.             WWW unpackerWWW = new WWW (Application.streamingAssetsPath + "/" + sapath);
    57.             while (!unpackerWWW.isDone) {
    58.                 yield return null;
    59.             }
    60.             if (!string.IsNullOrEmpty (unpackerWWW.error)) {
    61.                 Debug.Log ("Error unpacking:" + unpackerWWW.error + " path: " + unpackerWWW.url);
    62.  
    63.                 yield break; //skip it
    64.             } else {
    65.                 Debug.Log ("Extracting " + sapath + " to Persistant Data");
    66.  
    67.                 if (!Directory.Exists (Path.GetDirectoryName (Application.persistentDataPath + "/" + sapath))) {
    68.                     Directory.CreateDirectory (Path.GetDirectoryName (Application.persistentDataPath + "/" + sapath));
    69.                 }
    70.                 File.WriteAllBytes (Application.persistentDataPath + "/" + sapath, unpackerWWW.bytes);
    71.             }
    72.         }
    73.         yield return 0;
    74.     }
    75. }
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Probably easiest to try something like this: https://assetstore.unity.com/packages/tools/utilities/google-play-obb-downloader-3189

    It used to be the case that you always had to download the OBB file from Google the first time you ran the game but it seems like that has now changed. When the player downloads your app the OBB file gets automatically downloaded so it should all 'just work' but it's worth using that plugin anyway to make sure it downloads it (if it didn't happen automatically). Here's some further info: https://docs.unity3d.com/Manual/android-OBBsupport.html

    The OBB file needs to be named in a particular way too. If your app identifier was 'com.mycompany.appname' then the OBB might be 'main.1.com.mycompany.appname' where '1' is the OBB version number.

    Alternatively you could try using AssetBundles if the OBB stuff is too complicated.