Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Sprite sorting right in the editor

Discussion in 'Scripting' started by Nicokam, Mar 26, 2021.

  1. Nicokam

    Nicokam

    Joined:
    May 28, 2020
    Posts:
    64
    Hello,

    So, I'm using a sprite that is a tree top in order to decorate my environment.

    I would like to know if there is a way to code something in the inspector so that when click and drag the sprite, it will randomize the size and color (previously defined values), so that I dont have to copy them then change those settings manually.

    (I dont want to do that at run time! It is only a "builder tool")
     
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You can do that with a custom editor for your script. I'm not sure you can call a method upon drag/drop (probably can), but you can easily create a button to randomize the values via script. I haven't worked with custom editors much so will defer to others for advice ;)
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    You can really do all kinds of whacky things at editor time... pretty much almost everything you can do at runtime.

    For instance you could put a single tree sprite somewhere then have a button to "spread a bunch of these randomly around here" or "randomly in this rectangular area," then tune them by hand and press save on the scene.

    You can do this with OnValidate() functions I believe, or just do it by selecting stuff in scene and running your code. It can be as simple as decorating a static function with [MenuItem("Do it")] and then iterating the Selected property and doing your thing.
     
    seejayjames likes this.
  4. Nicokam

    Nicokam

    Joined:
    May 28, 2020
    Posts:
    64
    Okay, will take a look at that. Thanks!