Im trying to figure out how to add a gameObject as a child of another object in the Unity editor, for example the UIButton has a text child Object automatically added to the button, or the UISlider automatically has a background fill and a handle automatically added. I need it to be visible in the editor as well as at runtime, so I can visually place my objects.
Im not sure how to do this, should I do this via script ? if so are there any tutorials demonstrating how to add the child Objects so they are visible in the editor, or should I use some kind of customisable prefab ? or is there another more standard way of doing this such as a custom object in the create menu of the Game Hierarchy, if so how would I do that, again is it some kind of gameObject script which creates the child objects at the point of being added to the editor or is it some kind of shortcut to a prefab ? how does it work ? Im fairly new to Unity so I’m a bit clueless here, Any help much appreciated
I believe all you need to do is save the hierarchy you want to replicate as a prefab.
It will be available where you saved it in the Project window (Ctrl+5), you can simply drag and drop it to the scene window, under the hierarchy position you want to have it.
Is there another way of doing it, the prefab route seems a bit hacky somehow, do you know of any tutorials about how to add the Object / objects to the create menu in the Hierarchy ?
I completely disagree with it being a hacky way, it is the intended way of using unity scene/hierarchy, I’d recommend trying to learn more on how to use the standard layout than to go ahead and make changes to it, specially on something so basic as adding an object to the scene.
But, to answer your question, there are a few different ways of doing it, here are a few examples:
You can take several routes, some are bit hard to figure out:
A. Use prefabs from your project folder, then make copies of these and modify to your needs. You can also instantiate prefabs at runtime.
B. Create functionality you can call from menus / inspector right click over your component / or component’s fields. Especially, check how ContextMenu works. These don’t need editor scripting, you can just call static methods (or non-static) in some file, that doesn’t have to have other functionality. See this page for good info on this:
C. Use editor scripting. Create custom EditorWindow / ScriptableWizard, where you have options and some buttons to create something you need.
Either way, UI system elements for example, are GameObjects with components added, and then these GameObjects might have one or more child GameObjects which in turn have components. So you could do pretty much same as what the built in UI elements do. Just repeat the steps - create object, add components, add child objects, add components to these and so on.
Thank you for the links, Im having a great deal of trouble understanding the terms Unity uses, so its very difficult for me to search for the information I need and communicate my problem. Coming from an Objective C and Java background its been a bit difficult to understand the difference between prefabs and Objects, the way Ive rationalised it is to think of prefabs as Objects with key properties already set such as sprite images and as such dependant on assets in my assets folder.
So Ive been looking at this problem with a mind to create a base level Object with no asset type dependancies.
So for example my game might have 3 different types of button, which I would make 3 prefabs of, blue button would then have 10 instances Red Button 4 instances and say Green Button 7 instances. But all of the prefabs would use a base level Button script which automatically attaches a text child object to the button, if you see what Im getting at. If I made the base level Button a prefab would it even be possible to then make the blue red and green button prefabs, in effect prefabs of prefabs ?
So I guess what Im asking is there some kind of event which only gets called when a script is added to a gameObject, kind of like an OnStart method for the editor ? where I could ensure the child objects are are only added once and then added to my Hierarchy. I understand theres an OnValidate but that gets called whenever I change any public variables in the inspector, so that would potentially if I wasn’t careful create duplicate orphan child objects I didn’t want
Ive just noticed a problem with my current line of thinking, in that it might create the text Object but any changes made to the text object in the inspector might not be remembered at run time, Im clearly over thinking this whats the solution ? is the only solution prefabs in which case referring to my last post can I make prefabs of prefabs ? ie a master button prefab of which red blue and green buttons are sub prefabs ?