- Home /
Automatically resize quad to match texture, in the editor
Maybe I'm not searching the right terms, but idk where to even start here... I want to build a simple character editor for a 2D game.
I previously built a prototype version. It would look at its material's texture at runtime, and set the quad's XY scale to a multiple of the pixel width/height of the tex. That worked well until I started trying to make pieces in many different sizes. For example, a tall helmet would cover the char's eyes, short hair and long hair would appear in different places, etc.
So then I wrote a bunch of code to try and auto-align the parts, but it's not a viable solution. What I want to do is align everything in the editor, on separate quads, and then just copy them at runtime according to a character's visual definition.
But to do this, every single quad I throw a texture onto will need its scale set by hand to look the same as it would in-game. Is there any way I can have a quad resize itself in the editor when I change its material/texture?
Thanks for any guidance!
Answer by callen · May 23, 2014 at 08:13 PM
If I'm doing everything right here, I found a good solution. I didn't know about the ExecuteInEditMode attribute, but a small script with this does a fine job. So after that all I did was attach the material to the script instead of the MeshRenderer, and copied it over in Update:
void Update()
{
if (renderer.sharedMaterial != TexMat)
{
if ((renderer.sharedMaterial = TexMat) != null)
transform.localScale = PixelScale * new Vector3(TexMat.mainTexture.width, TexMat.mainTexture.height, 1 / PixelScale);
}
}
At first I assigned to renderer.material, but got some kind of error which led me to using sharedMaterial. However idk what the difference means for me at runtime, I may have to add more code later.