- Home /
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.
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.
Can anyone provide more details on how and where to implement this script?
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...
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
Follow this Question
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