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 agirault · Oct 29, 2018 at 05:31 PM · custom-inspectorserializedpropertycustom inspectortextassetcsv

Serialize properties of the custom editor itself

Goal

I have a CSV file which holds data for multiple fields, which names are in the header (1st line). My goal is to pass the CSV file as a TextAsset to my Editor, let it parse the header, and create a popup menu below it in the inspector to select which field (aka column id) to choose to read. When the application plays, the columnId is used by the MonoBehavior script to read the data in that column at Start, then print it at Update:

Example

 public class DataReader: MonoBehaviour {
     public string[] lineArray;        // one string for each line of the csv file
     public int columnId = 0;          // defines which column aka field to read
     private List<float> columnValues; // values for the selected field
     private int lineId = 0;           // defines which line aka value to read
     void Start() {
         // Fill up columnValues given the array of lines and the id of the column to read
         columnValues = new List<float>(lineArray.Length - 1);
         for (lineId = 1; lineId < lineArray.Length; ++lineId {
             string[] values = linesArray.Split(',');
             string value = values[columnId];
             columnValues.Add(float.Parse(value));
         }
     }
     void Update() {
         // Just loop through the values for the selected field and print them
         if (lineId >= values.Length) {
             lineId = 0;
         }
         print(columnValues[lineId++])
     }
 }

 [CustomEditor(typeof(DataReader))]
 public class DataReaderEditor : Editor {
     private DataReader reader;
     private TextAsset CSVInput;
     private void OnEnable() {
         reader = target as DataReader;
     }
     public void OnInspectedGUI() {
         CSVInput = EditorGUILayout.ObjectField("CSV Input",
                                                CSVInput,
                                                typeof(TextAsset),
                                                true) as TextAsset;

         reader.lines = CSVInput.text.Split("\n");
         string firstLine = reader.lines[0];
         string[] headers = firstLine.Split(',');

         reader.columnId = EditorGUILayout.Popup("Field",
                                                 reader.columeId,
                                                 headers);
 }



Results

The parsing of the CSV file (simplified here since it's not the topic of the issue), the creation of the popup UI to show the name of the fields, passing the line data and selected field index to the reader for it to read all work great: alt text



Problem

My issue with this example is that DataReaderEditor.CSVInput does not persist when I use the Unity Editor: as soon as I select another object or play the application, the TextAsset field for CSVInput is empty in the inspector, even though DataReader.lineArray is properly serialized and set and DataReader then reads the proper values. alt text



I know I should probably use SerializedObjects instead in general, but even with that, my goal is not to serialize more properties for DataReader which works well, but for DataReaderEditor to maintain the TextAsset input and not reset it to nil all the time. I guess the issue comes from the fact that I store a property in the Editor that is not in the MonoBehavior, but that instead the Editor pre-processes the that TextAsset property into a string array that is enough for the DataReader, and I'd like to keep it that way if possible (not keep track of the TextAsset in DataReader since we don't need it).

Thank you!

screen-shot-2018-10-29-at-12311-pm.png (35.1 kB)
screen-shot-2018-10-29-at-12334-pm.png (23.1 kB)
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

158 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 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

EditorGUILayout.BeginVertical("Box") not filling the inspector 0 Answers

Unity Custom Editor: 1 True Bool Only Within A Group 1 Answer

Custom inspector - How to serialize scene objects in ScriptableObject? 0 Answers

No type in ObjectField created in uxml (UIElements) 3 Answers

Why does Unity fail to read CSV exported from Excel 2010? 2 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