using System; using System.Collections.Generic; using UnityEngine; namespace HK { public class HanYinPrinterManager : MonoBehaviour { public Texture2D logo; private static string GetTime() { string time = DateTime.Now.ToString("h:mm"); if (DateTime.Now.Hour < 12) time += "am"; else time += "pm"; return time; } public void PrintTestPaper() { PrintLogo("192.168.0.126", "123fdsa1", "\"NOTEBOOK LG DOT HYDRANGEA BLUE HARD COVER,1,338,,Large Customization Front or Back,1,0,5577000008701,NOTEBOOK PK DOT MYRTLE GREEN HARD COVER,1,258,,Pocket Customization Front or Back,1,0,5577000008718\"", 123, "abc1243", "test", "12345678"); } void PrintText(AndroidJavaClass printClass, string s) { printClass.CallStatic("PrintText", s, 0, 0, 0); } void PrintLine(AndroidJavaClass printClass) { printClass.CallStatic("PrintText", " ", 0, 4, 0); } void PrintNewLine(AndroidJavaClass printClass) { printClass.CallStatic("PrintAndLineFeed"); } /// /// 打印 /// /// 打印机IP地址 /// 这个应该就是orderID /// 打印的主体内容 /// 价钱 /// orderID /// 顾客姓名:指购买商品或服务的人的名字。 /// 顾客电话 public void PrintLogo(string printer_ip, string backend_order_id, string json, decimal totalPrice, string order_id, string customer_name, string customer_phone) { if (Application.platform == RuntimePlatform.Android) { try { // Get Android Context using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) using (AndroidJavaObject currentActivity = unityPlayer.GetStatic("currentActivity")) { // Call Print.PortOpen(Context, String) using (AndroidJavaClass printClass = new AndroidJavaClass("print.Print")) { string port = $"WiFi,{printer_ip},9100"; // Change to your printer's connection type int result = printClass.CallStatic("PortOpen", currentActivity, port); Debug.Log($"connect result: {result}"); } } using (AndroidJavaClass printClass = new AndroidJavaClass("print.Print")) { printClass.CallStatic("PrintText", " \n", 1, 1, 0); using (AndroidJavaClass wrapperClass = new AndroidJavaClass("PrinterWrapper")) { // Call PrintImage(String path, byte var1, byte var2, int var3) byte[] array = logo.EncodeToPNG(); int result = wrapperClass.CallStatic("PrintImageFromString", array, 0, 0); Debug.Log(result != -1 ? "Print logo success" : "Print logo failed"); } PrintNewLine(printClass); PrintNewLine(printClass); PrintText(printClass, "Code no. " + backend_order_id); PrintText(printClass, "Date: " + DateTime.Now.ToString("yyyy/MM/dd") + " " + "Time: " + GetTime()); PrintLine(printClass); PrintNewLine(printClass); json = json.Trim('\"'); string[] words = json.Split(','); List productNames = new List(); List productQuantity = new List(); List productPrice = new List(); List productBarcode = new List(); for (int i = 0; i < words.Length; i++) { if (i % 4 == 0) { productNames.Add(words[i]); } else if (i % 4 == 1) { productQuantity.Add(words[i]); } else if (i % 4 == 2) { productPrice.Add(words[i]); } else if (i % 4 == 3) { productBarcode.Add(words[i]); } } int frontLimit = 30; for (int i = 0; i < productNames.Count; i++) { string productName = productNames[i]; int productLength = productName.Length; int head = 0; // Tracks current position in productName while (productLength > 0) { int printLength = Math.Min(frontLimit, productLength); string partToPrint; // Check if we are in the middle of a word if (head + printLength < productName.Length && !char.IsWhiteSpace(productName[head + printLength])) { int lastSpace = productName.LastIndexOf(' ', head + printLength, printLength); if (lastSpace > head) { printLength = lastSpace - head; // Adjust to break at the last space } } partToPrint = productName.Substring(head, printLength).Trim(); // If it's the first line, append quantity and price if (head == 0) { partToPrint = partToPrint.PadRight(frontLimit, ' ') + ("x" + productQuantity[i]).PadLeft(3, ' ') + ("SGD" + productPrice[i]).PadLeft(10, ' '); } else { partToPrint = partToPrint.PadRight(frontLimit, ' '); // Pad remaining space } PrintText(printClass, partToPrint); // Move to the next part head += printLength + 1; // Skip the space productLength -= printLength + 1; } printClass.CallStatic("PrintBarCode", 69, productBarcode[i], 2, 60, 0, 1); PrintNewLine(printClass); } PrintLine(printClass); PrintNewLine(printClass); PrintText(printClass, "Total:".PadRight(frontLimit, ' ') + ("SGD" + totalPrice).PadLeft(13, ' ')); PrintNewLine(printClass); printClass.CallStatic("PrintText", "\nThank you! Please show this\nticket to the staff at the counter.\n\n", 1, 0, 0); printClass.CallStatic("PrintText", order_id, 1, 0, 32); PrintNewLine(printClass); PrintNewLine(printClass); printClass.CallStatic("PrintText", customer_name + " " + customer_phone + "\n\n", 1, 0, 0); PrintNewLine(printClass); PrintNewLine(printClass); PrintNewLine(printClass); PrintNewLine(printClass); printClass.CallStatic("CutPaper", 1); printClass.CallStatic("PortClose"); } } catch (Exception e) { Debug.LogError("Error calling PrintImage: " + e.Message); } } } } }