Search Unity

Render preview of a sprite on screen before placing it

Discussion in '2D' started by OliverTheLove, Dec 20, 2016.

  1. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Hey!

    I've got a 2D platform game where I need to place objects on the scene to move forwards. Image the same was a placing structures in starcraft, you get a preview image of the structure before placing it. Slight transparency of the object.

    I'm a bit stuck now on how I should do it. I gave it a try with an image on a canvas that I keep switching and enabling, but I never got the size of the image to be the same on the canvas as the actual object placed, and the size of the image on the canvas kept changing was developing further.

    I also tried just creating all the objects needed and hiding them elsewhere, and then moving them to the mouse position, but that felt like a bad solution.

    Is there a nice way how I can draw a preview of the prefab or sprite before placing it? :)
     
  2. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    You could use the actual object but change the SpriteRenderer's color to 50% alpha if the position isn't valid and change it back to 100% when you actually place the object. Some pseudo code
    Code (CSharp):
    1. //Assuming a bool validPos to determine if the position is valid
    2. void Update() {
    3. spriteRenderer.color = validPos ? validColor : invalidColor
    4. }
    Just be sure you disable any colliders or behaviors you don't want to happen prior to placing the object and re-enable them when you've placed it.
     
    OliverTheLove likes this.
  3. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    I see!

    So you suggest I should instantiate the object but change the colour of the object and remove its collider? Sounds nice, I'll give it a try! :)
     
  4. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Yes, but you can keep doing what you're doing with creating and hiding the objects instead of instantiating them (it's what I do as a matter of fact). Don't remove the collider though, just disable it so you can get it back how you set it up in the inspector.
     
  5. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Hmm... how do you mean by creating objects instead of instatiating them?
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I think he meant creating and then showing/hiding, as opposed to instantiating & destroying.
     
    OliverTheLove likes this.
  7. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Yes, sorry for not being more clear.
     
    OliverTheLove likes this.
  8. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Hey again!

    I solved it by creating the platforms in the game at start with my character and then hiding them. When I want to place them I enable the SpriteRenderer for object I have chosen to place and lastly I check the trigger of the object to see if something is in the way to determin color and if it was placable.

    Thanks!
     
    LiterallyJeff likes this.