- Home /
How to i split a bitmap into parts? And use each part for a texture?
I am new!
I am creating a simple slider puzzle as a programming excersise. I have successfully created 15 cubes at runtime and stacked them into a 4x4 on the XY plane. I have a bitmap image 1024 by 1024 in my assets as a material whose scale is 1,1,1. Each cube is 1,1,1 as well. I want to paint the cooresponding part of the image onto each cube as they are instantiated in the camera's startup routine.
Any Ideas?
Thanx!
Answer by flamy · Dec 22, 2011 at 04:25 AM
You have to unwrap all fifteen cubes in maya and make it share the same texture!! that is the easiest!.. If u want to follow this search for unwraping in google u will get.
If you want to do it in unity u have set the UVs manually by using Material.textureOffset and Material.textureScale
Answer by JerryCic · Dec 22, 2011 at 10:09 PM
For the sake of others who find this thread.
To divide a square image into 16 parts (4wide by 4high) i did this:
using UnityEngine; using System.Collections;
public class InitCube : MonoBehaviour {
public GameObject ga;
float wide; // tiles wide
float high; // tiles high
// Use this for initialization
void Start () {
float x;
float y;
x = ga.transform.position.x;
y = ga.transform.position.y;
wide = 4;
high = 4;
float scx=-(1/wide);
float scy=-(1/high);
float offx=-(wide-1-x)/wide;
float offy=-(high-1-y)/high;
ga.renderer.material.SetTextureScale("_MainTex", new Vector2(scx,scy));
ga.renderer.material.SetTextureOffset("_MainTex", new Vector2(offx, offy));
}
}
Notice:
Think of a brick wall 4 bricks tall by 4 bricks wide.
Think of one graphic covering all 16 bricks such that the image is intact accross the entire wall.
Each brick gets a part of the image.
The GameObject is a cube whose scale is 1,1,1
The graphic is already a material on the cube surface.
position x=0,y=0 is the lower left corner of the structure
position 3,3 is the upper right brick.
In Unity, the material has a "Tileing" and an "Offset" properties.
In the c# code the SetTextureScale actually changes the "Tileing" property.
There you have it.