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 /
avatar image
0
Question by ELGUN · Jan 11, 2016 at 07:55 AM · editorscriptableobjectcustom-inspector

Custom Inspector for ScriptableObject

Hello guys! I have some problem. Please help me.

This my ScriptableObject

     [System.Serializable]
     public class dm_MapTiles : ScriptableObject
     {
 
         public dm_MapTiles() { }
 
         public string mapName;
         public Sprite[,] mapSprites;
 
     }

And this is my Custom Editor Script

 using UnityEditor;
 using UnityEngine;
 using System.Collections.Generic;
 
 [CustomEditor(typeof(dm_MapTiles))]
 public class dm_MapTilesEditor : Editor {
 
     dm_MapTiles comp;
     static bool showTileEditor = false;
 
     public void OnEnable()
     {
         comp = (dm_MapTiles)target;
         if (comp.mapSprites == null)
         {
             comp.mapSprites = new Sprite[1,1];
         }
     }
 
     public override void OnInspectorGUI()
     {
 
         //MAP DEFAULT INFORMATION
         comp.mapName = EditorGUILayout.TextField("Name", comp.mapName);
 
         //WIDTH - HEIGHT
         int width = EditorGUILayout.IntField("Map Sprite Width", comp.mapSprites.GetLength(0));
         int height = EditorGUILayout.IntField("Map Sprite Height", comp.mapSprites.GetLength(1));
 
         if (width != comp.mapSprites.GetLength(0) || height != comp.mapSprites.GetLength(1))
         {
             comp.mapSprites = new Sprite[width, height];
         }
 
         showTileEditor = EditorGUILayout.Foldout(showTileEditor, "Tile Editor");
 
         if (showTileEditor)
         {
             for (int h = 0; h < height; h++)
             {
                 EditorGUILayout.BeginHorizontal();
                 for (int w = 0; w < width; w++)
                 {
                     comp.mapSprites[w, h] = (Sprite)EditorGUILayout.ObjectField(comp.mapSprites[w, h], typeof(Sprite), false, GUILayout.Width(65f), GUILayout.Height(65f));
                 }
                 EditorGUILayout.EndHorizontal();
             }
         }
     }
 
 }
 

And this my screenshot alt text

If I Play game, my scriptableobject lost all data. Please help me.

screenshoot.jpg (22.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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Munchy2007 · Jan 11, 2016 at 10:35 AM

Add this line to your OnInspectorGUI() function

 EditorUtility.SetDirty(comp);

Full function with added line, this ensures the changes you made are saved when you enter play mode or exit Unity.

 public override void OnInspectorGUI()
     {
 
         //MAP DEFAULT INFORMATION
         comp.mapName = EditorGUILayout.TextField("Name", comp.mapName);
 
         //WIDTH - HEIGHT
         int width = EditorGUILayout.IntField("Map Sprite Width", comp.mapSprites.GetLength(0));
         int height = EditorGUILayout.IntField("Map Sprite Height", comp.mapSprites.GetLength(1));
 
         if (width != comp.mapSprites.GetLength(0) || height != comp.mapSprites.GetLength(1))
         {
             comp.mapSprites = new Sprite[width, height];
         }
 
         showTileEditor = EditorGUILayout.Foldout(showTileEditor, "Tile Editor");
 
         if (showTileEditor)
         {
             for (int h = 0; h < height; h++)
             {
                 EditorGUILayout.BeginHorizontal();
                 for (int w = 0; w < width; w++)
                 {
                     comp.mapSprites[w, h] = (Sprite)EditorGUILayout.ObjectField(comp.mapSprites[w, h], typeof(Sprite), false, GUILayout.Width(65f), GUILayout.Height(65f));
                 }
                 EditorGUILayout.EndHorizontal();
             }
 
         }
         EditorUtility.SetDirty(comp);
 
     }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How should I serialize data that is also editable in the Inspector? 2 Answers

Display Custom Inspectors for each class in a List<> 1 Answer

How to use "SerializeObject" with an object which doesn't derive from Object? 1 Answer

Drawing a custom variable in the inspector y position bugs selection 0 Answers

ScriptableObject with Custom Editor resetting data in inspector 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