There is the following situation: in Unity - I have some material containing a certain texture. There is the following task: when scrolling with the mouse over a game object with this material - draw lines on the object - that is, as in Paint - but in realTime and on any object on the stage. To do this, I added a script to the object containing the Draw method:

public void Draw ()

{ Vector2 MouseVector = new Vector2(OnMouseUp(),OnMouseDown()); Vector2 ImageVector = new Vector2( MainSHDtexture.width,MainSHDtexture.height); for (int i = 0; i < ImageVector.magnitude; i++) { if (MouseVector.x<=ImageVector.x && MouseVector.y <= ImageVector.x) { int K = System.Convert.ToInt32(MouseVector.y); int L = System.Convert.ToInt32(MouseVector.x); for (K++; i < ImageVector.x; i++) { for (L++; i < ImageVector.y; i++) { Texture2D tx = (Texture2D)MainSHDtexture; tx.SetPixel(K , L , Color.white); tx.Apply(); MainSHDtexture = tx; } } } else if (MouseVector.y >= ImageVector.x ) { int K = System.Convert.ToInt32(MouseVector.y); for (K++; i < ImageVector.y; i++) { Texture2D tx = (Texture2D)MainSHDtexture; tx.SetPixel(K , System.Convert.ToInt32( ImageVector.x), Color.white); tx.Apply(); MainSHDtexture = tx; } } else if (MouseVector.x <= ImageVector.y) { int K = System.Convert.ToInt32(MouseVector.x); for (K++; i < ImageVector.y; i++) { Texture2D tx = (Texture2D)MainSHDtexture; tx.SetPixel(K , System.Convert.ToInt32(ImageVector.y) , Color.white); tx.Apply(); MainSHDtexture = tx; } } } 

}

The idea of ​​the script in the following is something similar to IntervalTree - let the mouse move can be set as a vector - where its beginning is the click of a button, and its end is, respectively, the case when we release the mouse button.

Respectively in OnMouseUp() и OnMouseDown() - I'm returning the coordinates of the mouse X and Y

Similarly, in the form of a vector, you can also set a texture - starting from the width - ending with the height - you will get one segment of the total length.

Ie the task itself comes down to replace the pixels from the ImageVector vector with all the pixels of the MouseVector length. I assume that these vectors do not intersect, but are parallel, and the MouseVector lies “above” ImageVector — and the projection of the MouseVector is the same number of pixels needed for replacement.

Further, I consider three cases - the full projection of the MouseVector in the ImageVector, the projection of only the MouseVector's head to the beginning of the ImageVector and the projection of the MouseVector's tail in the ImageVector.

I also want to note that K++ is made from hopelessness - because only increment, indirection, and creation of a new object are allowed.

Studio and Unity do not issue errors, but the script does not perform the task set - that is, it does not detect the desired pixels of the texture in the desired color.

Also I looked under Debug using breakpoints - the script enters the second if block (the one that else if) when executed.

Writing / reading in the properties of the texture - when importing - resolved.

What is the problem?

  • Check that your reference to the texture in the script corresponds to the link that is used when drawing in the scene. Perhaps you have a copy of the material created, and therefore your changes are not visible from the outside, that they are made in an object that we do not see. Pause at the time of execution and go into the texture of the material you are working with, see if changes are recorded in it. - KingPeas

2 answers 2

After you apply SetPixel or SetPixels to a texture, you must call the Texture2D.Apply () method to write your changes to the texture. If you do not call this method, then you will not see any changes.

Also, I want to note that applying SetPixel or SetPixels to texutras will only work for textures without compression. That is, for example, for compressing PVRTC (iOS native compression), these methods will not work and you will get an error in the console.

  • Thanks for the answer - I applied Apply as you advised, but the result did not work. - BadCatss

Prompted the solution - this is where the problem was:

 Vector2 MouseVector = new Vector2(OnMouseUp(),OnMouseDown()); 

they won't work together