Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having a problem with GUI.DrawTexture() and I'm looking an alternative function to draw 2D Texture Images. The problem I have is that my textures draw above all other objects and when my camera moves, the images move with the camera, keeping their relative position inline with the cameras position.

using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using UnityEngine;

public class AnimatedGifDrawer : MonoBehaviour
{
public string loadingGifPath;
public float speed = 1;
public Vector2 drawPosition;

List<Texture2D> gifFrames = new List<Texture2D>();
void Awake()
{
    var gifImage = Image.FromFile(loadingGifPath);
    var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
    int frameCount = gifImage.GetFrameCount(dimension);
    for (int i = 0; i < frameCount; i++)
    {
        gifImage.SelectActiveFrame(dimension, i);
        var frame = new Bitmap(gifImage.Width, gifImage.Height);
        System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
        var frameTexture = new Texture2D(frame.Width, frame.Height);
        for (int x = 0; x < frame.Width; x++)
            for (int y = 0; y < frame.Height; y++)
        {
            System.Drawing.Color sourceColor = frame.GetPixel(x, y);
            frameTexture.SetPixel(frame.Width - 1 - x, y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped
        }
        frameTexture.Apply();
        //frameTexture.LoadImage.
        gifFrames.Add(frameTexture);
    }
}

void OnGUI()
{
    GUI.DrawTexture(new Rect(drawPosition.x, drawPosition.y, gifFrames[0].width, -gifFrames[0].height), gifFrames[(int)(Time.frameCount * speed) % gifFrames.Count]);
}

}

The code above is designed to draw animated GIFs using System.Drawing.dll in Unity

share|improve this question

1 Answer 1

You need to use camera faced billboards in order to solve this issue. Which means that you need :

  • To use a 3D quad to display those textures (which you construct by code)
  • To give this 3D quad a 3D position, and a 3D size
  • To orient this 3D quad towards camera at each frame, either by code or in its vertex shader

Example to construct a billboard here And to orient toward camera here (may be a bit outdated) General doc on 3D billboards here

share|improve this answer
    
I don't want the Images to stay inline with the camera at all, I wish them to stay in their current positions in world space even when the camera moves –  Brendy Donnelly Aug 17 at 12:04
    
When I say "orient this 3D quad towards camera" I am only speaking about orientation, not position, so the quads will stay in their current position in world space. However you need to rotate the quad in order to "face camera" otherwise you will sometimes see the quad "by the side", imagine what is happening for example if the camera would be rotating around such billboard ... –  VB_overflow Aug 17 at 12:19
    
these links and this billboard solution, does not tell me how to assign a texture, Ive spend the last hour trying to assign the texture to a mesh by creating the mesh and the vertices and the meshfilter() and meshrenderer() but its problematic, its only rendering one triangle of the two and it distorting the image, as well as adding a black backround to the gif images which contain a transparent background.. –  Brendy Donnelly Aug 17 at 14:28
    
id expect a function like DrawTexture() to exist for 2d graphics –  Brendy Donnelly Aug 17 at 14:30
    
Sadly this does not exist to my knowledge, last time I used Unity (version 4), I had to implement myself such system of billboards. To solve your issue maybe what you could do is create a mapped textured quad with Blender or 3dsmax and then use that as your quad instead of trying to create it by code. Also check this link : docs.unity3d.com/Manual/HOWTO-UIWorldSpace.html, maybe this could work too.... –  VB_overflow Aug 17 at 14:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.