Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Facundo · Apr 06, 2010 at 10:47 PM · texturefontblitting

Specs for Graphics.DrawTexture

I want to implement pixmap fonts in unity. The glyphs are specified in a png file and the file is loaded as a Texture2D.

I need some way to copy a small rectangle of the pixmap image containing a single character to a given position on the screen. For that sake, I thought I could use

Graphics.DrawTexture (screenRect : Rect, texture : Texture, sourceRect : Rect, leftBorder : int, rightBorder : int, topBorder : int, bottomBorder : int, mat : Material = null) : void

but the documentation does not explain what the different parameters are for, and I could not figure them out by testing. When calling (boo code follows):

Graphics.DrawTexture(Rect(0,0,40,40),texture,Rect(0,0,40,40),0,0,0,0,null)

I would expect the top-left 40x40 pixels of my image to be copied to the top-left 40x40 pixels of the screen. However, all I get is flat gray where the pixels should appear.

Does somebody know how Graphics.DrawTexture is intended to work? Is there another way to blit a rectangle from a texture onto the screen?

I know the texture image is well imported because it looks fine when using this other version of DrawTexture, which does not allow to specify a rectangle within the texture:

Graphics.DrawTexture (screenRect : Rect, texture : Texture, leftBorder : int, rightBorder : int, topBorder : int, bottomBorder : int, mat : Material = null) : void

I thought also of creating one texture for each glyph and drawing that instead. But I think that's a less natural solution.

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

2 Replies

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

Answer by jonas-echterhoff · Apr 07, 2010 at 09:58 AM

Try specifying the srcRect parameters in texture coordinate space instead (ie: [0,0,1,1] for the complete texture).

Also, you might want to consider using Unity's built-in font rendering instead, by creating a custom font for your texture. Create an empty font asset from the Create Assets popup, and set up the texture, and the character positions. Then you should be able to use it in any GUI code like any imported font.

Comment
Add comment · Show 1 · 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 Facundo · Apr 07, 2010 at 06:09 PM 0
Share

Thanks a lot for the hint! So the sourceRect specifies texture coordinates from 0.0 to 1.0 in each dimension, being (0.0,0.0) the bottom-left corner.

I searched the web for documentation on using AssetsPopup-> Create -> Custom Font, but it seems to be even worse documented than DrawTexture. The blocker issues is, I think, how to associate to each character the corresponding area of the texture. I made some tests with it, not being able to get anything better than white squares on the screen ins$$anonymous$$d of letters.

avatar image
3

Answer by jamin1001 · Jun 18, 2010 at 03:47 AM

Here is an example. This draws the lower left third of the texture in texture coords (range is 0.0 to 1.0, x increases right and y increases up) with origin being in the bottom-left corner of the texture, into a rectangle at (200,100) pixel coords (origin being upper-left corner of the screen) with width and height of 128.

Now as far as pixel-perfect drawing, I haven't achieved that - i.e., there seems to be some stretching no matter what, when source texture dimensions match the destination rectangle dimensions (even trying off by one). Is there a trick to specify coords to avoid any stretching or blurring?

public Texture myTexture;

...

void OnGUI() { if (Event.current.type == EventType.Repaint) { Graphics.DrawTexture( new Rect(200, 100, 128, 128), myTexture, new Rect(0.0, 0.0, 0.3f, 0.3f), 0, 0, 0, 0); } }

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 jamin1001 · Jun 18, 2010 at 04:01 AM 0
Share

I found out how why my example was stretching. The original bitmap was 2048 x 2048, but the default for the texture in the inspector was to limit it to 1024 x 1024 and auto compress it. So the original big bitmap was not being drawn, but the compressed version.

avatar image Facundo · Jun 18, 2010 at 01:17 PM 0
Share

Thanks for the comments. I've been using the ARGB32 uncompressed texture format without any problems so far.

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

No one has followed this question yet.

Related Questions

Assigning UV Map to model at runtime 0 Answers

Cant get font type in textures 0 Answers

Custom score texture 2 Answers

Help with GUIstyles between platforms 1 Answer

How to assign font to material 0 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