コード例 #1
0
        public static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial, bool isPolygon)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return(null);
            }
            float     num           = sprite.rect.width;
            float     num2          = sprite.rect.height;
            Texture2D spriteTexture = SpriteUtility.GetSpriteTexture(sprite, false);

            if (!isPolygon)
            {
                PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)num, (int)num2, ref width, ref height);
            }
            SavedRenderTargetState state = new SavedRenderTargetState();
            RenderTexture          temp  = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);

            RenderTexture.active = temp;
            GL.sRGBWrite         = QualitySettings.activeColorSpace == ColorSpace.Linear;
            GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
            Texture texture = null;
            Vector4 vector  = new Vector4(0f, 0f, 0f, 0f);
            bool    flag    = false;
            bool    flag2   = false;

            if (spriteRendererMaterial != null)
            {
                flag  = spriteRendererMaterial.HasProperty("_MainTex");
                flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
            }
            Material material = null;

            if (spriteRendererMaterial != null)
            {
                if (flag)
                {
                    texture = spriteRendererMaterial.GetTexture("_MainTex");
                    spriteRendererMaterial.SetTexture("_MainTex", spriteTexture);
                }
                if (flag2)
                {
                    vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector(spriteTexture));
                }
                spriteRendererMaterial.SetPass(0);
            }
            else
            {
                material = new Material(Shader.Find("Hidden/BlitCopy"))
                {
                    mainTexture       = spriteTexture,
                    mainTextureScale  = Vector2.one,
                    mainTextureOffset = Vector2.zero
                };
                material.SetPass(0);
            }
            float num3 = sprite.rect.width / sprite.bounds.size.x;

            Vector2[] vertices  = sprite.vertices;
            Vector2[] uv        = sprite.uv;
            ushort[]  triangles = sprite.triangles;
            Vector2   pivot     = sprite.pivot;

            GL.PushMatrix();
            GL.LoadOrtho();
            GL.Color(new Color(1f, 1f, 1f, 1f));
            GL.Begin(4);
            for (int i = 0; i < sprite.triangles.Length; i++)
            {
                ushort  index   = triangles[i];
                Vector2 vector3 = vertices[index];
                Vector2 vector4 = uv[index];
                GL.TexCoord(new Vector3(vector4.x, vector4.y, 0f));
                GL.Vertex3(((vector3.x * num3) + pivot.x) / num, ((vector3.y * num3) + pivot.y) / num2, 0f);
            }
            GL.End();
            GL.PopMatrix();
            GL.sRGBWrite = false;
            if (spriteRendererMaterial != null)
            {
                if (flag)
                {
                    spriteRendererMaterial.SetTexture("_MainTex", texture);
                }
                if (flag2)
                {
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
                }
            }
            Texture2D textured2 = new Texture2D(width, height, TextureFormat.ARGB32, false)
            {
                hideFlags = HideFlags.HideAndDontSave
            };

            textured2.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
            textured2.Apply();
            RenderTexture.ReleaseTemporary(temp);
            state.Restore();
            if (material != null)
            {
                Object.DestroyImmediate(material);
            }
            return(textured2);
        }