IOS捕捉屏幕截图,并将图片传给Unity

2021-10-18  本文已影响0人  小骄傲999

在Xcode项目中找到UnityAppController+ViewHandling.h,在里面加入下面的代码

- (void)registNotification;

加好后,在找到UnityAppController+ViewHandling.mm,在里面加入下面的代码

- (void)registNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

- (void)getScreenshot:(NSNotification *)notification{
   NSLog(@"捕捉截屏事件");
   UIImage *shorImg =  [UIImage imageWithData:[self imageDataScreenShot]];
   NSData  *imageData = UIImagePNGRepresentation(shorImg);
   NSArray  *paths =  NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory ,  NSUserDomainMask ,  YES );
   NSString  *documentsDirectory = [paths objectAtIndex:0];
   NSString  *filePath = [documentsDirectory stringByAppendingPathComponent:@ "Test.jpg" ];  //Add the file name
   NSLog (@ "filePath %@" ,filePath);
    [imageData writeToFile:filePath atomically: YES ];
    UnitySendMessage("ShareMgr","ScreenshotsListener", [filePath UTF8String]);
}

- (NSData *)imageDataScreenShot
{
   CGSize imageSize = CGSizeZero;
   imageSize = [UIScreen mainScreen].bounds.size;

   UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
   CGContextRef context = UIGraphicsGetCurrentContext();
   for (UIWindow *window in [[UIApplication sharedApplication] windows])
   {
       CGContextSaveGState(context);
       CGContextTranslateCTM(context, window.center.x, window.center.y);
       CGContextConcatCTM(context, window.transform);
       CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
       if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
       {
           [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
       }
       else
       {
           [window.layer renderInContext:context];
       }
       CGContextRestoreGState(context);
   }

   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return UIImagePNGRepresentation(image);
}

Unity中通过路径将图片显示在游戏中

    public class ShareMgr : MonoBehaviour {
              void ScreenshotsListener(string ScreenshotsPath) {
                    StartCoroutine(LoadTexturePreview(@"file://" + path));
               }

              IEnumerator LoadTexturePreview(string path){
                    UnityWebRequest uwr = UnityWebRequest.GetTexture(path);
                    yield return uwr.Send();
                    Debug.Log("图片地址:" + path);
                    Debug.Log("图片地址:" + path); 
                    if (uwr.isError) { 
                            Debug.Log("错误" + uwr.error); 
                    } 
                    else {
                             Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
                              // 图片显示在按钮上
                             Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));                                                                        
                             m_ScreenShotImage.sprite = sprite;
                   }
    }
上一篇下一篇

猜你喜欢

热点阅读