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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Create in-game inspector?

Discussion in 'Scripting' started by djnewty, May 13, 2015.

  1. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    Hi,

    I'm trying to find out information on building an in-game inspector, so the user can change some of the values of certain objects when they click on them, like size, color, etc.

    I'm wondering if anyone has any pointers on how this could be done? I'm guessing I will need a popup of some kind, which I've found in the unity free asset examples. The main things I'm a bit confused about are

    1) How to make a popup work so that it is specific to each object? I'm guessing I need some kind of name recognition system that ties these objects together?

    2) How to close other popups if another is opened (so only one popup can be opened at the same time?

    3) How to make the public variables available to the popup, so I could change a slider inside the popup and have it change the color of the object?

    Any thoughts would be most appreciated

    Cheers
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    For changing variables around at runtime I'd just go for a basic like input field that pops up when something is "selected" that let's you adjust some sliders or enter floats or something.

    Are you wanting to do this just for testing purposes or is it a gameplay thing - like garrys mod?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can build one with reflection. Its a deep rabbit hole to go down, but if you are interested I can dig up the scripts.
     
  4. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    Hi MD_Reptile, it's a gameplay thing, yes very similar to garrys mod, but not nearly as complicated (just using 2d objects).

    I kind of like the way the draggable/resizable pop up works in the UI samples from the asset store - https://www.assetstore.unity3d.com/en/#!/content/25468

    but I'm stuck on how I could implement this pop up so the content inside could pick up the currently selected object? Is there any standard way of allowing selectable objects?

    I'm also stuck on how to make a slider inside the popup change an attribute/variable of the object it is supposed to change. Does the popup need to find the object by name? or maybe there is a better way?

    BoredMormon, reflection sounds interesting. I am kinda keen on using the pop up as mentioned above. Not sure can this be used with reflection?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You've got two seperate concepts here, your UI, and the actual code behind it. Keeping the two ideas seperate will help.

    Your pop up menu is just UI stuff. It's pretty easy to build with Unity's built in UI tools.

    Actually changing the variable will require coding. If you know in advance the names of all of the variables you want to adjust, then you could hard code it all. This approach will work, but it's not very flexible.

    Reflection allows you to examine your code at runtime, and find out details about it. You can use reflection to create a list of all of the fields or properties on a script. You can also use reflection to modify these values.
     
  6. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    I'd be really grateful to see any examples you might have of reflection. I'm not finding much from google searches. Although Im quite new to unity, so maybe the solution is more simple.

    I'm wondering also, If I have a prefab with a bunch of nested objects, each with scripts attached. e.g.

    1st Level - Canvas (container.cs - script attached to resize canvas/panel)
    2nd Level - 16 buttons (grid.cs - script attached doing some basic formatting)
    2nd Level - Popup GameObject (popup.cs - script attached to create a popup)

    Inside the popup.cs script, how can I get/set variables for the 1st level canvas (container.cs script)? Would I access it by using the parent object reference? Maybe using GetComponentInParent? If so, how may I go up 2 parents?
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
  8. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    Does reflection work in a cross compatible app (e.g. IOS , Android), or is it specific to C# only?
     
  9. Deleted User

    Deleted User

    Guest

    It works on all platforms except for Reflection.Emit. It doesn't work on iOS (from what I've heard).
     
    Kiwasi likes this.
  10. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    oh dear, any ideas how to have selectable objects with popup inspectors that are cross compatible?
     
  11. Deleted User

    Deleted User

    Guest

    Did you look at the System.Reflection.Emit namespace? I'm not sure how much of its features you'd need to use for your purposes.
     
  12. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I feel like your not gonna need all that - if you only need to edit and adjust certain variables (transform, script variables) at runtime - just get a "pop up" working, and get it references to those variables, then use sliders and fields to update and adjust them... it might not be as fancy as unity inspector, but a hell of a lot easier/faster to implement right?
     
    Kiwasi likes this.