This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

在运行时制作截图

This article provides the code sufficient to create a screenshot making component configurable via Editor and making screenshots at runtime.本文提供了足够的代码来创建一个屏幕截图,使组件可以通过Editor进行配置,并在运行时制作屏幕截图。

Component Code
组件代码#

Create a component in the Editor and insert this code:在编辑器中创建一个组件并插入以下代码:

源代码 (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class ScreenshotMaker : Component
{
	public enum Format
	{
		tga = 0,
		png,
		jpg
	}

	public enum State
	{
		STATE_START = 0,
		STATE_WARMUP,
		STATE_SAVE,
		STATE_DONE,
	};

	[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 Texture texture = null;
	private Viewport viewport = null;
	private int count = 0;

	private State state = State.STATE_DONE;
	const int warmup_frames = 30;
	int warm_up_count = 0;

	private void Init()
	{
		texture = new Texture();
		viewport = new Viewport();
		viewport.SkipFlags = Viewport.SKIP_VISUALIZER;

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

	private void Update()
	{
		Player player = Game.Player;

		if (player == null)
		{
			state = State.STATE_DONE;
			Unigine.Console.OnscreenMessageLine(vec4.RED, "No active camera.");
			return;
		}

		if (state == State.STATE_DONE && Input.IsKeyDown(Input.KEY.T))
		{
			warm_up_count = 0;
			state = State.STATE_WARMUP;
			Unigine.Console.OnscreenMessageLine(vec4.GREEN, "Trying to take a screenshot...");
		}

		if (state == State.STATE_WARMUP)
		{
			viewport.Mode = Render.ViewportMode;

			if (warm_up_count == 0)
			{
				// First frame we render with velocity buffer turned off to avoid temporal effects artifacts
				viewport.AppendSkipFlags(Viewport.SKIP_VELOCITY_BUFFER);
				viewport.RenderTexture2D(player.Camera, texture, width, height);
				viewport.RemoveSkipFlags(Viewport.SKIP_VELOCITY_BUFFER);
			}
			else
			{
				// We temporarily set exposure adaptation time to 0, otherwise the image may be too dark
				float exposureAdaptation = Render.ExposureAdaptation;
				Render.ExposureAdaptation = 0.0f;
				viewport.RenderTexture2D(player.Camera, texture, width, height);
				Render.ExposureAdaptation = exposureAdaptation;
			}
			
			warm_up_count++;
			if (warm_up_count == warmup_frames)
				state = State.STATE_SAVE;

			if (state == State.STATE_SAVE)
			{	
				Render.AsyncTransferTextureToImage(
					null,
					(Image image) =>
						{
							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);
							}

							if (!Render.IsFlipped)
								image.FlipY();

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

						},
				texture);

				count++;
				state = State.STATE_DONE;
			}
		}
	}
}

Making Screenshots
使屏幕截图#

Now you have a component in your project. To be able to make screenshots, you need to do the following:现在您的项目中有了一个组件。为了能够截图,您需要做以下操作:

  1. Assign the ScreenshotMaker component to any node in the Editor (or create a Node Dummy and assign the component to it). ScreenshotMaker组件分配给编辑器中的任何节点(或创建一个Node Dummy并将组件分配给它)。

    Component assigned in the Editor

  2. Adjust settings as necessary.根据需要调整设置。

    Name Prefix The prefix used in the file name.文件名中使用的前缀。
    Width Width of the saved screenshot image.保存的截图图像的宽度。
    Height Height of the saved screenshot image.保存的截图图像的高度。
    Format Format of the saved screenshot image.保存的截图格式。
    Alpha Channel If enabled, transparent areas are cut off.如果启用,透明区域将被切断。
  3. Run your app and take screenshots.运行应用程序并截图。
  4. Check the data/ folder of your project.检查项目的data/文件夹。
最新更新: 2024-04-19
Build: ()