Unity调用打印机
生活随笔
收集整理的這篇文章主要介紹了
Unity调用打印机
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using UnityEngine;namespace LCPrinter
{public static class Prints{//打印圖片(圖片的字節(jié)流,打印張數(shù),打印的名字)public static void PrintTexture(byte[] texture2DBytes, int numCopies, string printerName){//字節(jié)流是否為空if (texture2DBytes == null){//輸出提示UnityEngine.Debug.LogWarning("LCPrinter: Texture is empty.");return;}//打印設(shè)置新建對(duì)象PrinterSettings printerSettings = new PrinterSettings();//打印名字為空或者等于空if (printerName == null || printerName.Equals("")){printerName = printerSettings.PrinterName;UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ").");}//儲(chǔ)存時(shí)間段信息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);//字節(jié)流寫入File.WriteAllBytes(text, texture2DBytes);Prints.PrintCMD(text, numCopies, printerName);}//通過紋理路徑(路徑,打印張數(shù),輸出的名字)public static void PrintTextureByPath(string path, int numCopies, string printerName){//打印機(jī)設(shè)置PrinterSettings printerSettings = new PrinterSettings();//判斷是否為空if (printerName == null || printerName.Equals("")){//設(shè)置打印的名字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){//對(duì)進(jìn)程的控制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.");}}}
}
使用時(shí)需要Using 命名空間,然后通過Prints.PrintTexture()方法來打印。
舉個(gè)栗子:
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, "");} }這里并沒有給打印機(jī)名字是使用默認(rèn)的打印機(jī),需要自己設(shè)置。
需要注意的是texture2D的分辨率是否符合打印機(jī)默認(rèn)的尺寸。
總結(jié)
以上是生活随笔為你收集整理的Unity调用打印机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单学生成绩管理系统Java版(GUI+
- 下一篇: 函数 devm_kzalloc()