• 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 kendyb · Apr 30, 2015 at 06:46 PM · c#meshfilter

How do you map UV coords to quad in script?

alt text

In my project I have the bitmap font above. Each letter is 32x32 pixels. I need to UV map each letter to a quad. To get the rectangle where the letter is positioned on the image I do the following:

 public Rect LetterRect(char letter)
 {

     int StartLetterValue = 32; // first letter is space (' ')

     int indicator = (int)letter - StartLetterValue;

     int left = (indicator % Columns) * CharWidth;

     int top = (indicator / Columns) * CharHeight;

     Rect letterRect = new Rect (left, top, left + CharWidth, top + CharHeight);

     return letterRect;
 }

After getting the rectangle, I try to UV map the letter onto a quad doing the following:

         // this is the quad
         GameObject pnl = (GameObject)Instantiate(Panel);

         // BitmapFont is my texture (512x192)
         float width = BitmapFont.width;
         float height = BitmapFont.height;

         // I get the letter rectangle here
         Rect r = fontUtil.LetterRect('A');

         // I calculate UVs based on image size
         float top = r.y/height;
         float left = r.x/width;
         float bottom = (r.height/height);
         float right = (r.width/width);

         // I set the texture to the panel
         pnl.renderer.material.SetTexture(0, BitmapFont);

         // I get the mesh
         Mesh mesh = pnl.GetComponent<MeshFilter>().mesh;

         // I set up my UVs
         Vector2[] UVs = new Vector2[4];

         UVs[0] = new Vector2(left, bottom); // left, bottom
         UVs[1] = new Vector2(right, top); // right, top
         UVs[2] = new Vector2(right, bottom); // right, bottom
         UVs[3] = new Vector2(left, top); // left, top

         // I set the UVs to the mesh
         mesh.mesh.uv = UVs;

         // set panel position to center screen
         pnl.transform.position = new Vector3(0, 0, 0);

My UV calculations come out terrible. If I set the UVs to values of 0 and 1 like below, then everything is fine; otherwise it doesn't work.

         UVs[0] = new Vector2(0, 1); // left, bottom
         UVs[1] = new Vector2(1, 0); // right, top
         UVs[2] = new Vector2(1, 1); // right, bottom
         UVs[3] = new Vector2(0, 0); // left, top

I know that my rectangle calculations are correct, but I am not sure where I am going wrong when calculating and setting the UVs.

font2.png (41.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Apr 30, 2015 at 06:53 PM

This should provide all the info you need.

Try to think about where each uv coordinate for any given vertex needs to be, for example to map the red X to a quad.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

C#, Creating a single mesh polygon problems 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