Calling Android NDK function from Unity Script

前端 未结 2 993
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 04:11

So I\'m creating an Android app that uses Unity ... I am getting some assetbundle from Unity but I do not know the url before Unity starts. Thus Unity needs to call a funct

相关标签:
2条回答
  • 2021-01-07 04:42

    You could likely send the 'dynamic' url in your call to getURL. Do something like:

    return plugin.Call<string>("getURL", new object[] {"http://thisIsMyUrl.com"});

    and then your java looking like this:

    public String getURL(Object newUrl) {
        return newUrl.ToString();
    }
    
    0 讨论(0)
  • 2021-01-07 04:47

    I suggest ditch the NDK. Use the Android java plugin.

    In YourPlugin.cs:

    using UnityEngine;
    using System.Collections;
    using System.IO;
    
    #if UNITY_ANDROID
    public class UnityUrlPlugin {
    
      // Logs the user out and invalidates the token
      public static string getUrl() {
        if( Application.platform != RuntimePlatform.Android )
          return;
    
        var pluginClass = new AndroidJavaClass("com.you.UnityUrlPlugin") ;
        AndroidJavaObject plugin = pluginClass.CallStatic<AndroidJavaObject>("instance");
        return plugin.Call<string>("getURL");
      } 
    }
    #endif
    

    Then, in UnityUrlPlugin.java:

    package com.you;
    
    import android.content.ContentValues;
    import android.content.Intent;
    import android.os.Environment;
    
    public class UnityUrlPlugin {
    
      private static UnityUrlPlugin m_instance;
    
      public static UnityUrlPlugin instance() {
        if(m_instance == null)
          m_instance = new UnityUrlPlugin();
        return m_instance;
      }
    
      private UnityUrlPlugin(){
      }
    
      public String getURL() {
        return "http://blah.com";
      }
    }
    

    And throw UnityUrlPlugin.jar in to Assets/Plugins/Android folder.

    No need for NDK!

    0 讨论(0)
提交回复
热议问题