Unity-Android的配置、整合、匯出[Mac]

掌心朝下發表於2016-08-23

1.mac下安裝配置Android SDK

[1]安裝java jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
[2]sdk安裝詳見:http://www.cnblogs.com/yjmyzz/p/4219829.html
[3]安裝android studio IDE

2.整合Android第三方sdk或使用原生功能並匯出apk

[1]新增unity的class.jar檔案,詳見:http://www.narkii.com/club/thread-320335-1.html
[2]unity-classes Add To Library
[3]Android Studio 打Jar包見:http://blog.csdn.net/ta893115871/article/details/46955791/
[4]Jar包生成目錄:

./mylibrary/build/intermediates/bundles/debug/classes.jar

[5]儲存圖片至android相簿
java程式碼:

 // 儲存照片至相簿
    public static void savePng(final String fileName)
    {
        context = mainActivity.getApplicationContext();

        mainActivity.runOnUiThread(new Runnable() {
            public void run() {

                Bitmap bitmap = BitmapFactory.decodeFile(Environment
                        .getExternalStorageDirectory()
                        + "/Android/data/com.ztgame.snake/files/"
                        + fileName
                        + ".png");

                File file = new File(Environment.getExternalStorageDirectory()
                        + "/DCIM/Camera", fileName + ".jpg");

                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    Log.w("cat", e.toString());
                }
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                try {
                    fos.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.w("cat", e.toString());
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.w("cat", e.toString());
                }
                bitmap.recycle();//掃描儲存的圖片
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" +Environment.getExternalStorageDirectory()
                        + "/DCIM/Camera/"+fileName + ".jpg")));

                Toast.makeText(context, "照片已儲存到相簿", Toast.LENGTH_SHORT).show();

            }
        });
    }

c#部分:

//先儲存至本地目錄
string name = "name";
string writePath = Application.persistentDataPath + "/" + name + ".png";
File.WriteAllBytes(writePath, b);
//呼叫原生程式碼複製到相簿
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.CallStatic("savePng", name);

[6]將文字複製到剪貼簿
java部分:

    // 向剪貼簿中新增文字
    public static void copyTextToClipboard(final String str) throws Exception {
        context = mainActivity.getApplicationContext();
        if (Looper.myLooper() == null){
            Looper.prepare();
        }
        clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);

        ClipData textCd = ClipData.newPlainText("data", str);
        clipboard.setPrimaryClip(textCd);
        mainActivity.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(context, "連結已複製到剪貼簿", Toast.LENGTH_SHORT).show();
            }
        });

    }

c#部分:

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.CallStatic("copyTextToClipboard", input);

[7]安裝AndroidPlayer擴充套件即可匯出

3.特別注意

[1]Android工程包名要和unity設定中保持一致
[2]如果橫豎屏不能在unity設定中滿足需求,可以在程式碼中強制調整
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

相關文章