Unity Android Gallery

时间:2014-12-08 11:09:13

标签: c# android unity3d android-gallery

Android开发的新手,所以希望some1可以启发我。得到了相当大的谷歌搜索,但仍然卡住了。我正在使用Unity 4和Eclipse,尝试显示图像库并检索图像的Uri。 (注意:我正在使用Prime31的Android社交网络插件)

起初,我没有扩展UnityPlayerActivity,但是我意识到如果我不扩展它,我就无法获得onActivityResult。 在扩展之后,我得到“无法在未调用Looper.prepare()的线程内创建处理程序”。尝试runonuithread并没有工作,但可能是由于我使用它的经验。所以希望你们能帮忙。

以下是我在Eclipse中所做的:

public class AndroidGalleryPlugin extends UnityPlayerActivity{

public static AndroidGalleryPlugin plugin_instance;
public Uri ImageURI;
static final int REQUEST_CODE = 1;

static Context _context;

public static AndroidGalleryPlugin instance ()
{
    if(plugin_instance == null)
        plugin_instance = new AndroidGalleryPlugin();
    return plugin_instance;
}

private AndroidGalleryPlugin () {}

public static void StartOpenGallery (Context mContext)
{
    Log.d("AndroidGalleryPlugin", "Start Open Gallery");
    _context = mContext;
    Intent intent = new Intent ();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    Activity activity = (Activity) _context;
    activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("AndroidGalleryPlugin", "onActivityResult");
}

// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {

    Log.d("AndroidGalleryPlugin", "Get URI");
}

}

Unity中的代码:

jc = new AndroidJavaClass ("com.asd.androidgallery.AndroidGallery");
jo = jc.CallStatic <AndroidJavaObject> ("instance");

if(jo == null)
{
    Debug.LogError ("Java Obj Null");
    return false;
}

unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityObj = unityClass.GetStatic<AndroidJavaObject>("currentActivity");

if(unityObj == null)
{
    Debug.LogError ("Unity Obj Null");
    return false;
}

jo.CallStatic("StartOpenGallery", unityObj);

这是我的androidmanifest:

<activity android:name="com.prime31.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="sensorPortrait">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
  <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>

 <activity android:name="com.asd.androidgallery.AndroidGalleryPlugin" android:configChanges="orientation"></activity>

2 个答案:

答案 0 :(得分:1)

以下是一些更改,首先是您的java代码

public class AndroidGalleryPlugin extends UnityPlayerActivity{

public Uri ImageURI;
static final int REQUEST_CODE = 1;
private Activity _activity;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    _activity = UnityPlayer.currentActivity;
}

public static void StartOpenGallery (Context mContext)
{
    Log.d("AndroidGalleryPlugin", "Start Open Gallery");
    _context = mContext;
    Intent intent = new Intent ();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    _activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("AndroidGalleryPlugin", "onActivityResult");
}

// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {

    Log.d("AndroidGalleryPlugin", "Get URI");
}

添加了onCreate方法,我从那里获得了Unity当前的活动。此类构造函数方法对于您的类也是不必要的,因为它是一个活动。

对于Unity,因为你要覆盖你的活动,你只需要让你获得你的Unityplayer课而不是实例!只需使用此代码。

unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityObj = unityClass.GetStatic<AndroidJavaObject>("currentActivity");

if(unityObj == null)
{
    Debug.LogError ("Unity Obj Null");
    return false;
}

unityObj.CallStatic("StartOpenGallery");

答案 1 :(得分:0)

只需使用“Android Native Gallery项目选择器”插件。它可以从您的Android手机或Android图库中选择单个或多个图像和视频。

Click here for Android Native Gallery Item Picker