using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using static Writepdf; public enum Derive { Residentcontract, Transfercontract, Lowpressurecontract, Naturalcontract, Highpressurecontract, Temporarycontract, Powergenerationproject } public class Administrativefile : MonoBehaviour { /// /// 合同之间切换按钮 /// public Toggle residentendorsementtoggle, switchpowersupplytoggle, lowpressuretoggle, artificialtoggle, hightensiontoggle, temporarilytoggle, naturalpersontoggle; /// /// 切换合同的不同状态 /// Derive derive = Derive.Residentcontract; /// /// 需要生成的位置 /// public RectTransform operatingarea; /// /// 导出pdf格式按钮 /// public Button pdfbutton; /// /// 每个合同身上对应的脚本 /// public Transfercontract transfercontract; /// /// 居民背书合同 /// public GameObject residentcontract; /// /// 转供电合同 /// public GameObject Transfercontract; /// /// 低压供电合同 /// public GameObject Lowpowersupplycontract; /// /// 非自然人分布式光伏发电项目购售合同 /// public GameObject Nonnaturalperson; /// /// 高压供电合同 /// public GameObject highvoltagepowersupply; /// /// 临时供电合同 /// public GameObject Temporarypowersupply; /// /// 自然人分布式光伏发电项目购售电合同 /// public GameObject Contractofnaturalperson; /// /// 存取合同对象 /// public List contract = new List(); /// /// 存放的图片链表 /// public List pictures = new List(); /// /// 获取当前时间 /// private string Time { get { return System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); } } /// /// 提示导出路劲页面框 /// public GameObject Overlay; /// /// 显示路径 /// public InputField derivedpath; /// /// 把路劲页面失活 /// public Button verify; void Start() { Init(); Contractswitching(); } private void Init() { residentcontract = Resources.Load("UIpanel/residentcontract"); Transfercontract = Resources.Load("UIpanel/Transfercontract"); Lowpowersupplycontract = Resources.Load("UIpanel/Lowpowersupplycontract"); Nonnaturalperson = Resources.Load("UIpanel/Nonnaturalperson"); highvoltagepowersupply = Resources.Load("UIpanel/highvoltagepowersupply"); Temporarypowersupply = Resources.Load("UIpanel/Temporarypowersupply"); Contractofnaturalperson = Resources.Load("UIpanel/Contractofnaturalperson"); if (derive == Derive.Residentcontract) { Deletecontract(); if (residentcontract) { GameObject residentcontracts = Instantiate(residentcontract, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } pdfbutton.onClick.AddListener(() => { switch (derive) { case Derive.Residentcontract: EquippingItems(); derivedpath.text = "d:\\居民背书合同" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; case Derive.Transfercontract: EquippingItems(); derivedpath.text = "d:\\转供电合同" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; case Derive.Lowpressurecontract: EquippingItems(); derivedpath.text = "d:\\低压供用电合同(2022版)" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; case Derive.Naturalcontract: EquippingItems(); derivedpath.text = "d:\\非自然人分布式光伏发电项目购售" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; case Derive.Highpressurecontract: EquippingItems(); derivedpath.text = "d:\\高压供电合同" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; case Derive.Temporarycontract: EquippingItems(); derivedpath.text = "d:\\临时供电合同(2022)版" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; case Derive.Powergenerationproject: EquippingItems(); derivedpath.text = "d:\\自然人分布式光伏发电项目购售电合同" + Time + ".pdf"; StartCoroutine(CaptureScreenshot(transfercontract.inhabitants, derivedpath.text));//打印 break; default: break; } }); verify.onClick.AddListener(() => { derivedpath.text = ""; Overlay.SetActive(false); }); } public void EquippingItems() { if (pictures.Count > 0) { for (int i = 0; i < pictures.Count; i++) { Destroy(pictures[i]); } pictures.Clear(); } for (int i = 0; i < transfercontract.inhabitants.Length; i++) { Picture(transfercontract.inhabitants[i]); } } /// /// 合同切换按钮 /// private void Contractswitching() { residentendorsementtoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Residentcontract; Deletecontract(); if (residentcontract) { GameObject residentcontracts = Instantiate(residentcontract, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); switchpowersupplytoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Transfercontract; Deletecontract(); if (Transfercontract) { GameObject residentcontracts = Instantiate(Transfercontract, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); lowpressuretoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Lowpressurecontract; Deletecontract(); if (Lowpowersupplycontract) { GameObject residentcontracts = Instantiate(Lowpowersupplycontract, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); artificialtoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Naturalcontract; Deletecontract(); if (Nonnaturalperson) { GameObject residentcontracts = Instantiate(Nonnaturalperson, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); hightensiontoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Highpressurecontract; Deletecontract(); if (highvoltagepowersupply) { GameObject residentcontracts = Instantiate(highvoltagepowersupply, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); temporarilytoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Temporarycontract; Deletecontract(); if (Temporarypowersupply) { GameObject residentcontracts = Instantiate(Temporarypowersupply, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); naturalpersontoggle.onValueChanged.AddListener((ison) => { if (ison) { derive = Derive.Powergenerationproject; Deletecontract(); if (Contractofnaturalperson) { GameObject residentcontracts = Instantiate(Contractofnaturalperson, operatingarea); contract.Add(residentcontracts); if (residentcontracts) { transfercontract = residentcontracts.GetComponent(); } } } }); } /// /// 把图片转成pdf格式 /// /// /// private IEnumerator CaptureScreenshot(Image[] images, string path) { for (int i = 0; i < images.Length; i++) { Picture2(images[i]); yield return new WaitForEndOfFrame(); // 创建一个新的Texture2D对象,尺寸为屏幕分辨率 Texture2D screenshot = new Texture2D(1080, 814, TextureFormat.RGB24, false); // 从屏幕读取像素数据到Texture2D对象中 screenshot.ReadPixels(new Rect(445, 55, 1080, 814), 0, 0); screenshot.Apply(); pictures.Add(screenshot); Picture(images[i]); } Texture2D[] intArray = pictures.ToArray(); Convert2PDF(intArray, path); transfercontract.Restore(0); Overlay.SetActive(true); } /// /// 把图片失活 /// public void Picture(Image images) { images.gameObject.SetActive(false); } /// /// 把图片激活 /// public void Picture2(Image images) { images.gameObject.SetActive(true); } /// /// 删除方法 /// public void Deletecontract() { if (contract.Count > 0) { for (int i = 0; i < contract.Count; i++) { Destroy(contract[i].gameObject); } contract.Clear(); } } void Update() { } }