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 Change gameObjects values in editor

Discussion in 'Editor & General Support' started by Lala_Ghost, Jul 28, 2023.

  1. Lala_Ghost

    Lala_Ghost

    Joined:
    Jun 18, 2017
    Posts:
    7
    I was not sure how to name the thread.
    I am looking for a way to for example change the scale of a trampoline pad I have with a slider in the editor.
    See this video at 4:07 to understand better:


    The guy takes his magnet pad and increases it's size with a separate script you can see a little on the right of the screen inside the editor. How do you do that? That would greatly increase the speed of my testing iterations.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,707
    Looks like this guy has a bunch of custom editor stuff going on.

    Perhaps he has another video you can look at to see how it works?
     
  3. Lala_Ghost

    Lala_Ghost

    Joined:
    Jun 18, 2017
    Posts:
    7
    No he does not, he does not really show any code at all
    Getting a few bullet points or even examples would be good enough and I can probably find a lot on the web but I dont even know how to call that :p
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,707
    I would guess that it part of this GMTK he makes... or else it's built from that?

    Otherwise maybe it is a vanilla editor script?

    Or even some other addon he got from the asset store?

    If you're not able to tell from watching videos on that channel, I certainly am not either.

    There's ten billion examples and tutorials of Unity editor scripts out there.

    It would be incredible if you found one that did precisely what you need.

    Therefore like all tutorials, the process is to work through it until you understand all the parts.

    Then go engineer a solution suiting your exact needs.
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    I am not well-versed in 2D development, but at first glance it looks like the 2D Tilemap.
    https://docs.unity3d.com/Manual/Tilemap.html

    But I could be wrong.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    I believe it's custom editor tooling stuff, as in previous videos he notes the importance of making your own tools that suit the project you're in. It's pretty standard to have some kind of custom editor tooling for game-play elements to expedite/smoothen the process of designing them.

    Said tooling can definitely use Unity's existing tool kit in some form, too.
     
    Ryiah likes this.
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,123
    He's very clearly using custom editors if you look at the Inspectors whenever he shows the editor. At 5:50 you can see he has some form of custom property drawer for events that is noticeably different from the default property drawer also shown at that timestamp.

    Here's a screenshot showing: red for custom buttons, blue for custom event property drawers, and green for the default property drawers.

    upload_2023-7-28_22-32-41.png

    Oh, and if that weren't enough evidence, in the following video starting at 9:00 he talks about how he ran into a situation that would be easier to overcome if it could be automated and he ended up getting someone to create custom editor code for him.


    Here is the asset that is linked in the video description.

    https://github.com/Cratesmith/Cratesmith.AssetUI

    He goes on to talk about how it would be very advantageous to create these tools at the start of development.
     
    Last edited: Jul 29, 2023
  8. Lucid-Locust-Lollipop

    Lucid-Locust-Lollipop

    Joined:
    Jul 29, 2023
    Posts:
    1
    You can actually do this without a custom editor. If you put [SerializeField] on the line before the variable is entered, it will show you the value under the script's component in the inspector.
    For demonstration:
    Code (CSharp):
    1. [SerializeField]
    2. public float FlutterTime 0.2f;
    this will then make FlutterTime show up as a customizable number field in the inspector.
    [SerializeField] also works for other integer types as well, pretty much anything that you have to put "public" or "private" should show up in the inspector if you serialize it. I think that "public" does something similar, but I have no idea.

    Also, to make it an actual slider, you can just click and hold on the name to the left of the number field, and it will change the value when you move your mouse horizontally. Visually though, it's still a number.

    Hope this helps
    ~ Love, Lucy
     
    Last edited: Jul 30, 2023
  9. CiroContns

    CiroContns

    Unity Legend

    Joined:
    Jan 28, 2008
    Posts:
    74
    There can be many ways and as other suggested, you can use custom editor scripts etc.
    However, to do something like this, you could also simply rely on the built-in Unity event function
    OnValidate
    . Whenever you change the value of any property, the
    OnValidate
    is called and you can do something with that value!

    (documentation: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html)

    I mocked up a simple script:

    Code (CSharp):
    1. public class PanelFunctions : MonoBehaviour
    2. {
    3.     public Transform widthObject;
    4.     public Transform heightObject;
    5.  
    6.     [Header("Options")]
    7.     [Range(1f, 10f)] public float panelWidth = 3f;
    8.     [Range(1f, 10f)] public float panelHeight = 2f;
    9.  
    10.     private void OnValidate()
    11.     {
    12.         widthObject.localScale = new Vector3(panelWidth, 1f, 1f);
    13.         heightObject.localScale = new Vector3(1f, panelHeight, 1f);
    14.      
    15.         // positionedObject.position = new Vector3(x, y, 0f);
    16.         // anything else you can imagine :)
    17.         // ...
    18.     }
    19. }
    You can see how, in
    OnValidate
    , we take the value of
    panelWidth
    and
    panelHeight
    which has probably changed (like Mark is doing in the video), and use it to scale some child gameObjects that you link in the public properties at the top.

    This obviously requires you to structure your Prefab in a sensible way, that makes sense to scale, and that looks good when you scale it. Or you can set other properties to those values, like position, collider sizes, etc.

    The
    Range
    attribute can help keep the values of these properties in check, creating a slider in the UI.

    Hope it helps!
     
    Lala_Ghost likes this.