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 hakermania · Jul 05, 2013 at 08:51 PM · javascripttexture

Show part of a Texture, from the bottom

I am using the following code inside my OnGUI function so as to show part of my texture:

 percentIndicatorRect.height = getPercentage() * indicator.height;
 GUI.BeginGroup(percentIndicatorRect);
 GUI.DrawTexture(indicatorRect, indicator);
 GUI.EndGroup();

The thing is that it is showing a percentage of the texture only from the top through the bottom of the image.

For example, if getPercentage returns 0.35, 35% of the image will be shown from its top through the bottom.

I've understood pretty well how the BeginGroup function works and I cannot think of a way that I can make it show a percentage of the image from the bottom.

I've tried several things, like changing the value of the percentIndicatorRect.y (but then the whole texture moves along with it, so there is no use).

I have an idea of altering the percentIndicatorRect.y to += (1-percentage)*percentIndicatorRect.height (so as to move the "container" down and now its x, y values to be the upper left edge of the part of the image that I want to show) and then to abstract this value from indicatorRect.y (and thus it will take a negative value) (so as to move the texture up and place it to the right location)

But I have not implemented this because it doesn't sound correct.

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 robertbu · Jul 06, 2013 at 12:08 AM 0
Share

You might want to take a look at Graphics.DrawTexture. It allows you to specify a source rect.

http://docs.unity3d.com/Documentation/ScriptReference/Graphics.DrawTexture.html

avatar image hakermania · Jul 06, 2013 at 08:50 AM 0
Share

Thanks for this comment. I tried to use DrawTexture with these parameters but I wasn't able to. I got an error that BCE0023: No appropriate version of 'UnityEngine.GUI.DrawTexture' for the argument list '(UnityEngine.Rect, UnityEngine.Texture, UnityEngine.Rect, int, int, int, int, null)' was found. while clearly this is the needed parameters list.

But, then I found what SWGraham said in this thread: http://forum.unity3d.com/threads/9771-DrawTexture-to-draw-part-of-a-texture (3rd from the bottom post)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by iwaldrop · Jul 05, 2013 at 09:44 PM

I would think the best way to do this is through a shader, not through the OnGUI method.

Comment
Add comment · Show 3 · 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 hakermania · Jul 05, 2013 at 10:15 PM 0
Share

Are you 100% sure that this cannot be done through GUI Grouping? I've managed to do the opposite from what I want to do :P (not that I've a particular problem with shaders, but I'm looking for a lightweight solution, apart from the fact that I haven't worked with shaders yet)

avatar image iwaldrop · Jul 05, 2013 at 10:32 PM 0
Share

I didn't say it couldn't be done using Unity GUI, I said it'd be better done using a shader. And, computationally speaking, shaders are much more performant than any script you can write in Unity, so I don't know what you mean by 'lightweight solution'.

avatar image hakermania · Jul 05, 2013 at 10:33 PM 0
Share

Ok! Thanks a lot! I will try to implement it with shaders and I will post my final solution to my specific problem here :)

avatar image
0

Answer by Jamora · Jul 06, 2013 at 09:58 AM

While iwaldrop's shader suggestion is a good one, your "doesn't sound correct" idea is what Unity suggests in the Unity Manual at http://docs.unity3d.com/Documentation/Components/gui-Layout.html .

Basically, you need to create the group you want the texture to show. Then any parts of the image that do not fit inside the group are clipped.

     void OnGUI () {
         GUI.BeginGroup (new Rect (0,0,256,32));{
             //only 256 pixels are drawn, rest are clipped.
             GUI.Box (new Rect (0,0,300,32), 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 hakermania · Jul 06, 2013 at 10:54 AM 0
Share

Thanks for the answer and yes, I got what Grouping allows me to do. But, as in my occasion, how would you rewrite the code that you posted so as to clip the image from the right and not from the left?

avatar image Jamora · Jul 06, 2013 at 12:50 PM 0
Share

Use negative coordinates for x inside the group. The coordinates inside groups are calculated relative to the coordinates of the Rect of the group. so GUI.Box (new Rect (-30,0,300,32), texture); would clip texture by 30 pixels from the left and 14 from the right.

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

17 People are following this question.

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

Related Questions

How to make a Texture change in GUI.Window 2 Answers

How to assign and find Textures by tag 1 Answer

Assigning UV Map to model at runtime 0 Answers

Not able to download textures from server... 0 Answers

Application.CaptureScreenshot on a Texture2D? 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