Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
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.

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

Update Texture type to GUI during runtime 0 Answers

Same BackGroung For All Screen Resolution 1 Answer

Empty spaces in buttons are detected by mouse 2 Answers

Update Texture type to GUI during runtime 0 Answers

Update Texture type to GUI during runtime 1 Answer


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