Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by liftedplane · Dec 09, 2017 at 03:16 AM · editor-scriptingeditorwindoweditorguilayout

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();
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges