Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DownloadHandlerScript is not invoked on android in Unity2017.

Discussion in 'Multiplayer' started by watsonsong, Sep 6, 2017.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    The DownloadHandlerScript is not invoked on android when I upgrade my code to Unity2017.
    My code is compile in DLL and used in the project. In the editor it work fine, but on android device there is no any callback. The ReceiveData, CompleteContent and ReceiveContentLength is not invoked, but the UnityWebRequest is still finished.
    I am using UnityWebRequest.Get to create a new UnityWebRequest, new a custom DownloadHandlerScript and assign it to the downloadHandler.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Can you show the code for it?
    Also, since you compile it to a dll, does it work when it's in a script?
     
  3. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I have try using the UnityWebRequest constructor instand using UnityWebRequest.Get but the result is the same.
    My code is like this:
    Code (CSharp):
    1.  
    2. this.downloadBuffer = downloadBuffer;
    3. this.handler = new DownloadHandlerBundle(
    4.     this.writer, downloadBuffer);
    5. this.www = new UnityWebRequest(
    6. this.url, UnityWebRequest.kHttpVerbGET, this.handler, null);
    7. this.www.Send();
    8.  
    I customize the DownloadHandlerScript, and use a buffer pool as its internal buffer.
    Code (CSharp):
    1.  
    2. internal class DownloadHandlerBundle : DownloadHandlerScript
    3. {
    4.         internal DownloadHandlerBundle(
    5.             AssetBundleCacheWriter writer,
    6.             byte[] preallocatedBuffer)
    7.             : base(preallocatedBuffer)
    8.         {
    9.         }
    10. }
    11.  
    When I compile my code into DLL, I am using the CSharpCodeProvider:
    Code (CSharp):
    1.  
    2. var providerOptions = new Dictionary<string, string>(StringComparer.Ordinal);
    3. providerOptions.Add("CompilerVersion", "v4.0");
    4. using (var provider = new CSharpCodeProvider(providerOptions))
    5. {
    6.     var results = provider.CompileAssemblyFromFile(parameters, sourceFiles);
    7.     if (results.Errors.Count > 0)
    8.     {}
    9.  
    Yes it a bit more complicated, I am trying to simplify it, using the script directly and see what will happen.
     
  4. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I found my problem because I am obfuscated my DLL. If I am not obfuscate the DLL, it work no problem on android device.
    But I am doing the same thing in Uinty 5.6.x and everything is OK, it there any change broken this way?

    The obfuscated result is like this, it seem nothing to wrong.
     

    Attached Files:

  5. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I am using the Obfuscar to obfuscated my DLL. And if I am turn on the "UseUnicodeNames" for better obfuscate, the android package will occur error with DownloadHandlerScript.
    Is it use reflection to do something and does not support the unicode symbol name?
    It always work well on Unity 5.x~
     

    Attached Files:

  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Sounds worth of bug report.
     
  7. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I have no idea where to add the issue.
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    You can report bugs from Unity editor Help menu.
     
  9. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I am wrong, this problem has no relationship obfuscated.
    I write this code and compile it into dll without any obfuscated:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. public sealed class GameTest : MonoBehaviour
    6. {
    7.     private IEnumerator Start()
    8.     {
    9.         var handler = new MyDownloadHandler();
    10.         var www = new UnityWebRequest(
    11.             "http://www.bbc.com",
    12.             UnityWebRequest.kHttpVerbGET,
    13.            handler,
    14.            null);
    15.         Debug.Log("Start web request: " + www.url);
    16.         yield return www;
    17.  
    18.         Debug.Log("Complete web request: " + www.responseCode);
    19.     }
    20. }
    21.  
    22. public sealed class MyDownloadHandler : DownloadHandlerScript
    23. {
    24.     /// <inheritdoc/>
    25.     protected override bool ReceiveData(byte[] data, int dataLength)
    26.     {
    27.         Debug.LogError("ReceiveData: " + dataLength);
    28.         return true;
    29.     }
    30.  
    31.     /// <inheritdoc/>
    32.     protected override void CompleteContent()
    33.     {
    34.         Debug.LogError("CompleteContent.");
    35.     }
    36.  
    37.     /// <inheritdoc/>
    38.     protected override void ReceiveContentLength(int contentLength)
    39.     {
    40.         Debug.LogError("ReceiveContentLength: " + contentLength);
    41.     }
    42. }
    The log inside the 'MyDownloadHandler' is never invoked. I test it on android simulator on windows.
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    This line is wrong!
    Your should do:
    yield return www.Send();
     
  11. YYXL_01

    YYXL_01

    Joined:
    Dec 31, 2020
    Posts:
    2
    I have same problem,can you tell me how to fix it?
     
  12. unity_B2XDOGJocxNmxw

    unity_B2XDOGJocxNmxw

    Joined:
    Dec 23, 2021
    Posts:
    1
    "The DownloadHandlerScript is not invoked on android when I upgrade my code to Unity2017.
    My code is compile in DLL and used in the project. In the editor it work fine, but on android device there is no any callback. The ReceiveData, CompleteContent and ReceiveContentLength is not invoked, but the UnityWebRequest is still finished."

    I've got the same problem, finally I found it's due to stripping level, my project use "medium" stripping level and got the problem.

    after I preserve the UnityEngine.UnityWebRequestModule in link.xml, all works fine!

    link.xml ust like this:

    <?xml version="1.0" encoding="utf-8"?>
    <linker>
    <assembly fullname="UnityEngine.UnityWebRequestModule" preserve="all" />
    </linker>
     
  13. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I think it is a bug about the code strip. There is nothing about reflection, and code stripper should work with this.