Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by A Lee · Sep 09, 2010 at 01:24 PM · guitexturepixelinset

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by qJake · Sep 09, 2010 at 08:02 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by A Lee · Sep 09, 2010 at 08:33 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by yoyo · Oct 14, 2010 at 07:00 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Juan Manuel Palacios · Jan 12, 2011 at 10:49 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Mike Terry · May 11, 2013 at 09:03 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike Terry · May 11, 2013 at 08:51 PM 0
Share

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.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Fade script working but I can't 'see' it working? (Solved) 1 Answer

Is there an issue when playing a Movie Texture as a GUITexture? 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Changing GUITexture's position through code (relative to screen) 1 Answer

GUItexture Borders & Pixel Inset concept 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges