欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實(shí)現(xiàn)相機(jī)截圖功能

 更新時(shí)間:2020年04月18日 11:32:32   作者:冰封百度  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)相機(jī)截圖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近做項(xiàng)目的時(shí)候需要在游戲里截一張高清截圖,研究了一下寫(xiě)成腳本,方便以后使用。

腳本可以自定義分辨率,用相機(jī)截高清截圖??梢杂么a動(dòng)態(tài)截圖,也可以在編輯模式下截圖。

注意截圖寬高比要正確,寬高比不正確時(shí)可能會(huì)出問(wèn)題。

截圖效果:

腳本:

CameraCapture.cs

using UnityEngine;
using System.IO;
 
/// <summary>
/// 相機(jī)截圖
/// <para>ZhangYu 2018-07-06</para>
/// </summary>
public class CameraCapture : MonoBehaviour {
 
 // 截圖尺寸
 public enum CaptureSize {
 CameraSize,
 ScreenResolution,
 FixedSize
 }
 
 // 目標(biāo)攝像機(jī)
 public Camera targetCamera;
 // 截圖尺寸
 public CaptureSize captureSize = CaptureSize.CameraSize;
 // 像素尺寸
 public Vector2 pixelSize;
 // 保存路徑
 public string savePath = "StreamingAssets/";
 // 文件名稱
 public string fileName = "cameraCapture.png";
 
 #if UNITY_EDITOR
 private void Reset() {
 targetCamera = GetComponent<Camera>();
 pixelSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
 }
 #endif
 
 /// <summary> 保存截圖 </summary>
 /// <param name="camera">目標(biāo)攝像機(jī)</param>
 public void saveCapture() {
 Vector2 size = pixelSize;
 if (captureSize == CaptureSize.CameraSize) {
  size = new Vector2(targetCamera.pixelWidth, targetCamera.pixelHeight);
 } else if (captureSize == CaptureSize.ScreenResolution) {
  size = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
 }
 string path = Application.dataPath + "/" + savePath + fileName;
 saveTexture(path, capture(targetCamera, (int)size.x, (int)size.y));
 }
 
 /// <summary> 相機(jī)截圖 </summary>
 /// <param name="camera">目標(biāo)相機(jī)</param>
 public static Texture2D capture(Camera camera) {
 return capture(camera, Screen.width, Screen.height);
 }
 
 /// <summary> 相機(jī)截圖 </summary>
 /// <param name="camera">目標(biāo)相機(jī)</param>
 /// <param name="width">寬度</param>
 /// <param name="height">高度</param>
 public static Texture2D capture(Camera camera, int width, int height) {
 RenderTexture rt = new RenderTexture(width, height, 0);
 rt.depth = 24;
 rt.antiAliasing = 8;
 camera.targetTexture = rt;
 camera.RenderDontRestore();
 RenderTexture.active = rt;
 Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
 Rect rect = new Rect(0, 0, width, height);
 texture.ReadPixels(rect, 0, 0);
 texture.filterMode = FilterMode.Point;
 texture.Apply();
 camera.targetTexture = null;
 RenderTexture.active = null;
 Destroy(rt);
 return texture;
 }
 
 /// <summary> 保存貼圖 </summary>
 /// <param name="path">保存路徑</param>
 /// <param name="texture">Texture2D</param>
 public static void saveTexture(string path, Texture2D texture) {
 File.WriteAllBytes(path, texture.EncodeToPNG());
 #if UNITY_EDITOR
 Debug.Log("已保存截圖到:" + path);
 #endif
 }
 
}

腳本編輯器:

CameraCaptureEditor.cs

using UnityEditor;
using UnityEngine;
 
/// <summary>
/// 相機(jī)截圖 編輯器
/// <para>ZhangYu 2018-07-06</para>
/// </summary>
[CanEditMultipleObjects]
[CustomEditor(typeof(CameraCapture))]
public class CameraCaptureEditor : Editor {
 
