Unity3D调用Android系统相册

本文介绍了一个基于Unity和Android的跨平台图片选择功能实现方法。通过Android代码实现图片选择,并将选中的图片路径传递给Unity,Unity再进行图片加载和显示。涉及Android权限设置、图片路径获取、Unity与Android间消息传递等关键技术。
摘要由CSDN通过智能技术生成

Android代码:

public class MainActivity extends UnityPlayerActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int PHOTO_REQUEST_CODE = 1;
    private static final boolean DEBUG = false;
    private String gameobjectName = "Main Camera"; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void OpenGallery()
    {
       Intent intent = new Intent(Intent.ACTION_PICK,null);
       intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
       startActivityForResult(intent, PHOTO_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
       super.onActivityResult(requestCode, resultCode, data);

       if(PHOTO_REQUEST_CODE == requestCode){
           Uri uri = data.getData();
           String imagePath = getImagePath(uri);
           if(DEBUG){
               Log.d(TAG, imagePath);
           }
           UnityPlayer.UnitySendMessage(gameobjectName, "GetImagePath", imagePath);
       }
    }

    //获取到图片的地址
    private String getImagePath(Uri uri)
    {
        if(null == uri) return null;
        String path = null;
        final String scheme = uri.getScheme();
        if (null == scheme) {
            path = uri.getPath();
        } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            path = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(uri, proj, null, null,
                    null);
            int nPhotoColumn = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (null != cursor) {
                cursor.moveToFirst();
                path = cursor.getString(nPhotoColumn);
            }
            cursor.close();
        }
        return path;
    }
}

AndroidManifest.xml中添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Unity代码:

public class PhotoTest : MonoBehaviour {

    public Button mOpenPhoto;
    public Image mImage;
    public Text mLog;

    private void Start()
    {
        mOpenPhoto.onClick.AddListener(OpenPhoto);
    }

    //接通Android
    private void OpenPhoto()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("OpenGallery");       
    }

    //获取到图片的地址
    public void GetImagePath(string imagePath)
    { 
        if (imagePath == null)
            return;
        mLog.text = imagePath;
        StartCoroutine("LoadImage",imagePath);
    }

    //加载纹理
    private IEnumerator LoadImage(string imagePath)
    {
        WWW www = new WWW ("file://"+imagePath);
        mLog.text = mLog.text + "\n www开始加载";
        yield return www;
        mLog.text = mLog.text + "\n www加载完成";
        if (www.error == null) 
            StartCoroutine ("UpdataImage", www.texture);    
        else
            mLog.text = mLog.text + "\n"+www.error; 
    }

    //纹理转化为精灵
    private IEnumerator UpdataImage(Texture2D texture)
    {
        mLog.text = mLog.text + "\n 开始转化为精灵";
        Sprite sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        mImage.sprite = sprite;
        mLog.text = mLog.text + "\n 转换结束";
        yield return new WaitForSeconds(0.01f);
        Resources.UnloadUnusedAssets();
    }
}

下载地址:http://download.csdn.net/detail/piratecode/9661589

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值