Unity3d - Separate UI thread for Android

In my VR app for Android smartphones (I think it doesn't matter if this app is VR or not). I upload 6 large images from Street View and build a skybox from it. During this process, the entire application, incl. environment and user interface freeze. After ~ 10 seconds, the process disappears and Street View appears.

How can I separate the main processing from the user interface? So the phone works, but should not freeze. This is a common problem on the Internet, but how can I solve this problem in Unity for Android?

Thank!

the code:

private byte[] GetStreetviewTexture(string url) {
WWW www = new WWW(url);

while (!www.isDone) ;

if (!string.IsNullOrEmpty(www.error))
{
    Debug.LogWarning("Unable to DL texture: " + www.error);
}
else
{
    bytes = www.texture.EncodeToPNG();
}
return bytes;
}
+4
source share
1

, IEnumerator Unity Coroutine. , , - , ? , Unity / Unity Remote. Coroutines, , Thread, .

Coroutine:

void SomeMethod() {
    StartCoroutine(Threaded());
}

IEnumerator Threaded() {
    // Do something
    yield return new WaitForSeconds(3f);
}

WWW class IEnumerator


OP:

, Coroutine


OPs: /. : . , , , .

void ButtonClicked() {
     SetTexture()
}

void SetTexture() {
    Texture texture = GetTexture()
    Object.texture = texture;
}

Texture GetTexture() {
    Texture texture;
    StartCoroutine(DownloadTexture((textureCallback) => {
        texture = textureCallback;
    }));
    return texture;
}

IEnumerator DownloadTexture(Action<Texture> callbackTexture)
{
    WWW www = new WWW(URL);
    yield return www;

    callback(www.texture);
}

, Coroutine , .

,

โ†’ โ†’ โ†’ โ†’

:

โ†’ โ†’ โ†’

:

void ButtonClick() {
    StartCoroutine(DownloadTexture((callbackTexture) => {
        SetTexture(callbackTexture); // Will run SetTexture when Coroutine DownloadTexture is completed.
    }));
}

IEnumerator DownloadTexture(Action<Texture> callbackTexture)
{
    WWW www = new WWW(URL);
    yield return www;

    callback(www.texture);
}

void SetTexture(Texture texture) {
     object.texture = texture;
}

, . -, byte [] Texture, , Button, . / .

+1

Source: https://habr.com/ru/post/1667883/


All Articles