 public override void OnInspectorGUI() {
 // 屬性
 CameraCapture script = (CameraCapture)target;
 int selected = (int)script.captureSize;
 
 // 重繪GUI
 EditorGUI.BeginChangeCheck();
 drawProperty("targetCamera", "目標(biāo)像機(jī)");
 string[] options = new string[] { "像機(jī)尺寸", "屏幕尺寸", "固定尺寸"};
 selected = EditorGUILayout.Popup("截圖尺寸", selected, options, GUILayout.ExpandWidth(true));
 script.captureSize = (CameraCapture.CaptureSize)selected;
 if (script.captureSize == CameraCapture.CaptureSize.FixedSize) {
  drawProperty("pixelSize", "像素尺寸");
  EditorGUILayout.HelpBox("請(qǐng)保持正確的寬高比!\n否則截圖區(qū)域可能出現(xiàn)錯(cuò)誤。", MessageType.Info);
 }
 drawProperty("savePath", "保存路徑");
 drawProperty("fileName", "文件名稱");
 
 // 保存截圖按鈕
 bool isPress = GUILayout.Button("保存截圖", GUILayout.ExpandWidth(true));
 if (isPress) script.saveCapture();
 if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
 }
 
 private void drawProperty(string property, string label) {
 EditorGUILayout.PropertyField(serializedObject.FindProperty(property), new GUIContent(label), true);
 }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#集合之有序列表的用法

    C#集合之有序列表的用法

    這篇文章介紹了C#集合之有序列表的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#使用AnimateWindow()實(shí)現(xiàn)動(dòng)畫(huà)窗體的方法

    C#使用AnimateWindow()實(shí)現(xiàn)動(dòng)畫(huà)窗體的方法

    用API函數(shù)AnimateWindow函數(shù)來(lái)實(shí)現(xiàn)窗體的動(dòng)畫(huà)效果,在C#中,你可以使用P/Invoke技術(shù)調(diào)用Windows API中的AnimateWindow函數(shù)來(lái)實(shí)現(xiàn)動(dòng)畫(huà)窗體,本文就給大家介紹了C#使用AnimateWindow()實(shí)現(xiàn)動(dòng)畫(huà)窗體的方法,感興趣的朋友可以參考下
    2024-04-04
  • c# 實(shí)現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的方法

    c# 實(shí)現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的方法

    下面小編就為大家?guī)?lái)一篇c# 實(shí)現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • c#詳解datetime使用示例

    c#詳解datetime使用示例

    本文主要介紹了c# datetime使用示例,大家參考使用吧
    2014-05-05
  • 詳解c# 事件總線

    詳解c# 事件總線

    這篇文章主要介紹了c# 事件總線的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-05-05
  • Winform讓DataGridView左側(cè)顯示圖片

    Winform讓DataGridView左側(cè)顯示圖片

    本文主要介紹在如何讓DataGridView左側(cè)顯示圖片,這里主要講解重寫(xiě)DataGridView的OnRowPostPaint方法,需要的朋友可以參考下。
    2016-05-05
  • 避免在C#循環(huán)中使用await的方法小結(jié)

    避免在C#循環(huán)中使用await的方法小結(jié)

    在C#中,異步編程因其能夠提升應(yīng)用程序性能和響應(yīng)能力而變得越來(lái)越流行,async和await關(guān)鍵字使得編寫(xiě)異步代碼變得更加容易,但如果使用不當(dāng),它們也可能引入一些陷阱,所以本文我們將探討為什么應(yīng)該避免在C#循環(huán)中使用await,并討論一些更高效地處理異步操作的替代方法
    2024-09-09
  • C#利用后綴表達(dá)式解析計(jì)算字符串公式

    C#利用后綴表達(dá)式解析計(jì)算字符串公式

    當(dāng)我們拿到一個(gè)字符串比如:20+31*(100+1)的時(shí)候用口算就能算出結(jié)果為3151,因?yàn)檫@是中綴表達(dá)式對(duì)于人類(lèi)的思維很簡(jiǎn)單,但是對(duì)于計(jì)算機(jī)就比較復(fù)雜了。相對(duì)的后綴表達(dá)式適合計(jì)算機(jī)進(jìn)行計(jì)算。本文就來(lái)用后綴表達(dá)式實(shí)現(xiàn)解析計(jì)算字符串公式,需要的可以參考一下
    2023-02-02
  • 輕松學(xué)習(xí)C#的屬性

    輕松學(xué)習(xí)C#的屬性

    輕松學(xué)習(xí)C#的屬性,對(duì)C#的屬性感興趣的朋友可以參考本篇文章,幫助大家更靈活的運(yùn)用C#的屬性
    2015-11-11
  • C#(.Net)將非托管dll嵌入exe中的實(shí)現(xiàn)

    C#(.Net)將非托管dll嵌入exe中的實(shí)現(xiàn)

    本文主要介紹了C#(.Net)將非托管dll嵌入exe中的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12

最新評(píng)論