Unity调用打印机

由于原本插件是添加好引用的,反编译之后缺少了引用,所以要去统一的安装路径E:\ unity5.3.2 \统一\编辑\数据\单声道\ lib中\单\ 2.0中找到System.Drawing.dll程序程序放入项目中的插件下。如在VS中报错没有添加引用,则要对项目添加引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using UnityEngine;
  
namespace LCPrinter
{
    public static class Print
    {
        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);
            Print.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 + ").");
            }
            Print.PrintCMD(path, numCopies, printerName);
        }
  
        private 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.");
            }
        }
    }
}

这是实现功能的源码。调用方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
using System.IO;
using LCPrinter;
using UnityEngine.UI;
  
public class LCExampleScript : MonoBehaviour {
  
    public Texture2D texture2D;
    public string printerName = "";
    public int copies = 1;
  
    public InputField inputField;
  
    public void printSmileButton()
    {
        Print.PrintTexture(texture2D.EncodeToPNG(), copies, printerName);//打印一张编辑器中的图片
    }
  
    public void printByPathButton()
    {
        Print.PrintTextureByPath("D:\\pic.png", copies, printerName);//打印一张存在指定路径的图片
    }
}

  

posted @   多见多闻  阅读(428)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示