Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DyrdaOrg · Oct 10, 2013 at 11:31 AM · c#texturetexture2dcolorsblocks

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;
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DyrdaOrg · Oct 10, 2013 at 02:07 PM 0
Share

Tried www but I guess it was wrong assumption to use it.

avatar image fernio · Oct 24, 2013 at 01:44 PM 0
Share

What were you trying to accomplish? It's not clear from your question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges