Search Unity

[WRONG FORUM! Somebody please move!] Best way to store editor-only required data for GameObjects?

Discussion in 'Prefabs' started by StunAustralia, Jul 20, 2018.

  1. StunAustralia

    StunAustralia

    Joined:
    Jul 1, 2018
    Posts:
    33
    Sorry - I posted this to the wrong forum and it doesn't look like I have the ability to delete it... could some moderator please move it to the general c# forums.. thanks

    Hi folks

    This is a general rule-of-thumb best practice question...

    If I needed editor-time only control over a large number of GameObjects and to achieve this, each GameObject has a good amount of data that was only used at editor time for this behaviour

    How would I best go about making sure none of this editor-only data/behaviour was included in the final build (it's irrelevant when the game is being played)

    So far I've thought of:
    • A component to add onto the GameObject with a custom editor. Data would be defined on that component wrapped in a "#if UNITY-EDITOR" block.
      Upside: Data nicely associated with the object it belongs to, data not included in the build
      Downside: The (now empty) component itself would be included in the build.

    • A CustomWindow would recognise the objects (not sure how, each object can only have one tag as far as I know and adding a component leaves me no better off than the above) and would store the associated data in some storage it controls
      Upside: Absolutely nothing is included in the build
      Downside: I'd have to keep track of and sync objects being tagged/marked and deleted
    Are there any good rules-of-thumb for this? Natty techniques?
     
    Last edited: Jul 20, 2018
  2. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    For option one, do these game objects have any common MonoBehavior? Then you can write a custom editor for that one. Or maybe make several MonoBehaviors implement one interface and then do a custom editor for that (not sure if it will work though).
    For option two, if you need to grab specific objects from the scenes, you can have two options:
    - Again, use common interface for your MonoBehaviors, then use something like Object.FindObjectsOfType to get all objects that you need.
    - Write custom Tag component that allows for multiple tags and ditch the built-in GameObject.tag completely. (though it will still use FindObjectsOfType under the hood to work)

    Not sure how you would store the data so it's completely omitted from the build though. Your GameObjects are part of the scene which is stored on disk, so to make any editor-only data go away you can maybe implement some build preprocessor for your scenes that will parse all scene files and "optimize" them for build. Don't know if this is good solution, maybe something already exist for this, just a thought.
     
  3. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
  4. StunAustralia

    StunAustralia

    Joined:
    Jul 1, 2018
    Posts:
    33
    Not for this specific example, no they would have nothing in common other than needing to be centrally manipulated and tracked.

    Nice thought! But I can't think of a way of doing that that doesn't involve me creating a component to put on the GameObject.. which is what I'm trying to avoid :/

    I'm thinking you're both onto something there... hook into a build process and delete the data-only components just before they're compiled into the build.

    Thanks for your thoughts :)
     
  5. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    You can create a child game object and use the tag "Editor only".
     
  6. StunAustralia

    StunAustralia

    Joined:
    Jul 1, 2018
    Posts:
    33
    Oh wow.... that is a brilliantly elegant solution! So that'd let me search all the GameObjects I'd be interested in by tag. Then I'd run into an issue of finding a survivable GUID to use as a key. Definitely worth looking into!
     
  7. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    What key? Editor only gameobjects get stripped automatically, you don't need to search for them to remove them.
     
  8. StunAustralia

    StunAustralia

    Joined:
    Jul 1, 2018
    Posts:
    33
    lol - good point! My head was still in tag-and-find mode :)