- Home /
Creating prefab through custom editor window
Hello everyone.
I have a game which involves a lot of different units.
Here is what I want to do.
A window that will let me do the following options:
Set the unit's name, health etc.
When I hit "Create" button, it will create a prefab in a path in the assets with all the needed scripts and set the script's vars according to my input.
So far, I have a custom window with all the inputs I need.
To create the prefab, I'm creating a gameobject, then using PrefabUtility.CreatePrefab, then destroying the gameobject in the scene.
My problem is, I want to automatically add scripts to the gameobject, and I want them to be added directly from the assets.
Any ideas how to do this (I'm using C#)?
Thanks
You're almost there. :-)
When creating a gameobject, use gameobject.AddComponent();
Then create the prefab from this gameobject as you described.
This is quiet an old question, I've already learned a lot sense then.
I will post an actual answer describing how to do this sometime this week (maybe even today).
well this really will boost my knowledge about program$$anonymous$$g and I even won't need to create my self bunch of stuff for any new pool like I have to do it now
you'll get my +1 if you get it :)
sadly I won't be able to accept your answer, ...
BU$$anonymous$$P after long time, ... so how do you make that?
Answer by dorpeleg · Jan 30, 2014 at 06:59 AM
Really sorry for not replaying with the answer, was super busy and forgot.
So here it is:
(assuming you already have the input part setup)
// setup a new gameobject
tempGO = new GameObject(object name from the input);
// add a script to it
// (it might be possible to make this part more generic so it will accept scripts chosen from input)
tempComponent = tempGO.AddComponent<script name here>();
// give the script the values from input (note: values on the script needs to be public)
tempComponent.someValue = someInputValue;
tempComponent.someValue2 = someInputValue2;
// create an empty prefab in project
emptyPrefab = PrefabUtility.CreateEmptyPrefab(full path to save including prefab name);
// replace the gameobject with the empty prefab (save)
PrefabUtility.ReplacePrefab(tempGO, emptyPrefab);
And that should be it, if you got questions, please ask.
Is there a way to stop the object from being created in the scene?