是的...我以前从未在C#或Java中写过任何东西,所以我可能完全错了所有这些并且可能是一个简单的答案,但是这里有...
我有一个超简单的Unity应用程序,只需触发一个Android插件。该插件需要接受一个文本字符串和一个图像,它将做一种共享形式。
C#
using UnityEngine;
using System.Collections;
public class RunStuff : MonoBehaviour {
AndroidJavaClass androidClass;
AndroidJavaObject androidActivity;
void Start () {
Debug.Log ("----- UNTIY SCRIPT INIT ----- ");
var pathToImage = Application.streamingAssetsPath + "/meagain.jpg";
Debug.Log (pathToImage);
AndroidJNI.AttachCurrentThread ();
androidClass = new AndroidJavaClass("com.something.something.Plugin");
androidActivity = androidClass.GetStatic<AndroidJavaObject>("mContext");
androidActivity.Call("Plugin", pathToImage , "SOME TEST STRING");
Debug.Log ("----- UNTIY SCRIPT END ----- ");
}
}
爪哇
package com.something.something;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayerActivity;
public class Plugin extends UnityPlayerActivity {
public static Context mContext;
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
mContext = this;
}
public void Plugin(String imagePath, String message) {
Log.d("TAG", imagePath);
Log.d("TAG", message);
// HERE I NEED TO BE ABLE TO USE THE IMAGE
}
}
所有这一切都正常,就像我在java类中收到imagePath和消息一样......但是我根本无法从图像路径中获取图像。
图像位于StreamingAssets / meagain.jpg下的Unity项目中。如果我提取生成的apk,我可以看到该文件,但我的所有尝试都失败了!
有人有任何想法吗?!!
答案 0 :(得分:0)
Texture2D myTex;
string url = "file://"+Application.streamingAssetsPath + "/" + textureName;
WWW www = new WWW(url );
myTex = www.texture;
我只知道如何从streamignAssets加载东西。
答案 1 :(得分:0)
在您的Android java中,您可以使用类似
的内容 InputStream in = mContext.getAssets().open("meagain.jpg");
或者
InputStream in = Plugin.class.getClassLoader().getResourceAsStream("meagain.jpg");
两者都没有经过测试,但值得一试!
也进行了一些研究:
/* from your subroutine */
Bitmap newBitmap = getBitmapFromAsset(mContext, imagePath);
/* new function to get Bitmap From Asset */
public static Bitmap getBitmapFromAsset(Context context, String imagePath) {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open(imagePath);
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
// handle exception
}
return bitmap;
}
也不要忘记Bitmap,AssetManager,InputStream,BitmapFactory和IOException的导入。