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 meshhat · Nov 05, 2014 at 03:00 AM · c#2d2d gametilemapuv mapping

How to apply UV to Mesh in 2D tilemap?

I am trying out example project from: http://studentgamedev.blogspot.no/2013/08/unity-voxel-tutorial-part-1-generating.html

I can render the pink square, but when I apply the UV, I don't see anything. The code is exactly as he has it, so I suspect the way I'm importing the image is incorrect.

Here is what I have done:

  • Right-click in project view, Import New Asset..., select the image.

  • Change settings on the image in Inspector View. (Note: tutorial author doesn't seem to have the Sprite Mode option due to an older Unity version. Mine is Unity 4.5 (latest) and was set to Single by default, but I changed it to None. Not sure if this is correct). Click APPLY.

alt text

  • Drag and drop the image to my game object. Run the code. alt text

As you can see, the result is just a blank (and very dark) square. No uv image seems to have been applied. How do I get this to work?

As a side question, I also played with the Sprite Editor. If I select Multiple for the Sprite Mode, I can create individual sprites by defining a grid on the spritesheet image. Is there a way to map these defined individual sprites to the mesh I'm generating in the code from the tutorial?

In other words, instead of importing the image the way the tutorial author has done it, could I use Unity's new Sprite Editor system to map the UV to the square mesh? If so, would it be very inefficient for mapping to many square meshes (would it scale to a large tiled world)?

EDIT Added code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 [RequireComponent(typeof(MeshCollider))]
 public class PolygonGenerator : MonoBehaviour {
     public List<Vector3> newVertices = new List<Vector3>();
     public List<int> newTriangles = new List<int>();
     public List<Vector2> newUV = new List<Vector2>();
     private Mesh mesh;
 
     public float tUnit = 0.25f;
     private Vector2 tStone = new Vector2(0,0);
     private Vector2 tGrass = new Vector2(0,1);
 
 
     // Use this for initialization
     void Start () {
         mesh = GetComponent<MeshFilter> ().mesh;
 
         float x = transform.position.x;
         float y = transform.position.y;
         float z = transform.position.z;
 
         newVertices.Add (new Vector3 (x, y, z));
         newVertices.Add (new Vector3 (x + 1, y, z));
         newVertices.Add (new Vector3 (x + 1, y - 1, z));
         newVertices.Add (new Vector3 (x, y - 1, z));
 
         newTriangles.Add (0);
         newTriangles.Add (1);
         newTriangles.Add (3);
         newTriangles.Add (1);
         newTriangles.Add (2);
         newTriangles.Add (3);
 
         newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y + tUnit));
         newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y + tUnit));
         newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y));
         newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y));
 
         mesh.Clear ();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray(); 
         mesh.Optimize ();
         mesh.RecalculateNormals ();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }


one.png (56.0 kB)
two.png (45.9 kB)
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 · Nov 05, 2014 at 03:00 AM 0
Share

Can we see your code where you build your mesh and set your uvs?

avatar image meshhat · Nov 05, 2014 at 03:38 AM 0
Share

@robertbu Sure, I've edited my post. It's nearly identical to the author's code except for the Require statements.

1 Reply

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

Answer by meshhat · Nov 05, 2014 at 05:53 PM

Solved. I was missing the material. I guess the tutorial implied its creation (I'm new to Unity. Thought I could directly apply the texture).

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

In my 2D platformer game, how would I create a height marker?, 1 Answer

Get a tile relative to its position in the world 1 Answer

How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer

Firing Projectiles in the same direction as my character is looking. 1 Answer

Box collider 2d detecting collisions while not actually colliding 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