- Home /
GUI.DrawTexture not working
This must be the easiest question in this forum.
I have a GUI.DrawTexture that works perfectly in the OnGUI function but when I move it to a different function and then call that function from a GUI.Button, it doesn't display.
I know the function is being called because I use audio.PlayOneShot in that function and the sound plays.
Any ideas?
I just read the Graphics.DrawTexture is for Unity Pro ONLY
After lots and lots and lots of searching, it appears that GUI.(etc) can only be used in OnGUI unless you do the class solution below. However, I don't necessarily have to use GUI.DrawTexture. I just want to place an image (texture) at a particular point in front of my GUI.Texture that acts as a full screen background. Once I place the image I want to move and rotate it to another point. Then I want to flip it to reveal a new image (texture) on the other side, and lastly move it to yet another point. How do I place an image infront of my background?
Answer by Justin Warner · Dec 07, 2010 at 02:18 AM
It might be different resolutions... Try it with the button in the upper left corner...
http://forum.unity3d.com/threads/37439-solved-GUI-from-outside-OnGUI(-)
Also, that, but I thought you could without it being on OnGui, maybe check out:
http://www.unifycommunity.com/wiki/index.php?title=PauseMenu
They do function calls in gui.
Thanks for the links. I have seen those but I'm still looking for an easy solution. The class seems good but maybe there is a better way.
It seems to me that I should be able to put a Texture in my scene dynamically without having to do a lot of coding.
If I can call an audio when I click a GUI Button, why not add a graphic in one line too.
What about Graphics.DrawTexture? I've put that in and I get the same result.
Answer by denewbie · Dec 07, 2010 at 05:38 PM
I'm guessing that it probably because it did display. Rather, it only displayed once and that was when you called that so called function. If you want it do display from another function upon a mouse click try the method below.
// I'm using C# a lot these days so just bare with it. The ideas is that we get the right concept.
// We're gonna make use of a boolean to control the printing of the texture.
bool isPrinting = false;
// Then we have our OnGUI as normal
void OnGUI(){
if ( GUI.Button( new Rect(0,0,50,50), "Toggle") )
{
if (isPrinting)
isPrinting = false;
else
isPrinting = true;
if (isPrinting)
{
// Put your audio script here.
}
}
if (isPrinting)
printTexture();
}
void printTexture(){
GUI.drawTexture( new Rect(0, 50, 50,50), myTexture );
}
For 3D plane:
First off, GUI in Unity can't rotate or flip. the best it can do is to move and that requires quite some computing so you are right to go for 3D animation.
Secondly, I don't recommend using a plane. Rather, I'll recommend using a cube instead because comparatively, its much lighter in terms of polygon count.
Here's some script you can start off with to what you might want to do.
// First lets declare some configurable parameters
public float rotationCount = 1; public float rotationCount2 = 1;
public float moveSpeed = 1; public float rotateLapse =0; public float rotateLapse2 =0;
public Texture2D myTexture; public GameObject myPlaneObject;
public float moveTime = 2; private float timeLapse = 0;
public float moveTime2 = 2; private float timeLapse2 = 0;
bool isAnimating = false;
void OnGUI(){
if ( GUI.Button( new Rect(0,0,50,50), "Toggle") ) { if (isAnimating ) isAnimating = false; else isAnimating = true;
if (isAnimating )
{
// Reset all variable values
rotateLapse =0;
rotateLapse2 =0;
timeLapse = 0;
timeLapse2 = 0;
// Put your audio script here.
// Render and unrender your object accordingly
myPlaneObject.renderer.enabled = isAnimating; // Please make sure your attached object has a renderer.
}
} }
void Update(){ if (isAnimating ) AnimateObject(); }
void AnimateObject(){ // Put your audio script here.
// Lets do a move first. You can choose to translate left, right, up or up *-1 (down), forward or a combination. if ( timeLapse < moveTime) { myPlaneObject.transform.Translate(Vector3.left * moveSpeed * Time.deltaTime); timeLapse += Time.deltaTime; }
// Now lets rotate the object else if ( rotateLapse < rotationCount) { myPlaneObject.transform.Rotate(Vector3.left * Time.deltaTime); rotateLapse += Time.deltaTime; }
// Now lets flip the object by using the Rotate again again else if ( rotateLapse2 < rotationCount2) { myPlaneObject.transform.Rotate(Vector3.forward * Time.deltaTime); rotateLapse2 += Time.deltaTime; }
// Finally, Lets do a move again. else if ( timeLapse2 < moveTime2) { myPlaneObject.transform.Translate(Vector3.right moveSpeed Time.deltaTime); timeLapse2 += Time.deltaTime; } else { isAnimating = false; }
}
I haven't tested it but the idea is there.
This sort of worked. It does put the texture on the screen but now it plays the audio in a never ending loop and it updates the text infinitely. I suppose I can add some more code to prevent all this but I still need to move the texture and spin it, then flip it and move it again.
Adding a plane with a texture on it still seems like the best way. Thanks for the response.
Hi. I;ve edited my answer to suit your query. Hope it helps. The idea is there. IF there're any bugs you can just send me a comment of something. All the best to you. =)
Re 1: GUI can both rotate and flip, you just need to supply a transformation matrix to GUI.matrix, see http://unity3d.com/support/documentation/ScriptReference/GUI-matrix.html and http://forum.unity3d.com/threads/6954-video-intro?p=53105#post53105
Re 2: A plane, specifically a quad, is the way to go, admittedly the default Unity plane is 200 tris, so use a 2 tri plane ins$$anonymous$$d, you can either generate this like http://www.unifycommunity.com/wiki/index.php?title=CreatePlane or store a 3D model like http://code.google.com/p/ogldev/source/browse/trunk/tutorial30/quad.obj?spec=svn52&r=52
Your answer
Follow this Question
Related Questions
How to make foot to leave a print on snow? 1 Answer
GUI.DrawTexture shows in Unity but not in Build 1 Answer
GUITexture makes my image to low quality 1 Answer
How can I trace Alphabet 0 Answers