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 KVinS · Oct 26, 2015 at 02:18 AM · c#editorinspectorarraysserialization

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

Hi! I have a Component that holds some variables. Variables will be saved when restarting Unity. (int, color, float)

 using UnityEngine;
 using System.Collections;
 
 public class Grid : MonoBehaviour
 {
     public float startX = 0;
     public float startZ = 0;
 
     public int width = 1000;
     public int height = 1000;
 
     public float boxWidth = 32.0f;
     public float boxHeight = 32.0f;
 
     public float px;
     public float pz;
 
     public Color color = Color.white;
     public bool[,] map;
 
     void OnDrawGizmos()
     {
         if (map != null)
         {
             Vector3 pos = transform.position;
 
             px = pos.x + startX;
             pz = pos.z + startZ;
 
             Gizmos.color = color;
 
             Vector3 size = new Vector3(boxWidth, 0f, boxHeight);
 
             for (int x = 0; x < width; x++)
             {
                 for (int z = 0; z < height; z++)
                 {
                     if (map[x, z])
                     {
                         Gizmos.DrawCube(new Vector3(px + x * boxWidth, pos.y + 1, pz + z * boxHeight), size);
                     }
                     else
                     {
                         Gizmos.DrawWireCube(new Vector3(px + x * boxWidth, pos.y + 1, pz + z * boxHeight), size);
                     }
                 }
             }
         }
     }
 }

To edit the variables I have my inspector:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor (typeof(Grid))]
 public class GridEditor : Editor
 {
     Grid grid;
     
     public void OnEnable()
     {
         grid = (Grid)target;
         SceneView.onSceneGUIDelegate += GridUpdate;
 
         if (grid.map == null)
         {
             grid.map = new bool[grid.width, grid.height];
             for (int x = 0; x < grid.width; x++)
             {
                 for (int y = 0; y < grid.height; y++)
                 {
                     grid.map[x, y] = false;
                 }
             }
         }
     }
     
     public void OnDisable()
     {
         SceneView.onSceneGUIDelegate -= GridUpdate;
     }
 
     private int lastX = -1;
     private int lastZ = -1;
     void OnSceneGUI()
     {
         Event e = Event.current;
         Camera camera = Camera.current;
 
         Vector3 mousePos =  Vector3.zero; // = camera.ScreenToWorldPoint(new Vector3(e.mousePosition.x, e.mousePosition.y, distance));
 
         if (EventType.KeyDown == e.type && KeyCode.LeftControl == e.keyCode)
         {
   
             Ray ray = camera.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + camera.pixelHeight, 0));
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 2550f))
             {
                 mousePos = hit.point;
             }
 
             int x = Mathf.RoundToInt((mousePos.x - grid.px)/grid.boxWidth);
             int z = Mathf.RoundToInt((mousePos.z - grid.pz)/grid.boxHeight);
             
             if (x >= 0 && x < grid.width && z >= 0 && z < grid.height)
             {
                 if (x != lastX || z != lastZ)
                 {
                     grid.map[x, z] = !grid.map[x, z];
                     lastX = x;
                     lastZ = z;
                     SceneView.RepaintAll();
                 }
             }
         }
     }
 
     public override void OnInspectorGUI()
     {
         GUILayout.BeginHorizontal();
         GUILayout.Label(" Start x ");
         grid.startX = EditorGUILayout.FloatField(grid.startX, GUILayout.Width(50));
         GUILayout.EndHorizontal();
 
         GUILayout.BeginHorizontal();
         GUILayout.Label(" Start z ");
         grid.startZ = EditorGUILayout.FloatField(grid.startZ, GUILayout.Width(50));
         GUILayout.EndHorizontal();
 
 
         GUILayout.BeginHorizontal();
         GUILayout.Label(" Grid Width ");
         grid.width = EditorGUILayout.IntField(grid.width, GUILayout.Width(50));
         GUILayout.EndHorizontal();
 
         GUILayout.BeginHorizontal();
         GUILayout.Label(" Grid Height ");
         grid.height = EditorGUILayout.IntField(grid.height, GUILayout.Width(50));
         GUILayout.EndHorizontal();
 
         GUILayout.BeginHorizontal();
         GUILayout.Label(" Box Width ");
         grid.boxWidth = EditorGUILayout.FloatField(grid.boxWidth, GUILayout.Width(50));
         GUILayout.EndHorizontal();
 
         GUILayout.BeginHorizontal();
         GUILayout.Label(" Box Height ");
         grid.boxHeight = EditorGUILayout.FloatField(grid.boxHeight, GUILayout.Width(50));
         GUILayout.EndHorizontal();
 
         if (GUILayout.Button("Open Grid Window", GUILayout.Width(255)))
         {   
            GridWindow window = (GridWindow) EditorWindow.GetWindow(typeof (GridWindow));
            window.Init();
         }
 
         SceneView.RepaintAll();
     }
 }

Unfortunately, I can not figure out how to store a two-dimensional array (map [,]) of boolean, so he remained when restarting Unity. All that I came it to make serialization to file and look for the file at startup App. (array can be a big 1000x20) Maybe you can advise a better idea? Thank you! Excuse my English.

Comment
Add comment · Show 1
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 bubzy · Oct 26, 2015 at 09:39 AM 0
Share

re: the array

you can store it as an int and use 1 and 0, it will still work as boolean.

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

34 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

Related Questions

How Can I Stop my Array Being Cleared on Runtime via Serialisation? 0 Answers

Editor nullreference error when playing second time 0 Answers

editor script problem, looping 1 Answer

Array of custom classes not saving to Playmode 0 Answers

UnityEvent Generic (Inspector Serialization) 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