美文网首页
IOS捕捉屏幕截图,并将图片传给Unity

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

作者: 小骄傲999 | 来源:发表于2021-10-18 16:52 被阅读0次

    在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;
                       }
        }
    

    相关文章

      网友评论

          本文标题:IOS捕捉屏幕截图,并将图片传给Unity

          本文链接:https://www.haomeiwen.com/subject/qsynoltx.html