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
0
Question by security18 · Apr 12, 2015 at 09:36 PM · c#editorarraygrid

How to Save an multidimensional array trough editor script

How can i save my position plus value in a array trough a editor script. Currently when i click play it resets the values.

At the moment when i click a button in the scene, it will change the value in the array to one, example: buildgrid[1,5] = 1

When it is 1 it will turn to red and you cant build. But when i play it will reset all values back to 0

Below you the grid class and the editor script.

 public class Grid : MonoBehaviour
 {
 
     public int[,] buildGrid = new int[15,15];
 
 ..
 ..
 ..

Here is the editor script:

 public void OnSceneGUI()
     {
 
         for (int x = 0; x < myGrid.buildGrid.GetLength(0); x++)
         {
             for (int z = 0; z < myGrid.buildGrid.GetLength(1); z++)
             {
                 int a = x * 10;
                 int b = z * 10;
                 //Handles.RectangleCap(1, new Vector3(a, 10, b), Quaternion.Euler(90, 0, 0), 5f);
 
                 if (myGrid.buildGrid[x, z] == 0)
                 {
                     Handles.color = Color.green;
                 }
                 else if (myGrid.buildGrid[x, z] == 1)
                 {
                     Handles.color = Color.red;
                 }
 
                 if (Handles.Button(new Vector3(a, 10, b), Quaternion.Euler(90, 0, 0), 4.9f, 4.9f, Handles.RectangleCap))
                 {
                     if (myGrid.buildGrid[x, z] == 0)
                     {
                         myGrid.buildGrid[x, z] = 1;
                         EditorUtility.SetDirty(target);
                     }
                     else if (myGrid.buildGrid[x, z] == 1)
                     {
                         myGrid.buildGrid[x, z] = 0;
                         EditorUtility.SetDirty(target);
                     }
 
                     Debug.Log("Pressed button, value changed to: " + myGrid.buildGrid[x, z] + "|   Coords Array  X: " + x + "  z: " + z + "|   Coords  A: " + a + "  B: " + b);
 
 
 
                 }
             }
         }

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 RudyTheDev · Apr 13, 2015 at 01:04 PM

Short answer: no, you cannot natively save multi-dimensional arrays or lists. By default, Unity saves public fields that it knows how to serialize. In this case, it doesn't know how. Next time it (de)serializes your class, it resets any edits you have made to unserialized fields (even if you would properly dirty them).

That said, this is how you can do it and I would do it in a properly encapsulated fashion:

 using System;
 using UnityEngine;
 
 [Serializable]
 public class MultiIntArray
 {
     [SerializeField]
     private int width;
 
     [SerializeField]
     private int height;
 
     [SerializeField]
     private int[] values;
 
     public int Width { get { return width; } }
     
     public int Height { get { return height; } }
 
     public int this[int x, int y]
     {
         get { return values[y * width + x]; }
         set { values[y * width + x] = value; }
     }
 
     public MultiIntArray(int width, int height)
     {
         this.width = width;
         this.height = height;
 
         values = new int[width * height];
     }
 }
 

Then your code is like:

 using UnityEngine;
 
 public class MultiIntArrayTest : MonoBehaviour
 {
     public MultiIntArray testArray = new MultiIntArray(10, 5);
 
     void Start()
     {
         testArray[6, 2] = 100;
         testArray[0, 0] = 200;
         testArray[0, 4] = 300;
         testArray[9, 0] = 400;
         testArray[9, 4] = 500;
     }
 }
 

It won't look pretty in the editor inspector without a good PropertyDrawer and I'm entirely too lazy to do that now.

Comment
Add comment · Show 2 · 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 security18 · Apr 14, 2015 at 11:03 AM 0
Share

But than i still have to do everything myself:

          void Start()
          {
              testArray[6, 2] = 100;
              testArray[0, 0] = 200;
              testArray[0, 4] = 300;
              testArray[9, 0] = 400;
              testArray[9, 4] = 500;
          }

I need to make it clickable in the editor window

avatar image RudyTheDev · Apr 14, 2015 at 05:27 PM 0
Share

I thought you were doing OnSceneGUI and working on myGrid.buildGrid there. So you would just use buildGrid as:

 public $$anonymous$$ultiIntArray buildGrid = new $$anonymous$$ultiIntArray(15, 15);

(and .GetLength(0/1) would be .Width and .Height)

That code would work with this version the same way. That Start is just an example.

This is just an example of how you would handle such cases in Unity. The exact classes and usage are up to you.

If you need to modify the values in inspector, then you need to write an Editor script or PropertyDrawer to handle it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with array/lists manipulation in Editor script 1 Answer

How to display a list of methods and allow a choice of one of them 1 Answer

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

An Array of Classes with variables in C# 2 Answers

Need a hand using array.Length in a C# editor script 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