首页必威体育betway › unity 内存中切割图片

unity 内存中切割图片

貌似的说俺们切割图片是将一张图片资源切割成更小的图纸资源,也就是说在资源上就展开了切割,比如ugui上的切割形式。

一旦我们有一对情景比如做拼图,可能让玩家自己拔取自己的生活照作为拼图的原图。

这就是说我们需要展开在内存中展开切割

Texture2D ScaleTextureCutOut(Texture2D originalTexture, float startX,float startY, float originalWidth, float originalHeight)
    {
        originalWidth = Mathf.Clamp(originalWidth, 0, Mathf.Max(originalTexture.width - startX,0));
        originalHeight = Mathf.Clamp(originalHeight, 0, Mathf.Max(originalTexture.height - startY,0));
        Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
        int maxX = originalTexture.width - 1;
        int maxY = originalTexture.height - 1;
        for (int y = 0; y < newTexture.height; y++)
        {
            for (int x = 0; x < newTexture.width; x++)
            {
                float targetX = x + startX;
                float targetY = y + startY;
                int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
                int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
                int x2 = Mathf.Min(maxX, x1 + 1);
                int y2 = Mathf.Min(maxY, y1 + 1);

                float u = targetX - x1;
                float v = targetY - y1;
                float w1 = (1 - u) * (1 - v);
                float w2 = u * (1 - v);
                float w3 = (1 - u) * v;
                float w4 = u * v;
                Color color1 = originalTexture.GetPixel(x1, y1);
                Color color2 = originalTexture.GetPixel(x2, y1);
                Color color3 = originalTexture.GetPixel(x1, y2);
                Color color4 = originalTexture.GetPixel(x2, y2);
                Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
                                        Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
                                        Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
                                        Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
                                        );
                newTexture.SetPixel(x, y, color);
            }
        }
        newTexture.anisoLevel = 2;
        newTexture.Apply();
        return newTexture;
    }  

必威体育betway,这一个代码摘自网络上的,然后添加了序幕地方,逻辑是将每一个对应点的色素值取出来,放置在内存中的图片点上

前日在做应用的时候发现,图片切割的序幕地点是在左下角,于是,在做拼图或者其余娱乐,需要的时候要小心进行Y值的折算

还有此外的一些就是所使用的图纸假诺在unity中的境况,需要勾选允许读取和写入必威体育betway 1

再不在开展读颜色值的时候会产出错误。

做拼图遭受还有的一个问题是,让玩家接纳本地任意地方文件,参考 http://blog.csdn.net/awnuxcvbn/article/details/21277481 

末尾有觉察项目中用到图集,不过原图丢失了,然后在整理图集的时候不是特意有利,把A图集的一项删除之后不能放开B中。于是就需要拆迁原图

  [MenuItem("Tools/Resume Sprite From Atlas")]
    public static void ResumeSpriteFromAtlas()
    {
        Object[] go = Selection.objects;
        for (int i = 0; i < go.Length; i++)
        {
            if (go[i].GetType() == typeof(GameObject))
            {
                GameObject resObject = go[i] as GameObject;
                UIAtlas resAtlas = resObject.GetComponent<UIAtlas>();
                if (resAtlas != null)
                {
                    DecompressAtlas(resAtlas);
                }
            }
        }
    }

    public static void DecompressAtlas(UIAtlas resAtlas)
    {
        string mainPath = "D://OutSprites/" + resAtlas.name;
        if ( !System.IO.Directory.Exists(mainPath) )
        {
            System.IO.Directory.CreateDirectory(mainPath);
        }
        Material mainMaterial = resAtlas.spriteMaterial;
        Texture2D mainTexture = (Texture2D)mainMaterial.mainTexture;
        for(int i = 0; i < resAtlas.spriteList.Count; i++)
        {
            UISpriteData spData = resAtlas.spriteList[i];
            Texture2D newTexture = new Texture2D(spData.width, spData.height);
            Color[] needCopy = mainTexture.GetPixels(spData.x, mainTexture.height - (spData.y + spData.height), spData.width, spData.height);
            newTexture.SetPixels(0, 0, newTexture.width, newTexture.height, needCopy);
            newTexture.Apply();
            byte[] pngBytes = newTexture.EncodeToPNG();
            string filePath = mainPath + "/" + spData.name + ".png";
            Stream st = System.IO.File.Open(filePath, FileMode.OpenOrCreate);
            st.Write(pngBytes, 0, pngBytes.Length);
            st.Flush();
            st.Close();
            UnityEngine.Debug.Log(filePath);
        }
    }

 在untiy 编辑器下可以将入选的图集拆出原图,这样就又可以不管组合图集了

 

转载本站文章请注明出处:bway883.com https://www.piworx.com/?p=3998

上一篇:

下一篇:

相关文章

网站地图xml地图