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
0
Question by Robert 1 · Mar 03, 2010 at 11:03 PM · guibox

Rendering a portion of a texture using GUI.Box()?

Hi,

I have a texture that is 256x64. I need to render from 0,0 to 63,63 in this texture (A 64x64 sub-rect). How can I do this using GUI.Box()?

I've tried specifying a GUIStyle that sets contentOffset to (0,0) and fixedWidth/fixedHeight to 64,64 but this didn't work.

Thanks.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by rlbaldi · Mar 22, 2010 at 02:36 PM

Take a look at Graphics.DrawTexture.

http://unity3d.com/support/documentation/ScriptReference/Graphics.DrawTexture.html

You can specify a material and, with so, UV coordinates. This way you can draw only a portion of your texture inside OnGUI calls. You must end up with something like it:

if (Event.current.type == EventType.Repaint) 
{
   Graphics.DrawTexture(screenRect, texture, new Rect(u, v, uWidth, vHeight), leftBorder, rightBorder, topBorder, bottomBorder, material);
}

Where

new Rect(u, v, uWidth, vHeight)

is your sourceRect (texture portion that you want to draw).

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 qJake · May 31, 2010 at 09:34 PM 0
Share

The Graphics class is only available in Unity Pro.

avatar image Kerel · Apr 16, 2014 at 08:30 PM 0
Share

You can use Graphics class in Unity Free :)

avatar image
1

Answer by Molix · Apr 19, 2010 at 04:54 PM

I think you can use GUI.DrawTexture, e.g. (C#):

// Edited - Sorry for the mistake. Here's an example of updating the pixels as described elsewhere: //OnGUI() //{ // GUI.DrawTexture( myRect, myTexture, ScaleMode.ScaleAndCrop ); //}

Color[] colors = textureToCrop.GetPixels( (int)cropRect.x, (int)cropRect.y, (int)cropRect.width, (int)cropRect.height ); croppedTexture = new Texture2D( (int)cropRect.width, (int)cropRect.height ); croppedTexture.SetPixels( colors ); croppedTexture.Apply( false );

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 qJake · May 31, 2010 at 09:33 PM 0
Share

That will still scale the texture, it will just crop if the aspect ratio of the new dimensions don't match. This won't work.

avatar image Molix · Jun 01, 2010 at 03:26 AM 0
Share

As specified in the question, the aspect ratio does not match. However, this centers the texture on the other axis, so is not applicable.

I'll edit above to remove the problem.

avatar image
0

Answer by naruse · Mar 22, 2010 at 02:13 PM

There are several ways, copy only that part of the texture to a new texture using GetPixels (or something like that I dont remember pretty well now).

another one is render to texture from another camera only that part of the texture (which I think is kinda hacky)

I would use the 1rst one I suggested you.

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 qJake · May 31, 2010 at 09:31 PM

Naruse was correct, although I will flesh out his answer a bit:

Use Texture2D.GetPixels to get the area of pixels you want. Then, create a new Texture2D object, and call Texture2D.SetPixels to write the extracted section of pixels to the new texture object. Then, call GUI.Box() or GUI.Texture() or whatever else you want to use, but using the new Texture2D object you created instead of the old one.

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 MorphVGX · Nov 14, 2011 at 09:19 PM

My question would be. Do I gain something from having all my textures in a single, let's say, png file then do this to create subtextures to apply? Or it is better if the textures are split by the artist?

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

2 People are following this question.

avatar image avatar image

Related Questions

When I show a picture on the GUI there is a square behind the picture 1 Answer

Is it possible to use two different GUI Boxes in GUI? 1 Answer

Help With Message System Script 1 Answer

Is Mesh Collider Optimal for GUI? 2 Answers

Make GUI box appear and disappear 2 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