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
1
Question by DustyScreen · Jul 29, 2014 at 02:32 PM · arrayeditor-scriptingobjectfield

Custum editor: How do I setup a 3x3 grid of ObjectFields?

I'm new to editor scripting and trying to setup a 3x3 grid of my own Path-objects on the inspector of my Level-object, like this:

Screenshot from Unity

The problem is, that I can't seem to get the inspector data stored in the double-array. I've tried this workaround, but the data gets reset, when I hit the play button:

Level.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Level : MonoBehaviour {
 
     private Path[,] pathMatrix = new Path[3,3];
     
     public void SetPath(Path path, int x, int y)
     {
         pathMatrix[x,y] = path;
     }
     
     public Path GetPath(int x, int y)
     {
         return pathMatrix[x,y];
     }
 
 }

LevelEditor.cs:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(Level))]
 public class LevelEditor : Editor
 {
     private Level level;
     
     private void OnEnable()
     {
         level = (Level)target;
     }
     
     public override void OnInspectorGUI()
     {    
         EditorGUIUtility.labelWidth = 20f;
         
         GUILayout.BeginHorizontal();
         
         level.SetPath((Path) EditorGUILayout.ObjectField("TL", level.GetPath(0, 0), typeof(Path), true), 0, 0);
         level.SetPath((Path) EditorGUILayout.ObjectField("T ", level.GetPath(1, 0), typeof(Path), true), 1, 0);
         level.SetPath((Path) EditorGUILayout.ObjectField("TR", level.GetPath(2, 0), typeof(Path), true), 2, 0);
         
         GUILayout.EndHorizontal();
         
         GUILayout.BeginHorizontal();
         
         level.SetPath((Path) EditorGUILayout.ObjectField("L", level.GetPath(0, 1), typeof(Path), true), 0, 1);
         level.SetPath((Path) EditorGUILayout.ObjectField(" ", level.GetPath(1, 1), typeof(Path), true), 1, 1);
         level.SetPath((Path) EditorGUILayout.ObjectField("R", level.GetPath(2, 1), typeof(Path), true), 2, 1);
         
         GUILayout.EndHorizontal();
         
         GUILayout.BeginHorizontal();
         
         level.SetPath((Path) EditorGUILayout.ObjectField("BL", level.GetPath(0, 2), typeof(Path), true), 0, 2);
         level.SetPath((Path) EditorGUILayout.ObjectField("B ", level.GetPath(1, 2), typeof(Path), true), 1, 2);
         level.SetPath((Path) EditorGUILayout.ObjectField("BR", level.GetPath(2, 2), typeof(Path), true), 2, 2);
         
         GUILayout.EndHorizontal();
     }
 }



Why does the data get wiped when I enter Play-mode?

This is very important to me! Any help or hints in the right direction will be highly appreciated :-)

skærmbillede 2014-07-29 kl. 16.11.10.png (15.6 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

2 Replies

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

Answer by superpig · Jul 29, 2014 at 02:35 PM

2D arrays aren't supported by the Unity serializer.

You'll need to use a 1D array. You can do it by changing your Level class like this:

 using UnityEngine;
 using System.Collections;
  
 public class Level : MonoBehaviour {

     public const int Width = 3;
     private Path[] pathMatrix = new Path[9];
  
     public void SetPath(Path path, int x, int y)
     {
         pathMatrix[y * Width + x] = path;
     }
  
     public Path GetPath(int x, int y)
     {
         return pathMatrix[y * Width + x];
     }
  
 }
Comment
Add comment · Show 1 · 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 DustyScreen · Jul 29, 2014 at 02:43 PM 0
Share

You are one lovely ninja! Thanx so much! :-D

One final detail to get it working:

     [SerializeField] private Path[] path$$anonymous$$atrix = new Path[9];
 
avatar image
0

Answer by _dns_ · Jul 29, 2014 at 02:42 PM

Hi, Unity can't serialize multi dimension arrays, that's why the array is not saved/serialized. You can use a single dimension array (Path[3*3]) and reference data in it using mypath = Path[ y*3 + x ]. You could also define a new class containing the single dimension array and redefine index operator [] so you could access this array using [,] notation.

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

24 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

Related Questions

Using EditorGUI.ObjectField in PropertyDrawer 0 Answers

3D Class Array cleared on script save 1 Answer

How to prevent overlap when using nested arrays in CustomPropertyDrawers? 1 Answer

How to initialize array[][] via SerializedProperty? 1 Answer

Vector2 Editor Visualization 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