• 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 radiKal07 · Jul 25, 2014 at 12:37 PM · texturescale

How to make a texture fit on all objects

Hi! I am working on a 2D platformer. I have multiple squares of different size which represent the "ground". They all use the same texture but all of them look different. Check the image: http://i.imgur.com/Y27Jxxx.jpg How can I do to make them all look the same like the one at the bottom ?

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
1
Best Answer

Answer by robertbu · Jul 25, 2014 at 04:03 PM

There is a hierarchy of solutions for this problem

  • Material for each object (cumbersome, lower performance)

  • Script that allows you to adjust the tiling (lower performance)

  • Script that automatically adjusts the tiling (lower performance)

  • UV mapping the textures in a modeling program (higher performance)

  • UV mapping textures in code (higher performance)

  • Shader solution (highest performance)

And there are a few other solutions out there as well. For a PC game, or a mobile game with just few object, lower performance solutions are unlikely to slow the game down. Here is a bit of code for the third solution. Attach the script to each object with the material. It takes the scaling from the x and y size of the object. If the 'y' size is near 0.0, then it uses x and z. The scaling is done at runtime. Note this is not a solution for 3D objects with non-uniform scaling.

 #pragma strict
 
 var factor = 1.0;
 
 function Start() {
     var mf = GetComponent(MeshFilter);
     if (mf != null) {
         var bounds = mf.mesh.bounds;
         
         var size = Vector3.Scale(bounds.size, transform.localScale) * factor;
         
         if (size.y < .001)
             size.y = size.z;
         
         renderer.material.mainTextureScale = size;
     }
 }

The 'factor' variable allows you to make individual adjustments on a object by object basis if you want to.

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 radiKal07 · Jul 25, 2014 at 04:46 PM 0
Share

I LOVE YOU SO $$anonymous$$UCH !!!!

It works like a charm. Thank you so much man !

avatar image
1

Answer by gcoope · Jul 25, 2014 at 02:05 PM

If you find the material component on the objects, it will have a couple of text boxes where you can set Tiling and Offset. for your issue you'll want to make sure the Tiling is turned down low.

For your case I'd say try set tiling to something like x = 2 and y = 5, and see what it looks like, then just change them until it looks correct.

Comment
Add comment · Show 5 · 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 radiKal07 · Jul 25, 2014 at 03:02 PM 0
Share

Not working. For example I have the basic ground looking good. If I change the tiling so other objects look good the ground will start looking bad.

avatar image Owen-Reynolds · Jul 25, 2014 at 03:27 PM 0
Share

That's because they share a material, so changing one changes them all. $$anonymous$$ake a new $$anonymous$$aterial for each different way you want to tile.

But, I'm thinking that if you didn't already know that, and you don't understand UV mapping, you're going to have troubles. Working with the models in Unity, it's kind of assumed you've learned modelling basic concepts in $$anonymous$$ax/$$anonymous$$aya/blender. Unity deliberately copies the way those programs work and think.

avatar image radiKal07 · Jul 25, 2014 at 03:32 PM 0
Share

$$anonymous$$aking a new $$anonymous$$aterial for each tile won't be inefficient ? Can you point me to some UV mapping tutorials ?

avatar image gcoope · Jul 25, 2014 at 03:47 PM 0
Share

You wouldn't have a new material for each tile I don't believe.

Looking at your picture, I can see 3 kinds of objects:

  • The ground

  • Horizontal platform

  • Vertical platform

For your ground, you can keep that material as it is - maybe rename it to Ground$$anonymous$$aterial or Ground_mat so you can keep track.

Next you'll want to duplicate that material and call it something relevant to the horizontal platform, such as HorizontalPlatform$$anonymous$$aterial. With this new HorizontalPlatform$$anonymous$$aterial, apply that to the horizontal platform and then change the x and y tiling values as I mentioned beforehand so that it looks good. Then finally do the same for the vertical platform - duplicate the materials and edit it's tiling values.

avatar image radiKal07 · Jul 25, 2014 at 03:53 PM 0
Share

Yes but as I will add more levels I will have a lot of platforms of different size.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity -- iOs FPS dropdown on 0 Answers

How do I scale up a texture along with an ever-expanding underlying body ? 0 Answers

Non scale texture? 1 Answer

Animating a Texture2D using cells 1 Answer

I have a GUI element that highlights an onscreen object with a circle. This circle needs to scale according to the object's distance from the camera in order to compensate for perspective, but the thickness of the circle needs to stay the same. 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