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

[Need Help] How to load file from streaming asset with c++ plugin (android)

Discussion in 'Scripting' started by nbtthief, Jan 20, 2019.

  1. nbtthief

    nbtthief

    Joined:
    May 27, 2017
    Posts:
    3
    Hi everyone,

    I have some trouble, i got a problem like this. We have an c++ plugin which will load a file from StreamingAsset (ex: music.wav). What i want to achieve is sth like this:

    In UnityCode.cs
    string path = Path.Combine(Application.streamingAssetsPath, filePath);
    CallNativeFunction(path);

    In NativeCode.cpp
    3rdSdk.open(path);

    Everything work fine, no crash but the file cannot be loaded (sdk fire LOAD_ERROR event)

    There are 2 methods i want to try out, but i'm not at the desktop, but still i want some ideas for this task, or correct me if i am wrong.

    1) In C# there are many function in System.IO to help get the correct file <- i want to try out this but i'm afraid the result is like using Unity streamingAssetsPath
    2) Write a Java wrapper which will call Native code <- this approach is a bit messy

    Thank in advance, have a nice day!
     
  2. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    It really depends on the error message.

    Is the streaming asset actually readable?

    Try something as simple as:

    Code (CSharp):
    1. mport java.io.File;
    2. import java.nio.file.Files;
    3.  
    4. File file;
    5. // ...(file is initialised)...
    6. byte[] fileContent = Files.readAllBytes(file.toPath());
    In your Java native code.