Scripting GUITextures Issue

Hi,

I am trying to create a GUITexture entirely through script in C#, without using the editor. The way I am doing this is creating a new GameObject, attaching a GUITexture component to the GameObject, and then specifying the Texture2D image of the GUITexture. With this method, I am able to display the image on the screen. However, the sizing of the image, and it's screenspace coordinates are very strange, an I cannot figure out why. The image width is 480 pixels too wide, and the height is 320 pixels too tall. I understand that these are the dimensions of the screen, but I do not know why it is doing this. Also, if I place the GUITexture in the editor to test where they should be placed so I know what values to enter into the pixelInset of the script, they are completely different when the script is run.

Previously, I was creating prefabs with the GUITexture already attached, and then instantiating it through a script. When I was doing this, there was no issue at all. However, I do not want to use this method, as I do not want to rely on the Library folder to maintain my prefab connections.

Any ideas why this would be happening?

6 Answers

6

I think you need to set this value so Unity knows where to place your GUITexture:

GUITexture.pixelInset

The GUITexture is also dependent on the Transform component of the game object, so setting

textureGameObject.transform.position = Vector3.zero

would also be a good idea, that way you can configure the position solely using the pixel inset.

That's what I was doing, but the image is coming out massive, and not even close to it's actual dimensions. Here is some sample code of what I am doing to spawn one GameObject with a GUITexture attached:

using System;
using UnityEngine;

public class test : MonoBehaviour
{
    GameObject go;
    Texture2D texture;
    GUITexture guitexture;

    void Awake(){
        Initialize();

    }

    void Start(){

        go.transform.position = Vector3.zero;
        go.gameObject.AddComponent(guitexture.GetType());
        go.guiTexture.texture = texture;
        go.gameObject.guiTexture.pixelInset = new Rect(-160, -240, 320, 480);
    }

    private void Initialize(){
        go = new GameObject();
        texture = (Texture2D)Resources.Load("Textures/UI/Default");
        guitexture = new GUITexture();

    }
}

All of the required initialization is done in the 'Initialize()' function, then during the start method, I set the position of the GameObject, add the GUITexture to the object, and then set the texture of the GUITexture, and set it's pixelInset. However, the same issue persists.

In the editor, when I click on the texture, and create a GUITexture with the editor, the image comes out fine.

Set transform.localScale = Vector3.zero, and then your pixelInset will be in screen space pixels, with (0,0) at the bottom left.

A bit of an orthogonal contribution to your code: in lines like

go.gameObject.AddComponent(guitexture.GetType());

and

go.gameObject.guiTexture.pixelInset = new Rect(-160, -240, 320, 480);

you don't need the 'gameObject' link in the object chain, since the go variable is already of GameObject type and as such it'll have direct access to the AddComponent() method and to the guiTexture property, so you should be able to do:

go.AddComponent(guitexture.GetType());

and

go.guiTexture.pixelInset = new Rect(-160, -240, 320, 480);

My 2 cents, HTH!

  • jmpp

I tried this too, and the way I resolved it was to change the transform position and scale in the game tab, I first placed an empty game object at the (0,0,0) position, then in the game tab, I changed the transform position along the x, y axes and the transform scale also on the x, y axes to get the empty game object to the size I wanted. To do this programmaticly, just do that and note your settings and call them dynamically during your start function.

In the empty game object, I added the GUITexture too, so I could see this large blurred texture in the game tab in the lower left corner of the game tab.

–

If you’re importing the texture from the “Resources” folder be sure to set the Texture Type to “GUI” or Unity will resize the image to the closest power of 2.

Just find the image in the Project view and then check the Inspector to find the Texture Type.

Hope it helps :wink: