コード例 #1
0
        internal static Texture2D RenderVectorImageToTexture2D(UnityEngine.Object o, int width, int height, Material mat, int antiAliasing = 1)
        {
            var vi = o as VectorImage;

            if (o == null)
            {
                return(null);
            }

            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            RenderTexture rt        = null;
            var           oldActive = RenderTexture.active;

            var desc = new RenderTextureDescriptor(width, height, RenderTextureFormat.ARGB32, 0)
            {
                msaaSamples = antiAliasing,
                sRGB        = QualitySettings.activeColorSpace == ColorSpace.Linear
            };

            rt = RenderTexture.GetTemporary(desc);
            RenderTexture.active = rt;

            Vector2[] vertices       = null;
            UInt16[]  indices        = null;
            Vector2[] uvs            = null;
            Color[]   colors         = null;
            Vector2[] settingIndices = null;
            Texture2D atlas          = null;
            Vector2   size           = Vector2.zero;

            if (InternalBridge.GetDataFromVectorImage(o, ref vertices, ref indices, ref uvs, ref colors, ref settingIndices, ref atlas, ref size))
            {
                vertices = vertices.Select(v => new Vector2(v.x / size.x, 1.0f - v.y / size.y)).ToArray();
                var atlasWithEncodedSettings = vi.atlas != null?BuildAtlasWithEncodedSettings(vi.settings, vi.atlas) : null;

                VectorUtils.RenderFromArrays(vertices, indices, uvs, colors, settingIndices, atlasWithEncodedSettings, mat);
                Texture2D.DestroyImmediate(atlasWithEncodedSettings);
            }
            else
            {
                RenderTexture.active = oldActive;
                RenderTexture.ReleaseTemporary(rt);
                return(null);
            }

            Texture2D copy = new Texture2D(width, height, TextureFormat.RGBA32, false);

            copy.hideFlags = HideFlags.HideAndDontSave;
            copy.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            copy.Apply();

            RenderTexture.active = oldActive;
            RenderTexture.ReleaseTemporary(rt);

            return(copy);
        }