美文网首页
unity 压缩

unity 压缩

作者: MakerSir | 来源:发表于2020-11-03 15:57 被阅读0次

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)//如果不存在就创建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)//如果不存在就创建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 = "请选择文件路径";

        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)

    {

        //检查目标目录是否以目标分隔符结束,如果不是则添加之

        if (destFolderPath[destFolderPath.Length - 1] != Path.DirectorySeparatorChar)

            destFolderPath += Path.DirectorySeparatorChar;

        //判断目标目录是否存在,如果不在则创建之

        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(); //转换格式进一步压缩

            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))//如果不存在就创建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();

        }

    }

}

相关文章

  • Unity包尺寸优化

    注:Unity工程中用其他工具压缩png源图对优化包尺寸无效,必须在导入Unity后选择压缩格式 1.Unity ...

  • unity 压缩

    using System.Collections; using System.Collections.Generi...

  • 纹理压缩简介 DXT PVR ETC

    参考为什么需要纹理压缩移动端纹理压缩格式干货:Unity游戏开发图片纹理压缩方案Creator使用压缩纹理常用纹理...

  • Unity ETC 压缩

    纹理压缩可以通过减少内存来显著地提高OpenGL的性能,使内存使用的效率更高。Android设备支持很多格式的纹理...

  • 【Unity】图片压缩

  • Unity 贴图格式及压缩后的图片像素的一些比较

    一、Unity支持的贴图格式1、png 是无损压缩 占用内存小 支持透明2、jpg 是有损压缩 占用内存小 ...

  • 纹理压缩格式

    JPG:有损压缩不透明 PNG:无损压缩有透明 这两个是被压缩过的,不是最原始的纹理。我们导入到unity后,会产...

  • NGUI图集压缩UIAtlasMaker修改

    阅读前请先了解下Unity3D 图片纹理格式Unity游戏开发图片纹理压缩方案 解决的问题? 包体过大 内存占用大...

  • Unity3D 图片纹理格式

    Unity3D 纹理格式 [Desktop] RGB Compressed DXT1 压缩的RGB纹理。这是最...

  • 【Unity】贴图压缩浅析

    写在前面 素材也是挤压了很久,之前在编写框架是选择使用texture packer打图集还是直接使用unity自带...

网友评论

      本文标题:unity 压缩

      本文链接:https://www.haomeiwen.com/subject/edxivktx.html