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);
        }

        //CMD
        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.");
            }
        }
    }
}

使用时需要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的分辨率是否符合打印机默认的尺寸。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 实现 Unity 调用打印机功能 #### 文本打印 为了实现在 Unity调用打印机打印特定文本,在 C# 代码中可以创建 `Printer` 类的对象,并传递要打印的字符串作为参数。这通过如下方式完成: ```csharp // 创建 Printer 对象并将待打印文本传入构造函数 Printer printer = new Printer("This is the text to print"); ``` 此方法适用于简单的文本打印需求[^1]。 #### 移动端 Android 打印机调用 对于希望在基于 Android 的移动设备上实现打印功能的情况,可以通过 Java 插件与本地代码交互达成目的。下面展示了初始化插件以及验证其是否正确加载的方法: ```csharp void Initialize(string pluginName) { var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); var currentActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity"); try { _pluginInstance = new AndroidJavaObject(pluginName); if (_pluginInstance != null) { Debug.Log("Plugin loaded successfully."); // 可选:向插件传递当前活动实例 // _pluginInstance.Call("ReceiveUnityActivity", currentActivity); Debug.Log("Initialization completed."); // 如果有专门用于启动打印的方法,则可在此处调用 // _pluginInstance.Call("StartPrintJob", ...); } } catch (Exception e) { Debug.LogError($"Failed to load plugin: {e.Message}"); } } ``` 这段代码片段不仅实现了基本的插件初始化逻辑,还包含了异常处理机制以增强稳定性[^2]。 #### 图片打印 当目标是从 Unity 应用程序发送图像到默认图片查看器或编辑工具(如 Windows 上的画图应用程序),则可以直接利用操作系统命令行接口执行该任务。例如,使用 `Process.Start()` 方法打开指定路径下的文件,并附加相应参数指示应采取的动作: ```csharp using System.Diagnostics; ... Process.Start("mspaint.exe", "/pt d:\\screenshots\\sample.jpg"); ``` 这种方式依赖于外部应用的支持程度,因此可能不适合所有场景;但对于某些简单的需求来说已经足够有效[^3]。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值