unity 壓縮

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

? ? ? ? }

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市伊履,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌隅熙,老刑警劉巖哲戚,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異杨蛋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門逞力,熙熙樓的掌柜王于貴愁眉苦臉地迎上來曙寡,“玉大人,你說我怎么就攤上這事寇荧【偈” “怎么了?”我有些...
    開封第一講書人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵揩抡,是天一觀的道長(zhǎng)户侥。 經(jīng)常有香客問我,道長(zhǎng)峦嗤,這世上最難降的妖魔是什么蕊唐? 我笑而不...
    開封第一講書人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮烁设,結(jié)果婚禮上替梨,老公的妹妹穿的比我還像新娘。我一直安慰自己装黑,他們只是感情好副瀑,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著恋谭,像睡著了一般糠睡。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上疚颊,一...
    開封第一講書人閱讀 52,696評(píng)論 1 312
  • 那天狈孔,我揣著相機(jī)與錄音,去河邊找鬼串稀。 笑死除抛,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的母截。 我是一名探鬼主播到忽,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼清寇!你這毒婦竟也來了喘漏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤华烟,失蹤者是張志新(化名)和其女友劉穎翩迈,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盔夜,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡负饲,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年堤魁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片返十。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡妥泉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出洞坑,到底是詐尸還是另有隱情盲链,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布迟杂,位于F島的核電站刽沾,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏排拷。R本人自食惡果不足惜侧漓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望监氢。 院中可真熱鬧火架,春花似錦、人聲如沸忙菠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽牛欢。三九已至,卻和暖如春淆游,著一層夾襖步出監(jiān)牢的瞬間傍睹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來泰國打工犹菱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拾稳,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓腊脱,卻偏偏與公主長(zhǎng)得像访得,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子陕凹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容