Editor Plugin for prefab creation
I'm currently working on a plugin for a creator that will create a specific type of prefab based on the gameObject that's selected. I've only got the start of it working, except it won't save the actual prefab. I've been reading through the wiki and scripting API as well as other answers and I cannot figure out how to make this work
here's the code. basically I want the player to select the card type, then have the editor create a prefab with the proper scripts attached. I haven't even gotten to attaching the scripts based on the card type yet because even though the save file dailouge pops up it won't actually save said prefab. the other cases are just tests to make sure it's working creating anything.
before I put the pathBase in it was working by simply creating a prefab in the localpath Assets/ folder, I want it to create it in the prefab folder, I also figure a save dialogue box will be better because then if you want to change the prefab name you'll have the option before saving it.
Any help would be greatly appreciated. This is two different school projects rolled into one and I've been beating my head against the keyboard for a few days trying to figure it out.
using UnityEngine;
using UnityEditor;
using System.Collections;
//my items
public enum CARDTYPE
{
Monster = 0,
Defense = 1,
Spell = 2
}
public class CardCreator : EditorWindow {
static string cardName = "What is the Cards Name?";
bool groupEnabled;
bool myBool = true;
float myFloat = 1.23f;
public CARDTYPE selectedCardType;
//Add Menu item name "Card Creator: to window menu in unity
[MenuItem ("Window/Card Creator")]
//this is the code to store the window
public static void ShowWindow()
{
//Show existing window, if it doesn't exist create it
EditorWindow.GetWindow(typeof(CardCreator));
}
//this holds the code for the window
void OnGUI()
{
GUILayout.Label("Base Settings", EditorStyles.boldLabel);
cardName = EditorGUILayout.TextField("Card Name", cardName);
//dropDown selector for card type
selectedCardType = (CARDTYPE)EditorGUILayout.EnumPopup("Primitive to create:", selectedCardType);
if (GUILayout.Button("Create"))
InstantiateCardType(selectedCardType);
//unity code, keep for now
groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
myBool = EditorGUILayout.Toggle("Toggle", myBool);
myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
}
void InstantiateCardType(CARDTYPE selectedCardType)
{
switch (selectedCardType)
{
case CARDTYPE.Monster:
CreateCardPrefab();
break;
case CARDTYPE.Defense:
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = Vector3.zero;
break;
case CARDTYPE.Spell:
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.transform.position = Vector3.zero;
break;
default:
Debug.LogError("Unrecognized Option");
break;
}
}
static void CreateCardPrefab()
{
GameObject[] selectedObjects = Selection.gameObjects;
string pathBase = EditorUtility.SaveFilePanel("Choose save folder", "Assets/Prefabs", cardName, ".prefab");
if (!string.IsNullOrEmpty(pathBase))
{
foreach (GameObject go in selectedObjects)
{
string localPath = pathBase;
if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))
{
if (EditorUtility.DisplayDialog("Are you sure?",
"The prefab already exists. Do you want to overwrite it?",
"Yes",
"No"))
{
CreateNew(go, localPath);
}
}
else
{
Debug.Log(cardName + " is not a prefab, will convert");
CreateNew(go, localPath);
}
}
}
}
static bool ValidateCreatePrefab()
{
return Selection.activeGameObject != null;
}
static void CreateNew(GameObject obj, string localPath)
{
Object prefab = PrefabUtility.CreateEmptyPrefab(localPath);
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
AssetDatabase.Refresh();
}
}
Your answer
Follow this Question
Related Questions
How can I display a scriptable object in a custom editor ? 1 Answer
Custom Editor Window - Multiple Selection Dropdown 0 Answers
Editor Int sliders affecting each other 0 Answers
How to highlight non MonoBehaviour scripts from editor window 0 Answers
How to return the default value for EditorStyles.toolbarButton.normal.background ??? 1 Answer