真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

這篇文章主要講解了Unity實(shí)現(xiàn)相機(jī)截圖功能的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

成都創(chuàng)新互聯(lián)公司不只是一家網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司;我們對(duì)營(yíng)銷(xiāo)、技術(shù)、服務(wù)都有自己獨(dú)特見(jiàn)解,公司采取“創(chuàng)意+綜合+營(yíng)銷(xiāo)”一體化的方式為您提供更專(zhuān)業(yè)的服務(wù)!我們經(jīng)歷的每一步也許不一定是最完美的,但每一步都有值得深思的意義。我們珍視每一份信任,關(guān)注我們的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)質(zhì)量和服務(wù)品質(zhì),在得到用戶(hù)滿(mǎn)意的同時(shí),也能得到同行業(yè)的專(zhuān)業(yè)認(rèn)可,能夠?yàn)樾袠I(yè)創(chuàng)新發(fā)展助力。未來(lái)將繼續(xù)專(zhuān)注于技術(shù)創(chuàng)新,服務(wù)升級(jí),滿(mǎn)足企業(yè)一站式成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)需求,讓再小的高端網(wǎng)站設(shè)計(jì)也能產(chǎn)生價(jià)值!

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

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

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

截圖效果:

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

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

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

腳本:

CameraCapture.cs

using UnityEngine;
using System.IO;
 
/// 
/// 相機(jī)截圖
/// ZhangYu 2018-07-06
/// 
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/";
 // 文件名稱(chēng)
 public string fileName = "cameraCapture.png";
 
 #if UNITY_EDITOR
 private void Reset() {
 targetCamera = GetComponent();
 pixelSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
 }
 #endif
 
 ///  保存截圖 
 /// 目標(biāo)攝像機(jī)
 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));
 }
 
 ///  相機(jī)截圖 
 /// 目標(biāo)相機(jī)
 public static Texture2D capture(Camera camera) {
 return capture(camera, Screen.width, Screen.height);
 }
 
 ///  相機(jī)截圖 
 /// 目標(biāo)相機(jī)
 /// 寬度
 /// 高度
 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;
 }
 
 ///  保存貼圖 
 /// 保存路徑
 /// Texture2D
 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;
 
/// 
/// 相機(jī)截圖 編輯器
/// ZhangYu 2018-07-06
/// 
[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", "文件名稱(chēng)");
 
 // 保存截圖按鈕
 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);
 }
 
}

看完上述內(nèi)容,是不是對(duì)Unity實(shí)現(xiàn)相機(jī)截圖功能的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


網(wǎng)頁(yè)名稱(chēng):Unity實(shí)現(xiàn)相機(jī)截圖功能的方法
鏈接地址:http://weahome.cn/article/psdjoe.html

其他資訊

在線咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部