• 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 meroon · Aug 16, 2010 at 03:19 AM · texturemeshuv

Setting custom UV Mapping

Hey All,

I've created a procedural hexagon mesh and I'm trying to texture it. When I do apply a texture it is completed messed up. I'm sure it's because I'm not setting up a proper UV map for the mesh. The problem is I'm not exactly sure how to go about setting it up. I understand how I would do it for a cube, but I'm puzzled how to go about it for a hexagon.

Here's the code where I setup the mesh. Thanks for the help in advance.

public class HexTile : MonoBehaviour { public float height = 0.5f; public float zWidth = 2;

 void Awake()
 {
     Mesh m = ((MeshFilter)gameObject.GetComponent("MeshFilter")).mesh;

     float angle = 360 / 6;     

     float b = zWidth / 2;
     float c = b / Mathf.Cos((Mathf.PI / 180) * (angle / 2));
     float a = b * Mathf.Tan((Mathf.PI / 180) * (angle / 2));        

     int[] pt = { 0,2,1, 0,3,2, 0,4,3, 0,5,4, 0,6,5, 0,1,6, //top
              7,8,9, 7,9,10, 7,10,11, 7,11,12, 7,12,13, 7,13,8, //bottom
              2,8,1, 2,9,8, //sides
              3,9,2, 3,10,9,
              4,10,3, 4,11,10,
              5,11,4, 5,12,11,
              6,12,5, 6,13,12,
              1,13,6, 1,8,13};

     Vector3[] pv = new Vector3[14];

     //top face
     pv[0] = new Vector3(0, height, 0);
     pv[1] = new Vector3(c, height, 0);
     pv[2] = new Vector3(a, height, b);
     pv[3] = new Vector3(-a, height, b);
     pv[4] = new Vector3(-c, height, 0);
     pv[5] = new Vector3(-a, height, -b);
     pv[6] = new Vector3(a, height, -b);


     //bottom face
     pv[7] = new Vector3(0, 0, 0);
     pv[8] = new Vector3(c, 0, 0);
     pv[9] = new Vector3(a, 0, b);
     pv[10] = new Vector3(-a, 0, b);
     pv[11] = new Vector3(-c, 0, 0);
     pv[12] = new Vector3(-a, 0, -b);
     pv[13] = new Vector3(a, 0, -b);

     m.vertices = pv;
     m.triangles = pt;

     m.RecalculateNormals();
     m.RecalculateBounds();

} }

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
-2

Answer by Robotron18 · Sep 26, 2010 at 12:49 PM

http://unity3d.com/support/documentation/ScriptReference/Mesh-uv.html

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 Yanger_xy · Jun 18, 2011 at 03:21 PM

Add these codes after this two line.

 m.vertices = pv;
 m.triangles = pt;

 int nVertices = m.vertices.Length;
 Vector2 uvs = new Vector2[nVertices];
 for(int i=0; i<nVertices; i++)
 {
    uvs[i] = m.vertices[i];
 }
 m.uvs = uvs;

Good luck!

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

1 Person is following this question.

avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Does Unity support multi-tile texture workflows? 0 Answers

Automatic mesh combine and texture atlasing 4 Answers

Apply texture to a mesh on all 3 sides 2 Answers

Change UV texture on certain tiles? 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