简体   繁体   English

在iOS上为Unity / COCOS / SpritKit游戏截图

[英]Take a screenshot on ios for Unity/COCOS/SpritKit games

  • I want to develop an IOS SDK for Unity/COCOS/SpritKit game app to record video.Main program is to synthetic video after screenshots . 我想为Unity / COCOS / SpritKit游戏应用开发IOS SDK来录制视频。主程序是截图后的合成视频。

  • Now I do the test with IOS project from unity game on IOS 7.0 device and try to use Opengl function glreadpiexls () to read pixel buffer, but always got the black image which troubled me for weeks. 现在,我在IOS 7.0设备上对来自统一游戏的IOS项目进行了测试,并尝试使用Opengl函数glreadpiexls ()读取像素缓冲区,但始终出现困扰我数周的黑色图像。 I try some solutions like to set the context and set the EAGLView drawing property before glreadpiexls() but not work. 我尝试了一些解决方案,例如在glreadpiexls()之前设置上下文并设置EAGLView绘图属性,但是不起作用。 And some suggest to make sure to execute glreadpiexls() before presentRenderbuffer, but how . 有人建议确保在presentRenderbuffer之前执行glreadpiexls(),但是要如何执行。 I can not modify the code from Unity. 我无法从Unity修改代码。

  • Solutions like IOSuface depreciated on iOS 9.0 and seems only for UIView elements and IOSurface was depreciated on ios 9.0 and seems only for UIView elements. 诸如IOSuface之类的解决方案在iOS 9.0上已贬值,似乎仅适用于UIView元素,而IOSurface在ios 9.0上已贬值,且仅适用于UIView元素。 RenderTexture or Application.CaptureScreenshot are only for unity project, not for my IOS SDK. RenderTexture或Application.CaptureScreenshot仅用于统一项目,不适用于我的IOS SDK。

So 所以

So can anyone give me some suggestions for the screenshot on iOS for Unity/COCOS/SpritKit games, and this is my current code. 所以任何人都可以给我一些有关Unity / COCOS / SpritKit游戏的iOS屏幕截图的建议,这是我当前的代码。

int w = 320;//size.width;//viewport[2];//
int h = 480;//size.height;//viewport[3];//
NSInteger myDataLength = w * h * 4;    

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);

glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); glReadPixels(0,0,w,h,GL_RGBA,GL_UNSIGNED_BYTE,缓冲区);

GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
GLubyte temp = 0;
int index1 = 0;
int index2 = 0;
for(int y = 0; y < h - 1 - y; y++) {  // 交换
    index1 = y * 4 * w;
    index2 = (h -1 - y) * w * 4;
    for(int x = 0; x <w * 4; x++) {
        temp = buffer[index1 + x];
        buffer[index1 + x] = buffer[index2 + x];
        buffer[index2 + x] = temp;
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, NULL);


// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * w;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
// make the cgimage
CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

// myImage is alway nil when debug
UIImage *myImage = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ];    
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);

And some suggest to make sure to execute glreadpiexls() before presentRenderbuffer, but how . 有人建议确保在presentRenderbuffer之前执行glreadpiexls(),但是要如何执行。 I can not modify the code from Unity. 我无法从Unity修改代码。

What they are saying is that you should convert that piece of code into a simple function. 他们在说什么,您应该将这段代码转换为一个简单的函数。 For example void takeScreenShot(){//your code here} then convert it into a plugin. 例如void takeScreenShot(){//your code here}然后将其转换为插件。 Here is how to do that. 是这样做的方法。

After converting into a plugin, create a simple script in Unity and name it UnityCam . 转换为插件后,在Unity中创建一个简单脚本并将其命名为UnityCam Attach this script to the main Camera in Unity. 将此脚本附加到Unity中的主Camera Inside the Unity cam script, call the takeScreenShot() that is in your iOS plugin. 在Unity cam脚本中,调用iOS插件中的takeScreenShot() You should ONLY call it from the OnPostRender() callback function not from Start() or Update() function. 只能OnPostRender()回调函数而不是 Start()Update()函数调用它。

If that doesn't work, there is a new hidden and undocumented Unity API for recording Video on iOS. 如果这不起作用,则存在一个新的隐藏且未记录的 Unity API,用于在iOS上录制视频。 Download Unity 5.4 first, then use the code below to record video on iOS. 首先下载Unity 5.4 ,然后使用下面的代码在iOS上录制视频。 Unity is working on recording for Android too but since this question is about iOS , the code below should do it. Unity也正在为Android录音,但是由于这个问题是关于iOS的 ,因此下面的代码应该可以做到。 Note that it will only work on from iOS 9 and above. 请注意,它仅适用于iOS 9及更高版本。

using UnityEngine.Apple.ReplayKit;

void startRecordingOnIOS()
{
    if (ReplayKit.APIAvailable)
    {
        if (ReplayKit.isRecording)
        {
            ReplayKit.StopRecording();
        }
        ReplayKit.StartRecording(true);//True to use mic, false not not use mic
    }
    else
    {
        //Fallback. Use your code from your question()
    }
}

void stopRecordingOnIOS()
{
    if (ReplayKit.APIAvailable)
    {
        if (ReplayKit.isRecording)
        {
            ReplayKit.StopRecording();
        }
    }
    else
    {
        //Fallback. Use your code from your question()
    }
}

void viewRecordedVideoOnIOS()
{
    if (ReplayKit.APIAvailable)
    {
        if (ReplayKit.recordingAvailable)
        {
            ReplayKit.Preview();
        }
    }
    else
    {
        //Fallback. Use your code from your question()
    }
}

void discardRecordedVideoOnIOS()
{
    if (ReplayKit.APIAvailable)
    {
        if (ReplayKit.isRecording)
        {
            ReplayKit.Discard();
        }
    }
    else
    {
        //Fallback. Use your code from your question()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM