2.3 从屏幕内容获取图片(快速截屏)Unity
Unity技术Thepoly 14415 0
实名

通过了实名认证的内容创造者

发布于 2022-3-24 18:06:37

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 Thepoly 于 2022-3-24 20:06 编辑

src=http___xqimg.imedao.com_17b70f518351426d3fd73b36.png!800.jpgrefer=http___xqimg.imedao.webp.jpg
174551k9r66ex7z719e99h.jpg
2.3 从屏幕内容获取图片(快速截屏)
Hello,大家好。
今天由到Kinglary分享
游戏引擎相关内容

2.3 从屏幕内容获取图片(快速截屏)
2.3 Making textures from screen content

If you want your game or player to take in-game snapshots and apply it as a texture, this recipe will show you how. This can be very useful if you plan to implement an in-game photo gallery or display a snapshot of a past key moment at the end of a level (Race Games and Stunt Sims use this feature a lot).
如果你想你的游戏或是玩家在游戏中获得快速截图,并将其保存为图片,在本节内容中将会展示如何去做。如果你想实现一个游戏截图画廊或是在通关后按下快捷键截图进行展示,这将非常有用(在竞速游戏和模拟游戏中这个功能会经常被用到)。
Getting ready(准备工作)
In order to follow this recipe, please import the  basicTerrain package, available in the 0423_02_04_05 folder, into your project. The package includes a basic terrain and a camera that can be rotated via a mouse.
为了完成本章节内容,请导入basicTerrain包,该包位于0423_02_04_05文件夹中,导入到你的项目里。这个包包括一个基本的地形和一个可以通过镜头进行旋转的摄像机。
How to do it...(如何去做)
To create textures from screen content, follow these steps:
创建屏幕截图,需要遵循以下步骤:

1. Import the Unity package and open the  02_04_05 scene.
1. 导入Unity package,并打开02_04_05场景。

2. We need to create a script. In the Project view, click on the Create drop-down menuand choose C# Script. Rename it ScreenTexture and open it in your editor.
2. 我们需要创建一个脚本,在项目浏览器中,点击Create,在下拉菜单中选择 C# Scrip,将其重命名为ScreenTexture,并在编译器中打开脚本。

3. Open your script and replace everything with the following code:
3. 打开你的脚本,并替换成以下代码:
using UnityEngine;
using System.Collections;
public class ScreenTexture : MonoBehaviour {
public int photoWidth = 50;
public int photoHeight = 50;
public int thumbProportion = 25;
public Color borderColor = Color.white;
public int borderWidth = 2;
private Texture2D texture;
private Texture2D border;
private int screenWidth;
private int screenHeight;
private int frameWidth;
private int frameHeight;
private bool shoot = false;
void Start (){
screenWidth = Screen.width;
screenHeight = Screen.height;
frameWidth = Mathf.RoundToInt(screenWidth * photoWidth *
0.01f);
frameHeight = Mathf.RoundToInt(screenHeight * photoHeight
* 0.01f);
texture = new Texture2D (frameWidth,frameHeight,TextureFor
mat.RGB24,false);
border = new Texture2D (1,1,TextureFormat.ARGB32, false);
border.SetPixel(0,0,borderColor);
border.Apply();
}
Chapter 2
41
void Update (){
if (Input.GetKeyUp(KeyCode.Mouse0))
StartCoroutine(CaptureScreen());
}
void OnGUI (){
GUI.DrawTexture(new Rect((screenWidth*0.5f)-
(frameWidth*0.5f) - borderWidth*2,((screenHeight*0.5f)-
(frameHeight*0.5f)) - borderWidth,frameWidth + borderWidth*2,borde
rWidth),border,ScaleMode.StretchToFill);
GUI.DrawTexture(new Rect((screenWidth*0.5f)-
(frameWidth*0.5f) - borderWidth*2,(screenHeight*0.5f)+(frameHeigh
t*0.5f),frameWidth + borderWidth*2,borderWidth),border,ScaleMode.
StretchToFill);
GUI.DrawTexture(new Rect((screenWidth*0.5f)-
(frameWidth*0.5f)- borderWidth*2,(screenHeight*0.5f)-(frameHeight*
0.5f),borderWidth,frameHeight),border,ScaleMode.StretchToFill);
GUI.DrawTexture(new Rect((screenWidth*0.5f)+(frameWidth*0.
5f),(screenHeight*0.5f)-(frameHeight*0.5f),borderWidth,frameHeight
),border,ScaleMode.StretchToFill);
if(shoot){
GUI.DrawTexture(new Rect (10,10,frameWidth*thumbPropo
rtion*0.01f,frameHeight*thumbProportion* 0.01f),texture,ScaleMode.
StretchToFill);
}
}
IEnumerator CaptureScreen (){
yield return new WaitForEndOfFrame();
texture.ReadPixels(new Rect((screenWidth*0.5f)-(frameWidth
*0.5f),(screenHeight*0.5f)-(frameHeight*0.5f),frameWidth,frameHeig
ht),0,0);
texture.Apply();
shoot = true;
}
}

4. Save your script and apply it to the Main Camera game object.
4. 保存你的代码,让后将它挂载到你的主摄像机游戏对象上

5. In the Inspector view, change the values for the Screen Capture component, leaving Photo Width and Photo Height as 25 and Thumb Proportion as 75, as shown here:
5. 在检视面板中,改变屏幕截图内容的取值。将Photo Width(图片宽度)和Photo Height(图片高度)设为25,将Thumb Proportion设为75,如下所示:
6. Play the scene. You will be able to take a snapshot of the screen (and have it displayed on the top-left corner) by clicking the mouse button.
6. 运行你的场景,通过点击鼠标左键,你将会看到一个屏幕快拍(并展示在画面左上角处)
How it works...(工作原理)
Unity's menu actually reads the Standard Packages folder content when starting up,instead of getting that information from somewhere else. This is very practical, as it always reflects the actual content of that folder and also allows the user to quickly retrieve his favorite packages.
Unity的菜单在启动的时候会自动读取Standard Packages文件夹中的内容,而不是从其他某些地方读取信息。这一点很实用,正因为它自动读取该文件夹的内容,所以也允许使用者快速从菜单栏中提取自己喜欢的包。
There's more...(更多)
Clicking the mouse triggers a function that reads pixels within the specified rectangle and applies them into a texture that is drawn by the GUI.
点击鼠标,将会触发一个功能(函数),这个功能将会读取屏幕所示矩形内所有的像素,并通过GUI将其绘制进一张纹理图片当中
后续:关于C#和编程学习
unity是一款游戏引擎,游戏引擎具有整合多方功能的特性,美术人员在其中完成基本场景的搭建、布光、材质以及主要镜头的控制,程序员则需要根据游戏设计师的设计实现各项游戏机制。
但是,无论是美工还是编程,在游戏引擎中,程序(也就是脚本)是不可回避的,正因为脚本的存在,才可以灵活的实现一些效果和功能。在游戏美术中,PBS(也就是PBR材质)的全称为(Physically Based Shading),其中“shade”或是PBR中的“Render”就是着色器(shader)、渲染器。PBS材质之所以如此真实,很大部分原因就在于引擎中内置的shader(着色器)。而着色器,要实现逼真的光影、材质效果,就必须还要掌握着色器语言。
所以,代码的学习,看读者或是使用者的发展规划,如果完成一般项目,或是表现已有效果,只要将代码复制粘贴即可(很常用的),不用深究C#语言和着色器语言,以免造成额外的学习负担;如果,读者有意制作独立游戏,或是深入挖掘画面效果(比如技术美术),就要基本掌握C#语言和着色器语言。
译者是一个文科生出身,当年热爱游戏才选择自学编程。刚开始看编程,看不懂,很多概念都混淆不清。之后,下载IDE跟着书敲代码,再结合具体的unity案例,其实自主学习两三个月就可以应付一些基本的常见的代码了,所以有想深入学习unity知识的不要畏惧它,多看看都能学会。


本帖被以下画板推荐:

还没有设置签名!您可以在此展示你的链接,或者个人主页!
使用道具 <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表