Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Flickayy · Oct 07, 2014 at 06:17 PM · c#editorlistseditor window

[Editor Extensions] Editing Lists and Saving the Data

Hi all,

I've been developing an Editor Extension to edit the contents of a list, in my case I'm aiming to have it so designers can easily create trophies for the game.

here is my code for the window, for some reason the data comes up as null. Can someone people explain to me how to achieve this? Think I'm just having a break freeze.

EDIT:

Okay I'm getting somewhere, so far I've produced a text box and assigned a value. However it's data isn't currently being saved when you close the window and re-open, but rather it becomes something else.

My current code is as follows.

Trophy.cs

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Trophy {
 
     public int id = 0;
     public string name = "";
     public string description = "";
     public TrophyType type = TrophyType.Bronze;
 
     public Trophy(int _id, string _name, string _description, TrophyType _type) {
         id = _id;
         name = _name;
         description = _description;
         type = _type;
     }
 }
 
 [System.Serializable]
 public enum TrophyType {
     Bronze,
     Silver,
     Gold,
     Platinum
 }


TrophyEditor.cs

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 public class TrophyEditor : EditorWindow {
 
     public List<Trophy> trophies = new List<Trophy>();
     int index = 1;
 
     [MenuItem("Tools/Trophy Editor")]
     static void ShowWindow() {
         EditorWindow.GetWindow<TrophyEditor>();
     }
 
     void Awake() {
         trophies.Add(new Trophy(0, "Test", "Description", TrophyType.Bronze));
     }
 
     void OnGUI() {
         EditorGUILayout.HelpBox("Simple dynamic trophy editor.", MessageType.Info);
 
         EditorGUILayout.BeginHorizontal();
         foreach (Trophy t in trophies) {
             t.id = EditorGUILayout.IntField(t.id);
             t.name = EditorGUILayout.TextField(t.name);
             t.description = EditorGUILayout.TextField(t.description);
             t.type = (TrophyType)EditorGUILayout.EnumPopup(t.type);
         }
         EditorGUILayout.EndHorizontal();
 
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Add")) {
         }
         EditorGUILayout.EndVertical(); 
         
         Debug.Log(trophies[index - 1].name);
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MakeCodeNow · Oct 08, 2014 at 04:25 AM

You should create a ScriptableObject subclass that contains your list of Trophies and then create an instance of that asset. Then make a custom inspector for that class type. It'll work very similarly to your existing code, but the ScriptableObject is something that Unity knows how to save. You'll just want to either use EditorGUI.PropertyField along with SerializedObject's updateProperties calls or use EditorUtility.SetDirty() if you modify the SerializedObject directly so that Unity knows it's changed and needs saving.

Comment
Add comment · Show 7 · 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 Flickayy · Oct 08, 2014 at 09:16 AM 0
Share

I'm unsure of how to use ScriptableObjects inside of a list. I'll have a look at this approach anyway. Thanks for the input :)

avatar image MakeCodeNow · Oct 08, 2014 at 02:11 PM 0
Share

No, you want a ScriptableObject that contains the list.

Please mark answer as accepted when you are ready.

avatar image Flickayy · Oct 08, 2014 at 03:00 PM 0
Share

understood, when I get it working and others haven't put any other input in. Then I shall mark it :)

There has to be another way of doing it.

avatar image MakeCodeNow · Oct 09, 2014 at 01:09 AM 0
Share

The main issue is that your list of Trophies has to be saved somewhere. It could be to X$$anonymous$$L or JSON or an attribute in a component, but you can't just have a list floating in memory and expect it to be saved. All of the members of EditorWindow are ephemeral by design, because they are just tools for editing, not the source of the data to be edited.

avatar image Flickayy · Dec 02, 2014 at 05:32 PM 0
Share

Ah now I understand, thanks for that. Going to see what I come up, starting from scratch and using ScriptableObject ins$$anonymous$$d.

Show more comments

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

29 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

NPC Spawning after unlocking 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bind to OnWillSaveAssets and force to save my scene 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