• 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 t$$anonymous$$s texture (A 64x64 sub-rect). How can I do t$$anonymous$$s using GUI.Box()?

I've tried specifying a GUIStyle that sets contentOffset to (0,0) and fixedWidth/fixedHeight to 64,64 but t$$anonymous$$s 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 Grap$$anonymous$$cs.DrawTexture.

http://unity3d.com/support/documentation/ScriptReference/Grap$$anonymous$$cs.DrawTexture.html

You can specify a material and, with so, UV coordinates. T$$anonymous$$s way you can draw only a portion of your texture inside OnGUI calls. You must end up with somet$$anonymous$$ng like it:

if (Event.current.type == EventType.Repaint) 
{
   Grap$$anonymous$$cs.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 t$$anonymous$$nk 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 somet$$anonymous$$ng like that I dont remember pretty well now).

another one is render to texture from another camera only that part of the texture (w$$anonymous$$ch I t$$anonymous$$nk 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 $$anonymous$$s 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 somet$$anonymous$$ng from having all my textures in a single, let's say, png file then do t$$anonymous$$s 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

GUI box problem? 0 Answers

Change colour of GUI box, keep round corners. 0 Answers

How To Get Current Mouse Position and have a GUI Box on right clicked 2 Answers

How to display a rectangle around a player? 1 Answer

Custom GUI.Box function dilemma 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