相對來講,IOS 的實現比Android要簡單得多,可是重點是IOS很差測試,要macOS,要相應版本的xcode,建議你們不要搞虛擬機了,我搞了兩天,頭髮一把一把的拖,最後直接放棄ios
找公司的IOS配合或者看誰有mac的借來用用都比虛擬機強,話很少說,言歸正傳xcode
Unity調用IOS 的相冊只須要在Unity的Plugins-IOS文件夾裏放兩個文本文件,而後改後綴,一個改爲.h 一個改爲.m 名字同樣不要緊,咱後綴不同,一個是C++ 一個OCapp
.h是頭文件 至關於引入 接口 直接複製一下代碼函數
1 #import<QuartzCore/CADisplayLink.h> 2 @interface IOSCameraController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverPresentationControllerDelegate> 3 @end
.m是實現文件,功能在這寫測試
1 #import "IOSCameraController.h" 2 3 @implementation IOSCameraController 4 -(void)OpenCamera:(UIImagePickerControllerSourceType)type{ 5 //建立UIImagePickerController實例 6 UIImagePickerController *picker; 7 picker= [[UIImagePickerController alloc]init]; 8 //設置代理 9 picker.delegate = self; 10 //是否容許編輯 (默認爲NO) 11 picker.allowsEditing = YES; 12 //設置照片的來源 13 picker.sourceType = type; 14 //展現選取照片控制器 15 if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary &&[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 16 picker.modalPresentationStyle = UIModalPresentationPopover; 17 UIPopoverPresentationController *popover = picker.popoverPresentationController; 18 //picker.preferredContentSize = [UIScreen mainScreen].bounds.size; 19 popover.delegate = self; 20 popover.sourceRect = CGRectMake(0, 0, 0, 0); 21 popover.sourceView = self.view; 22 popover.permittedArrowDirections = UIPopoverArrowDirectionAny; 23 [self presentViewController:picker animated:YES completion:nil]; 24 } else { 25 [self presentViewController:picker animated:YES completion:^{}]; 26 } 27 28 } 29 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 30 [picker dismissViewControllerAnimated:YES completion:^{}]; 31 UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 32 if (image == nil) { 33 image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 34 } 35 //圖片旋轉 36 if (image.imageOrientation != UIImageOrientationUp) { 37 //圖片旋轉 38 image = [self fixOrientation:image]; 39 } 40 NSString *imagePath = [self GetSavePath:@"Temp.jpg"]; 41 [self SaveFileToDoc:image path:imagePath]; 42 } 43 -(NSString*)GetSavePath:(NSString *)filename{ 44 NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 45 NSString *docPath = [pathArray objectAtIndex:0]; 46 return [docPath stringByAppendingPathComponent:filename]; 47 } 48 -(void)SaveFileToDoc:(UIImage *)image path:(NSString *)path{ 49 NSData *data; 50 if (UIImagePNGRepresentation(image)==nil) { 51 data = UIImageJPEGRepresentation(image, 1); 52 }else{ 53 data = UIImagePNGRepresentation(image); 54 } 55 [data writeToFile:path atomically:YES]; 56 UnitySendMessage("Canvas", "Message", "Temp.jpg"); 57 } 58 #pragma mark 圖片處理方法 59 //圖片旋轉處理 60 - (UIImage *)fixOrientation:(UIImage *)aImage { 61 CGAffineTransform transform = CGAffineTransformIdentity; 62 63 switch (aImage.imageOrientation) { 64 case UIImageOrientationDown: 65 case UIImageOrientationDownMirrored: 66 transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); 67 transform = CGAffineTransformRotate(transform, M_PI); 68 break; 69 70 case UIImageOrientationLeft: 71 case UIImageOrientationLeftMirrored: 72 transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); 73 transform = CGAffineTransformRotate(transform, M_PI_2); 74 break; 75 76 case UIImageOrientationRight: 77 case UIImageOrientationRightMirrored: 78 transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); 79 transform = CGAffineTransformRotate(transform, -M_PI_2); 80 break; 81 default: 82 break; 83 } 84 85 switch (aImage.imageOrientation) { 86 case UIImageOrientationUpMirrored: 87 case UIImageOrientationDownMirrored: 88 transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); 89 transform = CGAffineTransformScale(transform, -1, 1); 90 break; 91 92 case UIImageOrientationLeftMirrored: 93 case UIImageOrientationRightMirrored: 94 transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); 95 transform = CGAffineTransformScale(transform, -1, 1); 96 break; 97 default: 98 break; 99 } 100 101 // Now we draw the underlying CGImage into a new context, applying the transform 102 // calculated above. 103 CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, 104 CGImageGetBitsPerComponent(aImage.CGImage), 0, 105 CGImageGetColorSpace(aImage.CGImage), 106 CGImageGetBitmapInfo(aImage.CGImage)); 107 CGContextConcatCTM(ctx, transform); 108 switch (aImage.imageOrientation) { 109 case UIImageOrientationLeft: 110 case UIImageOrientationLeftMirrored: 111 case UIImageOrientationRight: 112 case UIImageOrientationRightMirrored: 113 // Grr... 114 CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); 115 break; 116 117 default: 118 CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage); 119 break; 120 } 121 // And now we just create a new UIImage from the drawing context 122 CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 123 UIImage *img = [UIImage imageWithCGImage:cgimg]; 124 CGContextRelease(ctx); 125 CGImageRelease(cgimg); 126 return img; 127 } 128 @end 129 #if defined(__cplusplus) 130 extern "C" { 131 #endif 132 void IOS_OpenCamera(){ 133 IOSCameraController *app = [[IOSCameraController alloc]init]; 134 UIViewController *vc = UnityGetGLViewController(); 135 [vc.view addSubview:app.view]; 136 [app OpenCamera:UIImagePickerControllerSourceTypeCamera]; 137 } 138 void IOS_OpenAlbum(){ 139 IOSCameraController *app = [[IOSCameraController alloc]init]; 140 UIViewController *vc = UnityGetGLViewController(); 141 [vc.view addSubview:app.view]; 142 [app OpenCamera:UIImagePickerControllerSourceTypePhotoLibrary]; 143 } 144 #if defined(__cplusplus) 145 } 146 #endif
IOS就是這麼簡單粗暴atom
而後測試腳本,重點看怎麼調用spa
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 using System.IO; 6 using System.Runtime.InteropServices; 7 using UnityEngine.SceneManagement; 8 9 public class TestWWWTex : MonoBehaviour { 10 11 [SerializeField] private Button _openCamera; //打開相機按鈕 12 [SerializeField] private Button _openAlbum; //打開相冊按鈕 13 [SerializeField] private RawImage _image; //用於顯示的圖片 14 //引入在oc中定義的那兩個方法 15 [DllImport("__Internal")] 16 private static extern void IOS_OpenCamera(); 17 [DllImport("__Internal")] 18 private static extern void IOS_OpenAlbum(); 19 20 void Awake() 21 { 22 //爲兩個button添加點擊事件 23 _openCamera.onClick.AddListener(IOS_OpenCamera); 24 _openAlbum.onClick.AddListener(IOS_OpenAlbum); 25 } 26 27 //ios回調unity的函數 28 void Message(string filenName) 29 { 30 31 32 string filePath = Application.persistentDataPath + "/" + filenName; 33 34 35 TestFileFunc(filePath); 36 } 37 38 void TestFileFunc(string path) 39 { 40 FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); 41 fileStream.Seek(0, SeekOrigin.Begin); 42 byte[] bye = new byte[fileStream.Length]; 43 fileStream.Read(bye, 0, bye.Length); 44 fileStream.Close(); 45 46 Texture2D texture2D = new Texture2D(240, 144); 47 texture2D.LoadImage(bye); 48 49 _image.texture = texture2D; 50 51 } 52 }
這裏說一下,www加載會找不到照片,http加載,xcode會報錯,看不懂報了什麼錯,好像是沒給權限,xcode建議用https,我就懶得搞了,直接用文件流加載吧代理
接下來就是打xcode工程 在xcode裏找到info.plist文件,加一個相冊權限,以後就能真機測試了code