Unigine代码截屏

这里采用的C#代码,同时采用按键T,按一下截一张。

这个是官方文档里面的方法,在这里记录以下备忘。
可以在场景中创建一个Node,然后把组件绑个它。组件的代码如下:

public class ScreenshotMaker : Component
{
 public enum Format
 {
  tga = 0,
  png,
  jpg
 }

 [ShowInEditor]
 private string namePrefix = "screenshot";

 [ShowInEditor]
 [ParameterSlider(Min = 1)]
 private int width = 640;

 [ShowInEditor]
 [ParameterSlider(Min = 1)]
 private int height = 480;

 [ShowInEditor]
 private Format format = Format.png;

 [ShowInEditor]
 private bool alphaChannel = false;

 private Image image = null;
 private Viewport viewport = null;
 private int count = 0;

 private void Init()
 {
  image = new Image();
  viewport = new Viewport();
  viewport.SkipFlags = Viewport.SKIP_VISUALIZER;

  Unigine.Console.Onscreen = true;
  Unigine.Console.OnscreenMessageLine(vec4.GREEN, "Screenshot component is initialized.");
 }

 private void Update()
 {
  if (Input.IsKeyDown(Input.KEY.T))
  {
   Player player = Game.Player;
   if (player == null)
   {
    Unigine.Console.OnscreenMessageLine(vec4.RED, "No active camera.");
    return;
   }

   Unigine.Console.OnscreenMessageLine(vec4.GREEN, "Trying to take a screenshot...");

   viewport.Mode = Render.ViewportMode;

   // We temporarily set exposure adaptation time to 0, otherwise the image may be too dark
   float exposureAdaptation = Render.ExposureAdaptation;
   Render.ExposureAdaptation = 0.0f;

   // We render with velocity buffer turned off to avoid temporal effects artifacts
   viewport.AppendSkipFlags(Viewport.SKIP_VELOCITY_BUFFER);
   viewport.RenderImage2D(player.Camera, image, width, height);
   viewport.RemoveSkipFlags(Viewport.SKIP_VELOCITY_BUFFER);

   Render.ExposureAdaptation = exposureAdaptation;

   if (!alphaChannel || format == Format.jpg)
   {
    if (image.Format == Image.FORMAT_RGBA8)
     image.ConvertToFormat(Image.FORMAT_RGB8);
    else if (image.Format == Image.FORMAT_RGBA16F)
     image.ConvertToFormat(Image.FORMAT_RGB16F);
   }

   string fullName = $"{namePrefix}_{count}.{format}";
   image.Save(fullName);
   Unigine.Console.OnscreenMessageLine(vec4.GREEN, $"{fullName} saved.");

   count++;
  }
 }
}

猜你喜欢

转载自blog.csdn.net/weixin_41487423/article/details/124638997