Unity3D,获取相册图片,手机拍照

本文介绍了如何在Unity3D项目中获取相册图片和使用手机拍照功能。在Unity端,创建场景并添加UI组件,编写脚本以显示和压缩图片。在iOS端,通过Objective-C实现功能,包括图片的读取、压缩和保存。同时讨论了在不同类之间共享数据的方法以及避免特殊字符在文件名中的问题。
摘要由CSDN通过智能技术生成

Unity端

  • 先新建个项目吧,添加一个Button和两个UIimage吧。之后我们提取的相片会在UIimage上显示。
    至于为什么要两个,主要是一会我还要进行图片的压缩。然后把小图也显示出来。
    这里写图片描述
  • 新建个脚本,代码如下:
    public void GetBigImage(string filename){
        string path="file://"+platformPath+filename;
        Debug.Log (path);
        StartCoroutine (loadImage(path,true));
    }

    public void GetSmallImage(string filename){
        string path="file://"+platformPath+filename;
        Debug.Log (path);
        StartCoroutine (loadImage(path,false));
    }


    public void OnClickButton(){
        if(Application.platform != RuntimePlatform.OSXEditor){
            _GetImage();
        }
    }

    public string platformPath{
        get{
            string path=null;
            if(Application.platform==RuntimePlatform.IPhonePlayer)//判断平台
            {
                path= Application.persistentDataPath.Substring (0, Application.persistentDataPath.Length - 5);//ios 平台 就会获取documents路径
                path = path.Substring(0, path.LastIndexOf('/'))+"/Documents/";    
            }
            else
            {
                path=Application.dataPath+"/GameData/";//pc平台 获取当前工程GameData/的路径    GameData需要自己新建
            }
            return path;
        }   
    }

    IEnumerator loadImage(string path,bool isBig){
        WWW www = new WWW(path);
        yield return www;
        if (www.isDone && www.error == null) {
            if(isBig){
                Big_Spr = Sprite.Create (www.texture, new Rect (0, 0, www.texture.width, www.texture.height), new Vector2 (0, 0));
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值