马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册帐号
x
额(⊙o⊙)…接着以前的文章//www.sl6cl.com.cn/thread-89330-1-1.html继续,这是是iOS的
Unity代码
[C#] 纯文本查看 复制代码 using System;
using System.IO;
#if UNITY_IOS
using System.Runtime.InteropServices;
#endif
using UnityEngine;
public class UnityPlugins : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void _SavePhoto(string readAddr);
#endif
string path = "";
private void OnGUI()
{
if (GUILayout.Button("TakePhoto", GUILayout.Height(300), GUILayout.Width(300)))
{
CaptureCamera();
Debug.Log("TakePhoto");
}
}
void CaptureCamera()
{
Camera camera = Camera.main;
string name = DateTime.Now.ToFileTime().ToString();
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 0);
Texture2D frame = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
frame.ReadPixels(rect, 0, 0);
frame.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
byte [] bytes = frame.EncodeToJPG();
File.WriteAllBytes(Application.persistentDataPath + "/" + name + ".jpg",bytes);
#if UNITY_IOS
_SavePhoto(Application.persistentDataPath + "/" + name + ".jpg");
#endif
}
}
iOS代码
[AppleScript] 纯文本查看 复制代码 #import <Foundation/Foundation.h>
@interface UnityPlugins : NSObject
- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error
contextInfo: ( void *) contextInfo;
@end
[AppleScript] 纯文本查看 复制代码 #import "UnityPlugins.h"
@implementation UnityPlugins
- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error
contextInfo: ( void *) contextInfo
{
NSLog(@"保存结束");
if (error != nil) {
NSLog(@"有错误");
}
}
void _SavePhoto(char *readAddr)
{
NSString *strReadAddr = [NSString stringWithUTF8String:readAddr];
UIImage *img = [UIImage imageWithContentsOfFile:strReadAddr];
NSLog([NSString stringWithFormat:@"w:%f, h:%f", img.size.width, img.size.height]);
UnityPlugins *instance = [UnityPlugins alloc];
UIImageWriteToSavedPhotosAlbum(img, instance,
@selector(imageSaved:didFinishSavingWithError:contextInfo:), nil);
}
@end
目录结构
导出Xcode工程后或者 修改Info.plist 打开 Info.plist,点击 + 号,在 Key 中输入:Privacy - Photo Library Additions Usage Description,Type 选择 String,Value 中输入你的提示语(比如XXX访问相册)再次 Build
|