• 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
2
Question by Ricardo_Stefani · Aug 26, 2011 at 03:18 PM · guitexturetexture2d

Select part of the texture in GUI.DrawTexture

Hi guys, how are you? So, I need help to solution a problem in my game, how do I make to select just a part of the texture in GUI.DrawTexture and don't to show the rest? thanks a lot!

Comment
Add comment · Show 2
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 Rennat · Aug 26, 2011 at 04:15 PM 0
Share

can you elaborate please? Are you wanting to show half of the texture, a single sprite out of a sprite sheet, etc...?

avatar image Ricardo_Stefani · Aug 29, 2011 at 12:28 PM 0
Share

I'm wanting to show a part of the image. Example: A image with 300x200 I wanna show of the 0px to 50px and of the 0px to 80px

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Molix · Aug 29, 2011 at 02:33 PM

You could use a GUI.BeginGroup to crop it, e.g.

 public Texture2D texture;
 public Rect textureCrop = new Rect( 0.1f, 0.1f, 0.5f, 0.25f );
 public Vector2 position = new Vector2( 10, 10 );
     
 void OnGUI()
 {
     GUI.BeginGroup( new Rect( position.x, position.y, texture.width * textureCrop.width, texture.height * textureCrop.height ) );
     GUI.DrawTexture( new Rect( -texture.width * textureCrop.x, -texture.height * textureCrop.y, texture.width, texture.height ), texture );
     GUI.EndGroup();
 }
Comment
Add comment · Show 2 · 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 Ricardo_Stefani · Aug 29, 2011 at 11:20 PM 0
Share

Thanks guy!!!

avatar image Gurc · Apr 01, 2013 at 05:15 PM 0
Share

Excellent answer

avatar image
3

Answer by IsGreen · Jun 13, 2014 at 07:49 PM

Using GUI.DrawTextureWithTexCoords

Parameters

position: Represent position and "total" size of texture. texCoords: Use percentage per unit. Relative Left-Lower corner, and relative scale.

Axes Origin

OnGUI origin is Left-Upper corner: position parameter use this origin. But, in DrawTextureWithTexCoords the origin is Left-Lower corner: texCoords use this origin.

With texCoords parameter you can crop(percentage between 0-1) or tile image (width or height upper to 1).

To maintain aspect, also you must change "total" size of texture(position parameter).

Before this, you need save texture initial size(width,height).

 using UnityEngine;
 using System.Collections;
 
 
 [ExecuteInEditMode]
 public class NewBehaviourScript : MonoBehaviour {
     
     public Texture image;
     
     
     [Range(0f,1f)]
     public float x=0f;
     
     [Range(0f,1f)]
     public float y=0f;
     
     [Range(0f,1f)]
     public float width=1f;
     
     [Range(0f,1f)]
     public float height=1f;
     
     
     float initialWidth,initialHeight;
     bool imageEnabled=false;
     
     void Update(){
         
         if(!this.imageEnabled) if(this.image!=null){
             
             this.initialWidth = this.image.width;
             this.initialHeight = this.image.height;
             this.imageEnabled=true;
             
         }
         
     }
     
     void OnGUI(){
         
         if(imageEnabled){
             
             Rect position = new Rect(0f,0f,this.initialWidth*this.width,this.initialHeight*this.height);
             Rect coords = new Rect(this.x,this.y,this.width,this.height);
             GUI.DrawTextureWithTexCoords(position,this.image,coords);
             
         }
         
     }
     
 }




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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to script GUI Texture Window to see it in Script (Object's component) 1 Answer

Making texture cover whole button 1 Answer

How do I take maximum screenspace for Gui 1 Answer

Offset detail texture in c# 1 Answer

Tile and offset a Texture2D? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges