- Home /
 
Auto tiling texture
Hello, i have a little problem. I adding texture after spawn cube for example but this cube spawn in not the same localScale.z . I need to have tiling of texture the same on all objects, how can i do this ? Here is picture how i have it. But i want the same texture on both of them . Thank you a lot. 
Answer by taxvi · Dec 29, 2014 at 01:54 PM
 transform.renderer.material.SetTextureScale("_MainTex", new Vector3(1f / transform.scale.z, 1));
 
 
               also try multiplying the z value with some coefficients if this does not work.
EDIT: fixed the code line
transform.renderer.material.SetTextureScale("_$$anonymous$$ainTex", new Vector2(rozdiel / 1000f, 0.01f)); //this working for me , thank you very much
Hey, what if I got the same problem but need to fix it in a EditorWindow-Script? I can´t use "transform" here!
         foreach (Object o in Selection.objects) {
             GameObject go = (GameObject)o;
 
             //for each selected object do:
             //do whatever
         }
 
                  I'm sorry to bother you taxvi, but I don't know how to use your hint. I would like to fix the size of the texture independent to the size of the object.
$$anonymous$$y c# Code:
 public class Editor_Change$$anonymous$$aterial_08_Link: EditorWindow 
 {    
     [$$anonymous$$enuItem("Add-on/Change material)")] 
     
     public static void Change()
     {
         GameObject SelectedObject = Selection.activeGameObject;    
         $$anonymous$$aterial[] materials = SelectedObject.GetComponent<Renderer> ().shared$$anonymous$$aterials;
         .
         .
         .
 
         Texture2D New$$anonymous$$apTex = Resources.Load ("Texture/" +common[c]+"_TextureLink") as Texture2D;  
         SelectedObject.GetComponent<Renderer> ().shared$$anonymous$$aterials[1].SetTexture("_$$anonymous$$ainTex", New$$anonymous$$apTex);
         
         
         // * Here I would like to fix the size of the texture
 
         //m.mainTextureScale = new Vector2(1f, 1f);            
         //m.mainTextureOffset = new Vector2(0.0f, 0.0f);            
     }
 }                    
 
                  i just can't figure it out :( thanks in advance!!
oh, then after the line
 SelectedObject.GetComponent<Renderer> ().shared$$anonymous$$aterials[1].SetTexture("_$$anonymous$$ainTex", New$$anonymous$$apTex);
 
                   add
 SelectedObject.GetComponent<Renderer> ().shared$$anonymous$$aterials[1].SetTextureScale("_$$anonymous$$ainTex", /* whatever the scale */);
 
                  I did this! -->
 SelectedObject.GetComponent<Renderer> ().shared$$anonymous$$aterials[1].SetTextureScale("_$$anonymous$$ainTex",  new Vector2(3f, 3f));
 
                    ... and it is still just tiling dependent to the size of the surface! I want my script to put the texture of bricks on surfaces with different sizes without changing the size of the bricks!
But still thanks a lot for your effort!! :)
cast the SelectedObject to a GameObject and then use gameObject.transform. should do it :/ but maybe I still don't understand what you need
Unfortunately it didn't work neither! I'm now trying something according to this: http://answers.unity3d.com/questions/756419/how-to-make-a-texture-fit-on-all-objects.html
 var factor = 1; 
 $$anonymous$$esh mf = SelectedObject.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh;
 if (mf != null)
 {
     Bounds bounds = mf.bounds;
 
     Vector3 size = Vector3.Scale(bounds.size, SelectedObject.transform.localScale) * factor;
     if (size.y < .001)
     {
         size.y = size.z;
     }
                                 
     m.mainTextureScale = size;
 }
 
                    I still don´t have full controll of the size of the texture but I´m getting closer :)
Answer by Stephanides · Dec 29, 2014 at 01:56 PM
Whats mean MainTex?
use "add new comment" under the answers for questions like this, and "_$$anonymous$$ainTex" is the shader's property name for its texture
well you can debug what's wrong there or look for something else :/
Your answer
 
             Follow this Question
Related Questions
Dynamically tiling texture on a cube of changing scale. 1 Answer
Texture is Tied to Two Cubes 1 Answer
Real Sized Texture Tiling 2 Answers
Assigning UV Map to model at runtime 0 Answers