x


just started working with textures have bug

i want to build a function for a debuger i am working on,for this i want to make a grid 24*32 on the screen and show me in which square the mouse is at every point of time i want to color red the square. in order to do it i created a plane with a material red.png i set the alpha to 0. next i want that when my mouse is on the square to set the alpha of the square to 1 and restore to 0 if i leave the square.

my questions a) is there a better way to do it? b) what i did ,didnt work can anyone help me figuring out why?

thanks in advance and sorry for the poor english

using UnityEngine;
using System.Collections;

public class Bitmap : MonoBehaviour {

public  int Columns=32,Rows=24;
public int MouseCol,MouseRow;
 bool[] IsSquareColored;

    Texture2D texture;
    // Use this for initialization
    void Start () {

    IsSquareColored= new bool[Columns*Rows];



       texture=(Texture2D) this.transform.gameObject.renderer.sharedMaterial.mainTexture;
    }

    // Update is called once per frame
    void Update () {


       Vector2 MousePos= Input.mousePosition;    
       MouseCol= (int)(MousePos.x * Columns / Screen.width);
     MouseRow = (int)(MousePos.y *Rows /Screen.height);
    MouseCol=  Mathf.Clamp(MouseCol,0,Columns);
    MouseRow=  Mathf.Clamp(MouseRow,0,Rows);

    // Paint red

    for (int i=0;i<Rows;i++)
       {
         for ( int j=0;j<Columns;j++)
         {
          if (!IsSquareColored[ (MouseRow-1)*(Columns)+MouseCol] && (MouseCol==j )&& (MouseRow==i)){
              Paint(MouseRow,MouseCol);  
          }
          else  if (IsSquareColored[ (MouseRow-1)*(Columns)+MouseCol] && ((MouseCol!=j ) || (MouseRow!=i))){
              Unpaint(i,j);
          }
         }
       }

    }

    void Paint(int R,int C)
    {

       Color32[] colors= texture.GetPixels32();
       for (int i = (R*Screen.height/Rows);i<((R+1)*Screen.height/Rows);i++)
         for  (int j = (C*Screen.width /Columns);j<((C+1)*Screen.width /Columns);j++)
         colors[i*Screen.width+j].a=1;










    }
    void Unpaint(int R,int C){}

}
more ▼

asked Jun 14 '12 at 02:16 PM

blackpag gravatar image

blackpag
36 4 13 17

it is bizarre to approach the problem this way, man.

you could do it 10 thousand times more easily and more reliably simply by moving objects around, or something like that !

Jun 14 '12 at 03:24 PM Fattie

To all people who want to hear the conclusion. setpixel made my debuger work at 15 fps. setpixels improved it to 60. GUI.DrawTexture improved it to 150

Jun 19 '12 at 01:45 PM blackpag
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You don't appear to be setting the pixels back again after your paint function. GetPixels gives you an array that you can play with but you need to use SetPixels32 to set it back and then do a texture.Apply()

more ▼

answered Jun 14 '12 at 02:22 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

And it would probably be easier to make a texture that was the size of a block and just draw it in the right location when you need it. Especially if this is a 2D grid overlaid on the screen.

Jun 14 '12 at 02:24 PM whydoidoit

ok i understand you guys but what about the performance ? if i use it with system that supports two or more mouses isn't drawing more stuff will slow the system?

Jun 17 '12 at 08:03 AM blackpag

You would be better off doing what I said in my first comment to this answer for performance, but it will probably be ok. Looks like you are thinking is manipulating pixels which isn't going to be the fastest thing you could do with a system that is designed to paint areas of the screen quickly.

Jun 17 '12 at 09:18 PM whydoidoit

how do i do i draw the texture on location?

Jun 18 '12 at 11:20 AM blackpag

Well you can use GUI.DrawTexure or perhaps event GUI.SelectionGrid

Jun 18 '12 at 11:23 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x355
x239
x75

asked: Jun 14 '12 at 02:16 PM

Seen: 334 times

Last Updated: Jun 19 '12 at 01:45 PM