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 Limbo · Feb 18, 2016 at 07:17 AM · c#serializationcustom editor

Custom editor, not serializing

I have this code:

 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 using System.Collections.Generic;
 
 
 public class PrefabEditor : MonoBehaviour
 {
     #if EDITOR_FROM_SCRIPT
     [HideInInspector] public bool showOriginalFields = false;
     [SerializeField, HideInInspector] private int _selectedCat = 0;
     [SerializeField] private bool _showCat = true;
     [SerializeField] private string _output = "Prefab save path";
     [SerializeField] private string _prefabName = "New prefab name";
     [SerializeField] private List<Object> _category = new List<Object>();
 
     public int selectedCat
     {
         get{return _selectedCat;}
         set{_selectedCat = value;}
     }
 
     public string[] CategoryName
     {
         get
         {
             // init array
             string[] result = new string[_category.Count];
             // set the array
             for(int i = 0; i < _category.Count; i++)
             {
                 if(null == _category[i]) continue;
 
                 result[i] = _category[i].name;
             }
             // return it
             return result;
         }
     }
 
     public bool CategoryEmpty
     {
         get{return null == _category || (null != _category && 0 == _category.Count);}
     }
 
 #if UNITY_EDITOR
     public void Inspector_Draw()
     {
         // Draw toggle
         if(IEditor.Button(showOriginalFields ? "Hide [orignal fields]" : "Show [orginal fields]"))
             showOriginalFields = !showOriginalFields;
         // Draw header
         Inspector_Header();
     }
 
     private void Add()
     {
         // Add an empty object
         _category.Add(null);
     }
 
     private void Clear()
     {
         // Prompt the user
         bool confirm = EditorUtility.DisplayDialog("Prefab Editor", "Are you sure you want to delete all categories", "Yes", "No");
         // exit if confirmation was negative
         if(!confirm) return;
         // clear category list
         _category.Clear();
     }
 
     public void Inspector_Header()
     {
         EditorGUILayout.BeginVertical("Box");
         {
             // The output dictionary
             IEditor.Label("Prefab", EditorStyles.boldLabel);
 
             // [indent]++
             EditorGUI.indentLevel++;
             {
                 _output = EditorGUILayout.TextField("Output", _output);
                 _prefabName = EditorGUILayout.TextField("Name", _prefabName);
             }
             EditorGUI.indentLevel--;
             
             // [Space]
             EditorGUILayout.Space();
             
             // Label categories
             IEditor.Label("Categories", EditorStyles.boldLabel);
 
             EditorGUILayout.BeginHorizontal();
             {
                 if(IEditor.Button("Add", EditorStyles.miniButtonLeft))
                     Add();
 
                 if(_category.Count == 0)
                 {
                     if(IEditor.Button(_showCat ? "Hide" : "Show", EditorStyles.miniButtonRight))
                         _showCat = !_showCat;
                 }
                 else
                 {
                     if(IEditor.Button(_showCat ? "Hide" : "Show", EditorStyles.miniButtonMid))
                         _showCat = !_showCat;
 
                     if(IEditor.Button("Delete", EditorStyles.miniButtonRight)) Clear();
                 }
             }
             EditorGUILayout.EndHorizontal();
 
             // Debug.LogFormat("CatEmpty {0}", CategoryEmpty);
 
             // exit if item has nothing
             if(!CategoryEmpty)
             {
                 // Draw the categories
                 Inspector_DrawCategory();
 
                 if(_selectedCat > 0 && _selectedCat < _category.Count - 1)
                 {
                     // Draw the selection grid
                     Inspector_SelectionGrid();
                 }
             }
         }
         EditorGUILayout.EndVertical();
     }
 
     private void Inspector_DrawCategory()
     {
 
         if(_showCat)
         {
             // [Space]
             EditorGUILayout.Space();
             // [HelpBox] Tip
             IEditor.Box("Drag and drop folder as new category", IEditor.ExpandWidth);
 
             EditorGUILayout.BeginVertical("Box");
             {
                 EditorGUILayout.Space();
 
                 // Draw items
                 for(int i = 0; i < _category.Count; i++)
                 {
                     EditorGUILayout.BeginHorizontal();
                     {
                         _category[i] = EditorGUILayout.ObjectField(i + " Category", _category[i], typeof(Object), true) as Object;
                         
                         if(IEditor.Button("Delete", EditorStyles.miniButtonRight, IEditor.DontExpandWidth))
                         {
                             _category.RemoveAt(i);
                             continue;
                         }
                     }
                     EditorGUILayout.EndHorizontal();
                 }
             }
             EditorGUILayout.EndVertical();
         }
     }
 
     private void Inspector_SelectionGrid()
     {
         if(null != _category[_selectedCat]) return;
         // [Space]
         EditorGUILayout.Space();
 
         EditorGUILayout.BeginHorizontal("Box");
         {
             GUILayout.Label("Selected");
             selectedCat = GUILayout.SelectionGrid(selectedCat, CategoryName, 5, EditorStyles.miniButton);
         }
         EditorGUILayout.EndHorizontal();
     }
     #endif
 }

And this one:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(PrefabEditor))]
 public class IPreabEditor : Editor
 {
     private PrefabEditor I
     {
         get{return target as PrefabEditor;}
     }

     public override void OnInspectorGUI ()
     {
         if(I.showOriginalFields) base.OnInspectorGUI ();
 
         I.Inspector_Draw();
     }
 }

Unity won't detect any changes if I hit the add button on the inspector, thoughts?

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

106 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

Related Questions

[Solved] Custom editor resets after Play? 3 Answers

ScriptableObject created from custom editor lose data on Unity restart 1 Answer

SetDirty not working in Custom Editor with Nested Custom Property drawer 1 Answer

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Set a List Property with EditorScripting 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