unity游戏内拍照保存

public class CaptureScreenshotMgr : Utility.Singleton
{
#if UNITY_IPHONE     //与调用ios 里面的保存相册接口
    [DllImport("__Internal")]
    private static extern void SavePhotoAlbum(string path);
#endif
    private Texture2D screenShot;
    private static string _TextureUrl;
    public static string TextureUrl
    {
        set { _TextureUrl = value; }
        get
        {
            if (_TextureUrl == null)
            {
                // "/sdcard/Pictures/Screenshots";
#if UNITY_ANDROID && !UNITY_EDITOR
                _TextureUrl=Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"))+"/DCIM/Screenshots";
#elif UNITY_IPHONE && !UNITY_EDITOR
                _TextureUrl = Application.persistentDataPath+"/Screenshots";
#else
                _TextureUrl = "Screenshots";
#endif
                System.IO.Directory.CreateDirectory(_TextureUrl);
            }
            return _TextureUrl;
        }
    }

    public static string GetFileNameToTime
    {
        get
        {
            return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
            + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".png";
        }
    }

    public void ScreenShotStart(GameObject imgObj)
    {
        UnityEngine.Camera mCamera = GlobalData.sceneCamera;
        Rect mRect = new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
        //初始化RenderTexture
        RenderTexture mRender = RenderTexture.GetTemporary((int)mRect.width, (int)mRect.height, 24);
        //设置相机的渲染目标
        mCamera.targetTexture = mRender;
        //开始渲染
        mCamera.Render();
        //激活渲染贴图读取信息
        RenderTexture.active = mRender;
        screenShot = new Texture2D((int)mRect.width, (int)mRect.height, TextureFormat.RGB24, false);
        //读取屏幕像素信息并存储为纹理数据
        screenShot.ReadPixels(mRect, 0, 0);
        //应用
        screenShot.Apply();
        //释放相机,销毁渲染贴图
        mCamera.targetTexture = null;
        RenderTexture.active = null;
        Image RI = imgObj.GetComponent();
        if (RI == null)
        {
            RI = imgObj.AddComponent();
        }
        Sprite markTemp = Sprite.Create(screenShot, new Rect(0,0,screenShot.width, screenShot.height), Vector2.zero);
        RI.sprite = markTemp;
        GameObject.Destroy(mRender);
        //Framework.Instance.StartCoroutine(CaptureByCamera(imgObj));
    }

    public void ScreenShotSave()
    {
        string fileName = TextureUrl + "/" + GetFileNameToTime;
        byte[] bytes = screenShot.EncodeToPNG();
        System.IO.File.WriteAllBytes(fileName, bytes);
#if UNITY_ANDROID && !UNITY_EDITOR
        CommonSDKLogic.Instance.CallStatic(CommonSDKLogic.feedback_className, "scanFile", fileName);
#elif UNITY_IPHONE && !UNITY_EDITOR
        SavePhotoAlbum(fileName);
#endif
    }
}

 

//ios   .mm文件

    //将图片保存相册
    void SavePhotoAlbum(char *path)
    {
        NSString *readAddr = [NSString stringWithUTF8String: path];
        UIImage *img = [UIImage imageWithContentsOfFile:readAddr];
           UIImageWriteToSavedPhotosAlbum(img, nil,nil, nil);
    }

//Android

public void scanFile(String filePath) {
    final Activity activity = UnityPlayer.currentActivity;
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    scanIntent.setData(Uri.fromFile(new File(filePath)));
    activity.sendBroadcast(scanIntent);
}

你可能感兴趣的:(U3D)