iOS 版本可以參考:http://lolikitty.pixnet.net/blog/post/193807692

本程式碼使用 Android Etcetera Plugin 插件,如果沒有的話可以到 Asset Store 購買 (這不是廣告啦)

Asset Store:https://www.assetstore.unity3d.com/en/#!/content/2741

該範例執行結果:

1. 一開始一片空白,我們必須點選選擇圖片按鈕

Screenshot_2014-09-22-11-41-35  
2. 按下按鈕後,會跳出你要開啟的App工具

Screenshot_2014-09-22-11-41-54
  3. 選擇好圖片後,就會在該物件上顯示貼圖啦~

Screenshot_2014-09-22-11-38-28  


程式碼:

using UnityEngine;
using System.Collections;
using Prime31;

    public class MyTest : MonoBehaviour {

    public GameObject testPlane; // 外部建立一個物件,並把他丟進來,之後會以該物件上貼圖

    #if UNITY_ANDROID

    void Start(){
        // 使用該插件之前都需要初始化
        EtceteraAndroid.initTTS();
    }


    void OnEnable(){
        // Listen to the texture loaded methods so we can load up the image on our plane
        EtceteraAndroidManager.albumChooserSucceededEvent += imageLoaded;
        EtceteraAndroidManager.photoChooserSucceededEvent += imageLoaded;
    }


    void OnDisable(){
        EtceteraAndroidManager.albumChooserSucceededEvent -= imageLoaded;
        EtceteraAndroidManager.photoChooserSucceededEvent -= imageLoaded;
    }


    void OnGUI(){
        if( GUI.Button(new Rect(0,0,200,100), "選擇圖片")){
            EtceteraAndroid.promptForPictureFromAlbum( "a" ); // 字串不能為空
        }
    }

    // 載入圖片
    public void imageLoaded(string imagePath){
        // 後面的 1f 代表解析度的意思,1 為最大
        EtceteraAndroid.scaleImageAtPath( imagePath, 1f );
        testPlane.renderer.material.mainTexture = EtceteraAndroid.textureFromFileAtPath( imagePath );
    }

    #endif

}





arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(6) 人氣()