日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity调用打印机

發布時間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity调用打印机 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System; using System.Diagnostics; using System.Drawing.Printing; using System.IO; using UnityEngine;namespace LCPrinter {public static class Prints{//打印圖片(圖片的字節流,打印張數,打印的名字)public static void PrintTexture(byte[] texture2DBytes, int numCopies, string printerName){//字節流是否為空if (texture2DBytes == null){//輸出提示UnityEngine.Debug.LogWarning("LCPrinter: Texture is empty.");return;}//打印設置新建對象PrinterSettings printerSettings = new PrinterSettings();//打印名字為空或者等于空if (printerName == null || printerName.Equals("")){printerName = printerSettings.PrinterName;UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ").");}//儲存時間段信息string str = string.Concat(new string[]{DateTime.Now.Year.ToString(),"-",DateTime.Now.Month.ToString(),"-",DateTime.Now.Day.ToString(),"-",DateTime.Now.Hour.ToString(),"-",DateTime.Now.Minute.ToString(),"-",DateTime.Now.Second.ToString(),"-",DateTime.Now.Millisecond.ToString()});//文本string text = (Application.persistentDataPath + "\\LCPrinterFiletmp_" + str + ".png").Replace("/", "\\");UnityEngine.Debug.Log("LCPrinter: Temporary Path - " + text);//字節流寫入File.WriteAllBytes(text, texture2DBytes);Prints.PrintCMD(text, numCopies, printerName);}//通過紋理路徑(路徑,打印張數,輸出的名字)public static void PrintTextureByPath(string path, int numCopies, string printerName){//打印機設置PrinterSettings printerSettings = new PrinterSettings();//判斷是否為空if (printerName == null || printerName.Equals("")){//設置打印的名字printerName = printerSettings.PrinterName;UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ").");}Prints.PrintCMD(path, numCopies, printerName);}//CMDprivate static void PrintCMD(string path, int numCopies, string printerName){//對進程的控制Process process = new Process();try{for (int i = 0; i < numCopies; i++){process.StartInfo.FileName = "rundll32";process.StartInfo.Arguments = string.Concat(new string[]{"C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_PrintTo \"",path,"\" \"",printerName,"\""});process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;process.StartInfo.UseShellExecute = true;process.Start();}}catch (Exception arg){UnityEngine.Debug.LogWarning("LCPrinter: " + arg);}finally{process.Close();UnityEngine.Debug.Log("LCPrinter: Texture printing.");}}} }

使用時需要Using 命名空間,然后通過Prints.PrintTexture()方法來打印。

舉個栗子:

using System.Collections; using System.Collections.Generic; using UnityEngine; using LCPrinter;public class demo : MonoBehaviour {public Texture2D screenShot;void test(){Prints.PrintTexture(screenShot.EncodeToPNG(), 1, "");} }

這里并沒有給打印機名字是使用默認的打印機,需要自己設置。

需要注意的是texture2D的分辨率是否符合打印機默認的尺寸。

總結

以上是生活随笔為你收集整理的Unity调用打印机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。