Example #1
0
        public void Generate(Texture2D texture, Rect rect, float scale)
        {
            currentScale = scale;
            m_width      = (int)rect.width;
            m_heigth     = (int)rect.height;
            int i          = 0;
            var xStart     = (int)rect.x;
            var yStart     = (int)rect.y;
            var tmpTexture = new Texture2D(m_width, m_heigth, TextureFormat.ARGB32, true);

#if UNITY_EDITOR
            var imp = (UnityEditor.TextureImporter)UnityEditor.AssetImporter.GetAtPath(UnityEditor.AssetDatabase.GetAssetPath(texture)) as UnityEditor.TextureImporter;
            if (imp != null && !imp.isReadable)
            {
                imp.isReadable = true;
                imp.SaveAndReimport();
            }
#endif
            for (int y = 0; y < m_heigth; y++)
            {
                for (int x = 0; x < m_width; x++)
                {
                    tmpTexture.SetPixel(x, y, texture.GetPixel(xStart + x, yStart + y));
                    i++;
                }
            }
            tmpTexture.Apply();
            if (scale < 1)
            {
                m_width  = (int)(m_width * scale);
                m_heigth = (int)(m_heigth * scale);
                if (texture.filterMode == FilterMode.Point)
                {
                    TextureResizing.Point(tmpTexture, m_width, m_heigth);
                }
                else
                {
                    TextureResizing.Bilinear(tmpTexture, m_width, m_heigth);
                }
            }

            List <Point> list = new List <Point>(m_width * m_heigth);

            for (int y = 0; y < m_heigth; y++)
            {
                for (int x = 0; x < m_width; x++)
                {
                    var p = tmpTexture.GetPixel(x, y);
                    if (p.a > 0)
                    {
                        var point = new Point(x, y, tmpTexture.GetPixel(x, y));
                        list.Add(point);
                    }
                }
            }
            m_points = list.ToArray();
#if UNITY_EDITOR
            if (m_iconPreview != null)
            {
                DestroyImmediate(m_iconPreview, true);
            }
            m_iconPreview           = tmpTexture;
            m_iconPreview.hideFlags = HideFlags.HideInHierarchy;
            UnityEditor.AssetDatabase.AddObjectToAsset(m_iconPreview, this);
#else
            DestroyImmediate(tmpTexture);
#endif
        }