Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
4
Question by qwertyp · Jun 04, 2011 at 09:25 PM · texturematerialtile

Auto tile texture

I'm making a 2.5D platformer. Things are going well, except the ground texturing. I want to be able to make a cube and scale it however I want with the texture auto-tiling to keep in from stretching. In other words, I want to tile, not stretch, and don't want to set the tiling manually, because I have many objects with the same texture and don't want to make a ton of material files with different tiling settings. I was thinking a script may be able to do this... Thanks.

Comment
Add comment · Show 1
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 macdude2 · Jun 04, 2011 at 09:48 PM 1
Share

I was wondering this too.

1 Reply

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

Answer by qwertyp · Jun 05, 2011 at 01:25 AM

 var ScaleToTiles : float = 0.667;
 
 function Start () {
     renderer.material.mainTextureScale.x = transform.lossyScale.x*ScaleToTiles;
     renderer.material.mainTextureScale.y = transform.lossyScale.y*ScaleToTiles;
 }
 

Here is the script that I ended up using. It works great for me. I never change the z scale of my object, but you should be able to modify this script as necessary.

Comment
Add comment · Show 3 · 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 Alaric · Dec 12, 2012 at 01:31 PM 0
Share

Can anyone provide more details on how and where to implement this script?

avatar image guru7892 · Apr 21, 2013 at 01:58 AM 0
Share

Here is the full code in c#

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class AutoTileTexture : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void OnDrawGizmos()
     {
     
     this.gameObject.renderer.material.SetTextureScale("_$$anonymous$$ainTex",new Vector2(this.gameObject.transform.lossyScale.x,this.gameObject.transform.lossyScale.y))  ;
     }
 }
 

-Edit: use material to set things up, however you will get an error about leaking materials, change this.gameObject.renderer.material... to this.gameObject.renderer.shared$$anonymous$$aterial... to stop unity from complaining;

need to find a work around for that...

avatar image Anisoropos · Nov 30, 2015 at 07:38 PM 0
Share

Had some trouble when using this on walls and other flat objects - I extended the script from @qwertyp so you can specify which two dimensions should be used to calculate the scaling.

The actual update of the materials is only executed at runtime to prevent the material leak error mentioned by @guru7892 in his comment. The obvious downside is that you can only preview your changes after hitting play.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class AutoTile : $$anonymous$$onoBehaviour
 {
     public float scaleToTiles;
 
     public Dir dir;
 
     void OnDrawGizmos()
     {
         float scaleX = 1;
         float scaleY = 1;
 
         switch (dir)
         {
             case Dir.XY:
                 scaleX = transform.lossyScale.x;
                 scaleY = transform.lossyScale.y;
                 break;
             case Dir.ZY:
                 scaleX = transform.lossyScale.z;
                 scaleY = transform.lossyScale.y;
                 break;
             case Dir.XZ:
                 scaleX = transform.lossyScale.x;
                 scaleY = transform.lossyScale.z;
                 break;
         }
         if (!Application.isEditor || Application.isPlaying)
         {
             GetComponent<Renderer>().material.SetTextureScale("_$$anonymous$$ainTex",
                 new Vector2(scaleX * scaleToTiles, scaleY * scaleToTiles));
         }
     }
 }
 
 public enum Dir { XY, ZY, XZ}

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple random tiled textures in one material 1 Answer

Using Rpg maker XP Tiles (2D Tile System) 2 Answers

dynamic images for textures ar runtime 2 Answers

Changing texture of childrens 2 Answers

How can I swap an object's texture when I click? 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