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 TehFisharmahn · Oct 26, 2016 at 03:45 PM · editorprefabgeneric

Save a prefab from editor code

Hello

I have a problem with saving a prefab I am editing via custom Editor Window. The object is meant to be a database, to which I am creating a simple GUI for editing. Everything is fine except I can't seem to really save anything (after I exit Unity, changes are gone). I tried saving the prefab by adding a new one and overwriting the older, but this made my dictionary lose data.

Here's the code (it only adds a few options, since I started with saving issues):

 public class SkillsEditor : EditorWindow {
     string newSkillId = "";
 
     SkillType browseTypes;
     string[] skillsList;
     string currentSkill = "";
     int currentSkillIndex;
     SkillData data = null;
 
     [MenuItem("TFG/Editors/SkillsEditor")]
     public static void ShowWindow() {
         GetWindow(typeof(SkillsEditor));
     }
 
     void OnGUI() {
         SkillsManager manager = Resources.Load("Managers/SkillsManager", typeof(SkillsManager)) as SkillsManager;
         if (manager.container == null)
             manager.container = new System.Collections.Generic.Dictionary<string, SkillData>();
         GUILayout.BeginHorizontal();
         EditorGUILayout.LabelField("New skill index:", new GUILayoutOption[] { GUILayout.Width(100) });
         newSkillId = EditorGUILayout.TextField(newSkillId, new GUILayoutOption[] { GUILayout.Width(150) });
         if (newSkillId.Length > 0)
             if (GUILayout.Button("Add skill", new GUILayoutOption[] { GUILayout.Width(75) })) {
                 if (!manager.container.ContainsKey(newSkillId))
                     manager.container[newSkillId] = new SkillData();
                 currentSkill = newSkillId;
                 newSkillId = "";
             }
         GUILayout.EndHorizontal();
         if (manager.container != null && manager.container.Count > 0) {
             EditorGUILayout.BeginHorizontal();
             browseTypes = (SkillType)EditorGUILayout.Popup((int)browseTypes, new string[] { "Passives", "Auras", "Self targeted", "Projectiles", "Instants" });
             skillsList = manager.container.Where(skill => skill.Value.type == browseTypes).Select(kvp => kvp.Key).ToArray();
             if (skillsList.Length != 0) {
                 if (currentSkill.Length > 0) {
                     for (int i = 0; i < skillsList.Length; ++i)
                         if (skillsList[i] == currentSkill) {
                             currentSkillIndex = i;
                             break;
                         }
                 }
                 currentSkillIndex = EditorGUILayout.Popup(currentSkillIndex, skillsList);
                 currentSkill = skillsList[currentSkillIndex];
             }
             EditorGUILayout.EndHorizontal();
         }
         if (currentSkill.Length > 0) {
             Debug.Log(manager.container.Count);
             data = manager[currentSkill];
             data.type = (SkillType)EditorGUILayout.Popup((int)data.type, new string[] { "Passive", "Auras", "Self targeted", "Projectile", "Instant" });
             manager.container[currentSkill] = data;
         }
         GUILayout.Space(10);
         GUILayout.BeginHorizontal();
         if (GUILayout.Button("Save", new GUILayoutOption[] { GUILayout.Width(150), GUILayout.Height(20) })) {
             SkillsManager temp = Instantiate(manager);
             Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/Resources/Managers/SkillsManager.prefab");
             PrefabUtility.ReplacePrefab(temp.gameObject, prefab, ReplacePrefabOptions.ReplaceNameBased);
             DestroyImmediate(temp.gameObject);
         }
         GUILayout.Space(20);
         if (GUILayout.Button("Delete", new GUILayoutOption[] { GUILayout.Width(150), GUILayout.Height(20) })) {
 
         }
         GUILayout.EndHorizontal();
     }
 }
 

The thing is - after hitting "Save" button my dictionary is cleared of values - despite the fact, that it ought not to (by my expectations, at least). When I added a simple string field to my SkillsManager class and changed it with this code, it saved perfectly and the change persisted through turning Unity off and on again. But here? I get an error in this part:

         if (currentSkill.Length > 0) {
             Debug.Log(manager.container.Count);
             data = manager[currentSkill];
             data.type = (SkillType)EditorGUILayout.Popup((int)data.type, new string[] { "Passive", "Auras", "Self targeted", "Projectile", "Instant" });
             manager.container[currentSkill] = data;
         }

Is gives me the null pointer exception in line "data.type = ...", because, apparently, the managet.container dictionary is empty (despite it was not before saving).

Is it because I use my own class, the dictionary or the generic types? SkillsManager is derived from a generic class that holds a simple dictionary and overloads the [] operator for my general convinience. I tried removing the dictionary, but to no avail - i had two lists, one of string, second of "T" values. The second one was cleared, the strings persisted. Then I tried removing the generic types and made SkillsManager not inherit. Still no do.

Any ideas here?

If anybody asks, the SkillData class holds mainly floats and strings, but also one Dictionary, one enum, one sprite and two members of my own type (which holds some arrays, a dictionary, enum and a member of SkillData type).

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam-Mechtley · Oct 27, 2016 at 07:36 AM

It doesn't necessarily answer your specific question, but it seems like you could probably bypass a lot of the headaches here by not having a custom EditorWindow that is loading and replacing a prefab in Resources, but instead just make a custom Editor for your SkillsManager class so you can just pick it in the project and directly make edits in its inspector.

Comment
Add comment · Share
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
avatar image
0

Answer by TehFisharmahn · Oct 27, 2016 at 11:55 AM

Well, yes, I agree. But that was an idea.

Anyway, i changed it. Now I simply serialize everything to json and the manager only loads, holds and saves the data. This way I don't need the prefab.

So, If anyone this same specific idea - I didn't manage to do it. Just found a better solution.

Comment
Add comment · Share
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

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

95 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

Related Questions

How can I change the root game object of a prefab in the editor? 1 Answer

Why does GameObject.FindWithTag() get a wrong instance? 2 Answers

Entering Play mode is suddenly extremely slow 2 Answers

I'm trying to make a prefab placement editor tool for point and click prefab placements drawn from a random pool. 0 Answers

ParticleSystem Prefab Preview in EditorWindow 0 Answers


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