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 sadowlight123 · May 27, 2020 at 10:18 AM · custom editor2d array

Custom Editor Dynamic 2D array

I have a 2D grid that needs to be filled with numbers. I searched and tried multiple solutions online but they either deleted or did not work.

In the first image, when I select Grid Size to be small then I get 4 by 4 matrix or 16 intFields in total.

alt text

In the second image, when I select Grid Size to be huge then I get 7 by 7 matrix

alt text

The scripts are level and levelEditor. The level script is a scriptable object whereas levelEditor is a custom editor.

The problem here is that the intFields are generated by the levelEditor and they aren't linked to level since I am not knowing how to do it. The second problem is whenever I write anything into the fields , it is stuck to 0.

Here is the code for level script:

 using UnityEngine;
 
 
 [CreateAssetMenu(fileName = "New Level", menuName = "Levels/level", order = 1)]
 public class Level: ScriptableObject
 {
 
     public int number;
     public Constants.Difficulty difficulty;
     public Constants.GridSize gridSize;
    // tried different things here like list of list, 2d arrays and a class (row) of cols then an array of rows 
 
 }

Here is the code for the levelEditor:

 using System;
 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(Level))]
 public class LevelEditor : Editor
 {
     public override void OnInspectorGUI()
     {
 
         Level l = (Level) target;
 
         int[,] fieldsArray = new int[0, 0];
 
         EditorGUILayout.BeginHorizontal();
             GUILayout.Label("Number");
             GUILayout.Label("Difficulty");
             GUILayout.Label("Grid Size");
         EditorGUILayout.EndHorizontal();
 
         EditorGUILayout.BeginHorizontal();
             l.number = EditorGUILayout.IntField(l.number);
             l.difficulty = (Constants.Difficulty) EditorGUILayout.EnumPopup(l.difficulty);
             l.gridSize = (Constants.GridSize)EditorGUILayout.EnumPopup(l.gridSize);
         EditorGUILayout.EndHorizontal();
         
 
         int gridSize = 4;
         if (l.gridSize == Constants.GridSize.Medium) gridSize = 5;
         else if (l.gridSize == Constants.GridSize.Large) gridSize = 6;
         else if (l.gridSize == Constants.GridSize.Huge) gridSize = 7;

         //below is of interest, also tried a lot of solutions 
         GUILayout.Label("Equation");
 
         if (gridSize != fieldsArray.GetLength(0) || gridSize != fieldsArray.GetLength(1))
         {
             fieldsArray = new int[gridSize, gridSize];
         }
 
         for (int j = 0; j < gridSize; j++)
         {
             EditorGUILayout.BeginHorizontal();
             for (int i = 0; i < gridSize; i++)
             {
                 fieldsArray[i, j] = EditorGUILayout.IntField(fieldsArray[i, j]);
             }
             EditorGUILayout.EndHorizontal();
         }
 
     }
 }


2020-05-27-12-57-36-mail.png (7.9 kB)
2020-05-27-12-57-51-mail.png (10.3 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
0

Answer by Map-Builder · May 29, 2020 at 01:03 AM

      int[,] fieldsArray = new int[0, 0];

it is called each frame in GUI, so created each frame and destroyed at the end of function. just put it outside ? XD

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

128 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

Related Questions

custom button 2 Answers

Custom control in editor. 1 Answer

How to limit an ObjectField to assets with specific file extension 2 Answers

Custom Editor for array of custom class 1 Answer

Efficiently serializing large collision grid 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