- Home /
How to hardcode 2D square textures into that C#
I know that I sound needy and in desperation. Well that's true. I'm fighting with it for a long time to be properly published in webplayer and I'm sleepy, tired and I lost hope.
Still - I beg for your help guys (and gals) to remove that RGB and use square textures instead. Thank you in advance.
One script (you need to attach this to camera):
using UnityEngine;
using System.Collections;
public class UnityTetrisMain : MonoBehaviour
{
TetrisGrid tGrid = new TetrisGrid();
// Use this for initialization
void Start ()
{
int nx = 20;
int ny = 20;
tGrid.SetNumBlocks(nx, ny);
tGrid.SetScreenSize(400, 400);
tGrid.SetScreenPosition(10, 10);
tGrid.blockTextures.InitBlockTextures(10, 10);
int c = 0;
int i, j;
for(i = 0;i<nx;i++)
{
for(j = 0;j<ny;j++)
{
c++;
if(c==8)c = 1;
tGrid.SetBlockColor(i, j, c);
}
}
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
tGrid.RenderGrid();
}
}
Second script:
using UnityEngine;
using System.Collections;
public class BlockTextures
{
//custom textures used for each possible color
Texture2D red_texture = null;
Texture2D light_blue_texture = null;
Texture2D dark_blue_texture = null;
Texture2D green_texture = null;
Texture2D yellow_texture = null;
Texture2D purple_texture = null;
Texture2D gray_texture = null;
//create a beveled texture with the main color in the center, and darker and lighter
//versions of the color for the upper and lower sides, similar to classic Tetris
public void CreateBeveledTexture(Texture2D tex, float r, float g, float b)
{
//get the dimensions of the texture
int w = tex.width;
int h = tex.height;
int i, j;
//fill the entire texture with the color
Color color = new Color(r, g, b, 1);
for(i = 0;i<w;i++)
{
for(j = 0;j<h;j++)
{
tex.SetPixel(i, j, color);
}
}
//now to add the "bevel" effect we just need make the edges
//lighter or darker using modified versions of the original color
//get a dark color for the lower right
float r2 = r-0.5f;//darken everything
float g2 = g-0.5f;
float b2 = b-0.5f;
if(r2<0)r2 = 0;//make sure nothing goes below 0
if(g2<0)g2 = 0;
if(b2<0)b2 = 0;
Color dark_color = new Color(r2, g2, b2);
//and a bright color for the upper left
float r3 = r+0.5f;//brighten everything
float g3 = g+0.5f;
float b3 = b+0.5f;
if(r3>1)r3 = 1;//make sure nothing goes above 1
if(g3>1)g3 = 1;
if(b3>1)b3 = 1;
Color light_color = new Color(r3, g3, b3);
//now set the top and bottom pixels
for(i = 0;i<w;i++)
{
tex.SetPixel(i, h-1, light_color);//light on the top
tex.SetPixel(i, 0, dark_color);//dark on the bottom
}
//then set the left and right sides
for(j = 0;j<h;j++)
{
tex.SetPixel(0, j, light_color);//light on the left side
tex.SetPixel(w-1, j, dark_color);//dark on the right side
}
tex.Apply();//update the texture
}
//initialize all block textures
public void InitBlockTextures(int w, int h)
{
red_texture = new Texture2D(w, h);
light_blue_texture = new Texture2D(w, h);
dark_blue_texture = new Texture2D(w, h);
green_texture = new Texture2D(w, h);
yellow_texture = new Texture2D(w, h);
purple_texture = new Texture2D(w, h);
gray_texture = new Texture2D(w, h);
CreateBeveledTexture(red_texture, 1, 0, 0);
CreateBeveledTexture(light_blue_texture, 0.25f, 0.25f, 1);
CreateBeveledTexture(dark_blue_texture, 0, 0, 1);
CreateBeveledTexture(green_texture, 0, 1, 0);
CreateBeveledTexture(yellow_texture, 1, 1, 0);
CreateBeveledTexture(purple_texture, 1, 0, 1);
CreateBeveledTexture(gray_texture, 0.5f, 0.5f, 0.5f);
}
//get the appropriate texture for the specified color
public Texture2D GetBlockTexture(int color)
{
if(color==1)return red_texture;
if(color==2)return light_blue_texture;
if(color==3)return dark_blue_texture;
if(color==4)return green_texture;
if(color==5)return yellow_texture;
if(color==6)return purple_texture;
if(color==7)return gray_texture;
return null;
}
};
public class TetrisGrid
{
//the number of blocks in the grid
public int num_xblocks = 0;//across
public int num_yblocks = 0;//and down
//the total number of blocks
public int total_blocks = 0;
//the size of the grid on the screen
public int screen_width = 0;
public int screen_height = 0;
//the starting position of the grid on the screen
public int screen_xpos = 0;
public int screen_ypos = 0;
//the size of a single block
public int block_width = 0;
public int block_height = 0;
//an array of integers for the block colors (0 is blank)
public int[] block_colors = null;
//the class to store the textures used for each possible color
public BlockTextures blockTextures = new BlockTextures();
//get the block color at the xy location
public int GetBlockColor(int x, int y)
{
if(x<0||x>=num_xblocks||y<0||y>=num_yblocks)//make sure we are in bounds
{
return 0;
}
return block_colors[(y*num_xblocks)+x];
}
//set the block color at the xy location
public bool SetBlockColor(int x, int y, int color)
{
if(x<0||x>=num_xblocks||y<0||y>=num_yblocks)//make sure we are in bounds
{
return false;
}
block_colors[(y*num_xblocks)+x] = color;
return true;
}
//see if the block is empty (0 is blank)
public bool BlockIsEmpty(int x, int y)
{
if(x<0||x>=num_xblocks||y<0||y>=num_yblocks)//make sure we are in bounds
{
return false;
}
return block_colors[(y*num_xblocks)+x] == 0;
}
//set all the blocks to 0 (blank)
public void ClearBlocks()
{
for(int i = 0;i<total_blocks;i++)
{
block_colors[i] = 0;
}
}
//the starting screen X position for the blocks at column "x"
public int BlockXPos(int x)
{
return screen_xpos + block_width*x;
}
//the starting screen Y position for the blocks at row "y"
public int BlockYPos(int y)
{
return screen_ypos + block_height*y;
}
//recalculate the size of the blocks depending on screen size and the number of blocks
public void ReCalcBlockSize()
{
block_width = screen_width/num_xblocks;
block_height = screen_height/num_yblocks;
}
//initialize the grid with some number of blocks across and down
public void SetNumBlocks(int nx, int ny)
{
num_xblocks = nx;
num_yblocks = ny;
total_blocks = nx*ny;
block_colors = new int[total_blocks];
ReCalcBlockSize();
}
//set the screen size of the grid
public void SetScreenSize(int sx, int sy)
{
screen_width = sx;
screen_height = sy;
ReCalcBlockSize();
}
//set the starting screen position of the grid
public void SetScreenPosition(int px, int py)
{
screen_xpos = px;
screen_ypos = py;
ReCalcBlockSize();
}
//render a single block
public void RenderBlock(int x, int y)
{
//get the texture for the block color
Texture2D tex = blockTextures.GetBlockTexture(GetBlockColor(x, y));
//set the GUI.Box style
GUI.skin.box.normal.background = tex;
//render a box
GUI.Box(new Rect(BlockXPos(x), BlockYPos(y), block_width, block_height), "");
}
//render a background for the entire grid, and any blocks that are not blank
public void RenderGrid()
{
//save the current texture used for GUI.Box (we need to change this for block rendering)
Texture2D old_texture = GUI.skin.box.normal.background;
int border = 5;//add a border so we can render a background box
//render a normal GUI.Box as a background
GUI.Box(new Rect(screen_xpos-border, screen_ypos-border, screen_width+(border*2), screen_height+(border*2)), "");
//now render all the blocks that have some type of color
int i, j;
for(i = 0;i<num_xblocks;i++)
{
for(j = 0;j<num_yblocks;j++)
{
if(!BlockIsEmpty(i, j))
{
RenderBlock(i, j);
}
}
}
//set the normal GUI.Box texture back to the default
GUI.skin.box.normal.background = old_texture;
}
}
Tried www but I guess it was wrong assumption to use it.
What were you trying to accomplish? It's not clear from your question.
Answer by DyrdaOrg · Oct 11, 2013 at 06:39 AM
Okay... All night later: The solution was simple (as always when the thing is done)
in 85 line you must replace code with:
public Texture2D GetBlockTexture(int color)
{
if(color==01)return Resources.Load("Textures/01") as Texture2D;
if(color==02)return Resources.Load("Textures/02") as Texture2D;
//...
Where textures are in Assets/Resources/Textures/01.png
I hope that someone will be in some future as happy as me when I found that.
Your answer
Follow this Question
Related Questions
Combine Array of Sprites to Form One Sprite 0 Answers
Change texture of plane pixel by pixel (Unity 5) 0 Answers
Assigning script-generated textures 0 Answers
Why the gui texture is not in the middle of the screen and how can i change the texture size ? 1 Answer
Scroll using material.mainTextureOffset makes the texture very distorted 1 Answer