Home > database >  Unity, download only what user selects
Unity, download only what user selects

Time:01-04

My game is made by Unity 2021 and it has around 250K of sound files with around 1.5GB , But the user won't use all of this files , he will use only what he selects, When the game begins it will ask the user to choose only two languages, I want only these two languages folders to be downloaded into the user device after he installs the game. How do I Accomplish that? Thanks.

CodePudding user response:

Use There are several types of DownloadHandlers:

DownloadHandlerBuffer is used for simple data storage.
DownloadHandlerFile is used for downloading and saving file to disk with low memory footprint.
DownloadHandlerTexture is used for downloading images.
DownloadHandlerAssetBundle is used for fetching AssetBundles.
DownloadHandlerAudioClip is used for downloading audio files.
DownloadHandlerMovieTexture is used for downloading video files. It’s recommended that you use VideoPlayer for video download and movie playback since MovieTexture is deprecated.
DownloadHandlerScript is a special class. On its own, it does nothing. However, this class can be inherited by a user-defined class. This class receives callbacks from the UnityWebRequest system, which can then be used to perform completely custom handling of data as it arrives from the network.

The APIs are similar to DownloadHandlerTexture’s interface.

UnityWebRequest has a property disposeDownloadHandlerOnDispose, which defaults to true. If this property is true, when UnityWebRequest object is disposed, Dispose() will also be called on attached download handler rendering it useless. If you keep a reference to download handler longer than the reference to UnityWebRequest, you should set disposeDownloadHandlerOnDispose to false. DownloadHandlerBuffer

This Download Handler is the simplest, and handles the majority of use cases. It stores received data in a native code buffer. When the download is complete, you can access the buffered data either as an array of bytes or as a text string.

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class FileDownloader : MonoBehaviour {

    void Start () {
        StartCoroutine(DownloadFile());
    }

    IEnumerator DownloadFile() {
        var uwr = new UnityWebRequest("https://unity3d.com/", UnityWebRequest.kHttpVerbGET);
        string path = Path.Combine(Application.persistentDataPath, "unity3d.html");
        uwr.downloadHandler = new DownloadHandlerFile(path);
        yield return uwr.SendWebRequest();
        if (uwr.result != UnityWebRequest.Result.Success)
            Debug.LogError(uwr.error);
        else
            Debug.Log("File successfully downloaded and saved to "   path);
    }
}

CodePudding user response:

I used asset bundles that can be downloaded on-demand for the same objective in one of products. It is very easy to implement.

I am guessing every localized audio has a counterpart for the other language, I named them the same. (Example, PlayerSpeech1.ogg audio file has the same name for ENG and GER languages)

I bundled them into 2 different bundles (AudioENG.assetbundle and AudioGER.assetbundle).

At the start of the game, you choose which bundle to load or download (both are the same method for UnityWebRequestAssetBundle, it uses cached assetbundle if it is already downloaded) and use it in the rest of the game.

Because the audio names are the same, only thing you choose is the assetbundle to use.

Here you can find an exemplary code:

  •  Tags:  
  • Related