Search Unity

Question How to realize eraser mechanics in Unity?

Discussion in '2D' started by YokoShizuka, Feb 22, 2023.

  1. YokoShizuka

    YokoShizuka

    Joined:
    Jun 16, 2021
    Posts:
    5
    Hello everyone.

    I am newbie in Unity, and I try to learn by solving specific problems in my own game. I sorry if my question is stupid and easy to solve, but I really don't get it.
    I need to learn how to realize eraser mechanics. I have found these examples:
    https://answers.unity.com/questions/1546694/erasing-a-part-of-an-image-using-setpixel-without.html
    https://pastebin.com/Tcr4cuuu
    And there is the same mechanics in Red Strings Club


    But I don't understand how to make this script (the second url) work.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    The question contained in title of this thread is neither stupid nor is it easy to solve. I've been learning C# on my own for a few years now, and it would likely take me hours to dissect and comprehend this script: https://pastebin.com/Tcr4cuuu

    Generally, it's good to include what issues you're facing in making the script work; what you've tried, what errors you're getting, etc...

    However, I tried it out myself, and I got it working (mostly). Here's a step by step guide:

    1. I started by making a square sprite (project window -> right click -> create -> 2D -> sprites -> square).
    2. Click on the square in the project window -> look at its inspector window -> Advanced -> tick Read/Write.
    3. Add the square to the scene -> add the "DrawLines" script as a component and change the "Er Size" to 1.
    4. Give it a BoxCollider2D component and change the size of the collider to X 2.56 / Y 2.56.
    5. Make sure you have an orthographic camera with a negative z position.

    Hit play and you can erase the square by clicking and dragging. I don't know the specifics of how this works, or how to change the size of the sprite. All I managed to figure out was that you need a collider that matches the sprite's size to draw correctly on it.

    Edit: I was messing around and figured out that you can change the size of the square if you want and the collider will follow, you just need the collider size to remain at 2.56/2.56

    If you need more help, you'll have better luck in the scripting section: https://forum.unity.com/forums/scripting.12/
     
    Last edited: Feb 22, 2023
    YokoShizuka likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
  4. YokoShizuka

    YokoShizuka

    Joined:
    Jun 16, 2021
    Posts:
    5
    Thank you very much!!
    I am sorry I didn't write about errors I had got. I had these two:
     
  5. YokoShizuka

    YokoShizuka

    Joined:
    Jun 16, 2021
    Posts:
    5
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Always fix the first error first.

    What part of the error don't you understand?

    Did you mark your texture as read/write enabled?

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
  7. YokoShizuka

    YokoShizuka

    Joined:
    Jun 16, 2021
    Posts:
    5
    It seems like I understood explanations above. I am very grateful for the advice!
    Now I have a new issue...
    When I use a large image to use eraser, movements of the cursor become slow. It lookls like it's a performance issue (I'm not sure).

    Bunny83 suggested a way to improve the performance of the script:

    But I don't understand how to do it. Can someone explain please how to use mask mesh? And what does it mean "only apply the drawn lines" on practise, in script?
     
  8. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    This is what I do, and it works 99% of the time haha. I didn't understand the read/write error, but googling it gave me the answer in a few seconds.

    I'm pretty sure this involves modifying the script. At this point, you're asking people to write code for you, and that's not really the purpose of getting help on forums. Of course, I won't stop anyone if they want to do that for you, but it's beyond me. "Bunny83" was also just theorizing about improved performance, it may not even help that much once tested.

    I would highly recommend learning more about coding and Unity, and working your way up to this. Copy/pasting scripts you find online is fine, I do it sometimes myself; however, when you feel the need to start modifying things, you get stuck, which is what's happening to you right now. If it's a mechanic you're basing your entire game on, I recommend learning it.
     
    Last edited: Feb 23, 2023
    YokoShizuka likes this.
  9. YokoShizuka

    YokoShizuka

    Joined:
    Jun 16, 2021
    Posts:
    5
    I'm not really asking to write for me. I really don't know how to solve above issues. Because when I try to google it, I don't see needed information. I'm sorry if you have such opinion.
     
  10. unity_OcE8Q6TU7X8LGA

    unity_OcE8Q6TU7X8LGA

    Joined:
    Jul 21, 2020
    Posts:
    3
  11. unity_OcE8Q6TU7X8LGA

    unity_OcE8Q6TU7X8LGA

    Joined:
    Jul 21, 2020
    Posts:
    3