using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using static house_file_crl.TextureUtility;
public class house_file_crl : MonoBehaviour
{
? ? public int house_num;
? ? public bool add_one_more;
? ? public GameObject btn_delect, btn_reset, btn_details;
? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ? defaout_action();
? ? }
? ? public bool enter_defaut = false;
? ? void defaout_action()
? ? {
? ? ? ? enter_defaut = true;
? ? ? ? string path = house_details_crl._instance.get_foder_name() + "/" + house_num;
? ? ? ? if (System.IO.Directory.Exists(path) == true)//如果不存在就創(chuàng)建file文件夾
? ? ? ? {
? ? ? ? ? ? house_num_obj.GetComponent<Text>().text = house_num.ToString();
? ? ? ? ? ? if (add_one_more)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameObject m_Obj = Instantiate(this.gameObject);
? ? ? ? ? ? ? ? m_Obj.transform.SetParent(transform.parent);
? ? ? ? ? ? ? ? m_Obj.transform.localScale = Vector3.one;
? ? ? ? ? ? ? ? m_Obj.GetComponent<house_file_crl>().add_one_more = true;
? ? ? ? ? ? ? ? m_Obj.GetComponent<house_file_crl>().house_num = house_num+1;
? ? ? ? ? ? }
? ? ? ? ? ? btn_delect.SetActive(true);
? ? ? ? ? ? btn_reset.SetActive(true);
? ? ? ? ? ? btn_details.SetActive(true);
? ? ? ? ? ? house_num_obj.SetActive(true);
? ? ? ? ? ? add_img.SetActive(false);
? ? ? ? }
? ? }
? ? public void get_files()? //獲取圖片入口
? ? {
? ? ? ? init_file._instance.show_tip();
? ? ? ? string path = house_details_crl._instance.get_foder_name()+"/"+ house_num;
? ? ? ? if (System.IO.Directory.Exists(path) == false)//如果不存在就創(chuàng)建file文件夾
? ? ? ? {
? ? ? ? ? ? System.IO.Directory.CreateDirectory(path);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? DeleteFolder(path);
? ? ? ? }
? ? ? ? addhead(path);
? ? }
? ? public static void DeleteFolder(string dir)
? ? {
? ? ? ? foreach (string d in Directory.GetFileSystemEntries(dir))
? ? ? ? {
? ? ? ? ? ? if (File.Exists(d))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? FileInfo fi = new FileInfo(d);
? ? ? ? ? ? ? ? if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
? ? ? ? ? ? ? ? ? ? fi.Attributes = FileAttributes.Normal;
? ? ? ? ? ? ? ? File.Delete(d);//直接刪除其中的文件?
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DirectoryInfo d1 = new DirectoryInfo(d);
? ? ? ? ? ? ? ? if (d1.GetFiles().Length != 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? DeleteFolder(d1.FullName);////遞歸刪除子文件夾
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Directory.Delete(d);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public GameObject house_num_obj,add_img;
? ? public void addhead(string s)
? ? {
? ? ? ? print("path: "+s);
? ? ? ? FolderBrowserDialog dialog = new FolderBrowserDialog();
? ? ? ? dialog.Description = "請(qǐng)選擇文件路徑";
? ? ? ? if (dialog.ShowDialog() == DialogResult.OK)
? ? ? ? {
? ? ? ? ? ? string foldPath = dialog.SelectedPath;
? ? ? ? ? ? FolderMove(foldPath, s);
? ? ? ? ? ? print(foldPath+" _path: " + s);
? ? ? ? ? ? house_num_obj.GetComponent<Text>().text = house_num.ToString();
? ? ? ? ? ? if (add_one_more)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameObject m_Obj = Instantiate(this.gameObject);
? ? ? ? ? ? ? ? m_Obj.transform.SetParent(transform.parent);
? ? ? ? ? ? ? ? m_Obj.transform.localScale = Vector3.one;
? ? ? ? ? ? ? ? m_Obj.GetComponent<house_file_crl>().add_one_more = true;
? ? ? ? ? ? ? ? m_Obj.GetComponent<house_file_crl>().house_num = house_num+1;
? ? ? ? ? ? }
? ? ? ? ? ? btn_delect.SetActive(true);
? ? ? ? ? ? btn_reset.SetActive(true);
? ? ? ? ? ? btn_details.SetActive(true);
? ? ? ? ? ? house_num_obj.SetActive(true);
? ? ? ? ? ? add_img.SetActive(false);
? ? ? ? ? ? // MessageBox.Show("已選擇文件夾:" + foldPath, "選擇文件夾提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
? ? ? ? }
? ? }
? ? public void FolderMove(string srcFolderPath, string destFolderPath)
? ? {
? ? ? ? //檢查目標(biāo)目錄是否以目標(biāo)分隔符結(jié)束胀莹,如果不是則添加之
? ? ? ? if (destFolderPath[destFolderPath.Length - 1] != Path.DirectorySeparatorChar)
? ? ? ? ? ? destFolderPath += Path.DirectorySeparatorChar;
? ? ? ? //判斷目標(biāo)目錄是否存在,如果不在則創(chuàng)建之
? ? ? ? if (!System.IO.Directory.Exists(destFolderPath))
? ? ? ? ? ? System.IO.Directory.CreateDirectory(destFolderPath);
? ? ? ? string[] fileList = System.IO.Directory.GetFileSystemEntries(srcFolderPath);
? ? ? ? foreach (string file in fileList)
? ? ? ? {
? ? ? ? ? ? if (System.IO.Directory.Exists(file))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? print("path1: " + file);
? ? ? ? ? ? ? ? FolderMove(file, destFolderPath + Path.GetFileName(file));
? ? ? ? ? ? ? ? //Directory.Delete(file);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? //? System.IO.File.Copy(file, destFolderPath + Path.GetFileName(file));
? ? ? ? ? ? ? ? print(destFolderPath+" **** " + Path.GetFileName(file) + " __path2: " + file);
? ? ? ? ? ? ? ? if (Path.GetFileName(file).Contains("png"))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // GetTexture(file);
? ? ? ? ? ? ? ? ? ? StartCoroutine(GetTexture(file, destFolderPath + Path.GetFileName(file)));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? System.IO.File.Copy(file, destFolderPath + Path.GetFileName(file));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //System.IO.Directory.Delete(srcFolderPath);
? ? }
? ? IEnumerator GetTexture(string url,string destFolderPath)
? ? {
? ? ? ? init_file._instance.show_tip();
? ? ? ? WWW www = new WWW(url);
? ? ? ? yield return www;
? ? ? ? if (www.isDone && www.error == null)
? ? ? ? {
? ? ? ? ? ? Texture2D img = www.texture;
? ? ? ? ? ? // print("texelSize"+img.texelSize);
? ? ? ? ? ? //Sprite sprite = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
? ? ? ? ? ? Texture2D img1 = ThreadedScale(img, 1820, 750, false);? //? 圖片壓縮
? ? ? ? ? ? byte[] date = img1.EncodeToJPG(); //轉(zhuǎn)換格式進(jìn)一步壓縮
? ? ? ? ? ? File.WriteAllBytes(destFolderPath, date);
? ? ? ? }
? ? ? ? yield return new WaitForSeconds(2.0f);
? ? ? ? init_file._instance.close_tips();
? ? }
? ? public void delect_house_msg()
? ? {
? ? ? ? string path = house_details_crl._instance.get_foder_name() + "/" + house_num;
? ? ? // print(path);
? ? ? ? if (System.IO.Directory.Exists(path))//如果不存在就創(chuàng)建file文件夾
? ? ? ? {
? ? ? ? ? ? string path1 = house_details_crl._instance.get_foder_name();
? ? ? ? ? ? System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(path1);
? ? ? ? ? ? int result = dirInfo.GetDirectories().Length;
? ? ? ? ? ? DeleteFolder(path);
? ? ? ? ? ? System.IO.Directory.Delete(path);
? ? ? ? ? ? for (int i = house_num; i < result+1; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (System.IO.Directory.Exists(path1 + "/" + (i + 1)))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? System.IO.Directory.Move(path1 + "/" + (i+1), path1 + "/" + i);
? ? ? ? ? ? ? ? ? ? this.transform.parent.transform.GetChild(i).GetComponent<house_file_crl>().house_num=i;
? ? ? ? ? ? ? ? ? ? this.transform.parent.transform.GetChild(i).GetComponent<house_file_crl>().house_num_obj.GetComponent<Text>().text = i.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? this.transform.parent.transform.GetChild(i).GetComponent<house_file_crl>().house_num = i;
? ? ? ? ? ? ? ? ? ? this.transform.parent.transform.GetChild(i).GetComponent<house_file_crl>().house_num_obj.GetComponent<Text>().text = i.ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Destroy(this.gameObject);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? }
? ? }
? ? private static Color[] texColors;
? ? private static Color[] newColors;
? ? private static int w;
? ? private static float ratioX;
? ? private static float ratioY;
? ? private static int w2;
? ? private static int finishCount;
? ? private static Mutex mutex;
? ? private Texture2D ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear)
? ? {
? ? ? ? texColors = tex.GetPixels();
? ? ? ? newColors = new Color[newWidth * newHeight];
? ? ? ? if (useBilinear)
? ? ? ? {
? ? ? ? ? ? ratioX = 1.0f / ((float)newWidth / (tex.width - 1));
? ? ? ? ? ? ratioY = 1.0f / ((float)newHeight / (tex.height - 1));
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? ratioX = ((float)tex.width) / newWidth;
? ? ? ? ? ? ratioY = ((float)tex.height) / newHeight;
? ? ? ? }
? ? ? ? w = tex.width;
? ? ? ? w2 = newWidth;
? ? ? ? var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
? ? ? ? var slice = newHeight / cores;
? ? ? ? finishCount = 0;
? ? ? ? if (mutex == null)
? ? ? ? {
? ? ? ? ? ? mutex = new Mutex(false);
? ? ? ? }
? ? ? ? if (cores > 1)
? ? ? ? {
? ? ? ? ? ? int i = 0;
? ? ? ? ? ? ThreadData threadData;
? ? ? ? ? ? for (i = 0; i < cores - 1; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? threadData = new ThreadData(slice * i, slice * (i + 1));
? ? ? ? ? ? ? ? ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
? ? ? ? ? ? ? ? Thread thread = new Thread(ts);
? ? ? ? ? ? ? ? thread.Start(threadData);
? ? ? ? ? ? }
? ? ? ? ? ? threadData = new ThreadData(slice * i, newHeight);
? ? ? ? ? ? if (useBilinear)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? BilinearScale(threadData);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? PointScale(threadData);
? ? ? ? ? ? }
? ? ? ? ? ? while (finishCount < cores)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Thread.Sleep(1);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? ThreadData threadData = new ThreadData(0, newHeight);
? ? ? ? ? ? if (useBilinear)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? BilinearScale(threadData);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? PointScale(threadData);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? tex.Resize(newWidth, newHeight);
? ? ? ? tex.SetPixels(newColors);
? ? ? ? tex.Apply();
? ? ? ? return tex;
? ? }
? ? public class TextureUtility
? ? {
? ? ? ? public class ThreadData
? ? ? ? {
? ? ? ? ? ? public int start;
? ? ? ? ? ? public int end;
? ? ? ? ? ? public ThreadData(int s, int e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? start = s;
? ? ? ? ? ? ? ? end = e;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void BilinearScale(System.Object obj)
? ? {
? ? ? ? ThreadData threadData = (ThreadData)obj;
? ? ? ? for (var y = threadData.start; y < threadData.end; y++)
? ? ? ? {
? ? ? ? ? ? int yFloor = (int)Mathf.Floor(y * ratioY);
? ? ? ? ? ? var y1 = yFloor * w;
? ? ? ? ? ? var y2 = (yFloor + 1) * w;
? ? ? ? ? ? var yw = y * w2;
? ? ? ? ? ? for (var x = 0; x < w2; x++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int xFloor = (int)Mathf.Floor(x * ratioX);
? ? ? ? ? ? ? ? var xLerp = x * ratioX - xFloor;
? ? ? ? ? ? ? ? newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
? ? ? ? ? ? ? ? ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
? ? ? ? ? ? ? ? y * ratioY - yFloor);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? mutex.WaitOne();
? ? ? ? finishCount++;
? ? ? ? mutex.ReleaseMutex();
? ? }
? ? private static Color ColorLerpUnclamped(Color c1, Color c2, float value)
? ? {
? ? ? ? return new Color(c1.r + (c2.r - c1.r) * value,
? ? ? ? c1.g + (c2.g - c1.g) * value,
? ? ? ? c1.b + (c2.b - c1.b) * value,
? ? ? ? c1.a + (c2.a - c1.a) * value);
? ? }
? ? public static void PointScale(System.Object obj)
? ? {
? ? ? ? ThreadData threadData = (ThreadData)obj;
? ? ? ? for (var y = threadData.start; y < threadData.end; y++)
? ? ? ? {
? ? ? ? ? ? var thisY = (int)(ratioY * y) * w;
? ? ? ? ? ? var yw = y * w2;
? ? ? ? ? ? for (var x = 0; x < w2; x++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? mutex.WaitOne();
? ? ? ? finishCount++;
? ? ? ? mutex.ReleaseMutex();
? ? }
? ? public static int GetFilesCount(DirectoryInfo dirInfo)
? ? {
? ? ? ? int totalFile = 0;
? ? ? ? //totalFile += dirInfo.GetFiles().Length;//獲取全部文件
? ? ? ? totalFile += dirInfo.GetFiles("*").Length;//獲取某種格式
? ? ? ? foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories())
? ? ? ? {
? ? ? ? ? ? totalFile += GetFilesCount(subdir);
? ? ? ? }
? ? ? ? return totalFile;
? ? }
? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? if (enter_defaut==false)
? ? ? ? {
? ? ? ? ? ? defaout_action();
? ? ? ? }
? ? }
}