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 Hacky · Mar 22, 2013 at 05:05 PM · custom editorplaymodecustom inspector

changing value of child objects in root object not persistent?

I have a root object as gamefield with many child objects (cells). The root objects script has a custom editor script. With these script I instantiate the cells and can change a value of each cell (isWalkable). When I do this with my custom editor and go into playmode I lost all my changes. When I do this manually in each cell and go into playmode, I lost nothing. Do you have any suggestions?

Here are my scripts:

 public class Cell : MonoBehaviour
 {
     public bool isWalkable;
 }

 public class GameField : MonoBehaviour
 {
     public GameObject cellPrefab;
     public Vector2 gamefieldSize = new Vector2(10.0f, 10.0f);
     public Cell[,] cells;
 
     public void BuildGrid()
     {
         if (cellPrefab == null) return;
 
         cells = new Cell[(int)gamefieldSize.x, (int)gamefieldSize.y];
 
         for (int y = 0; y < gamefieldSize.y; y++)
         {
             for (int x = 0; x < gamefieldSize.x; x++)
             {
                 GameObject cell = PrefabUtility.InstantiatePrefab(cellPrefab) as GameObject;
                 cell.name = "Cell-" + y + "-" + x;
                 cell.transform.parent = transform;
                 cell.transform.position = transform.position + x * transform.right + y * transform.forward;
                 cell.transform.localRotation = Quaternion.identity;  
                 cells[x, y] = cell.GetComponent<Cell>();
                 if (cells[x, y] == null)
                     cells[x, y] = cell.AddComponent<Cell>();
                 cells[x, y].isWalkable = true;
             }
         }
     }
 }

 [CustomEditor(typeof(GameField))]
 public class GameFieldEditor : Editor {
 
     private bool m_isInEditMode = false;
     private GameField m_gamefield;
     private SerializedProperty m_cellPrefab;
     private SerializedProperty m_gamefieldSize;
     private Vector2 m_tmpGamefieldSize;
     private bool m_gamefieldSizeFolded;
 
     private void OnEnable()
     {
         m_gamefield = target as GameField;
         m_cellPrefab = serializedObject.FindProperty("cellPrefab");
         m_gamefieldSize = serializedObject.FindProperty("gamefieldSize");
         m_tmpGamefieldSize = m_gamefieldSize.vector2Value;
 
         SceneView.onSceneGUIDelegate += GameFieldUpdate;
     }
 
     private void OnDisable()
     {
         SceneView.onSceneGUIDelegate -= GameFieldUpdate;
     }
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
 
         EditorGUILayout.PropertyField(m_cellPrefab, new GUIContent("Cell Prefab"));
         m_gamefieldSizeFolded = EditorGUILayout.Foldout(m_gamefieldSizeFolded, "Resolution of Gamefield");
         if (m_gamefieldSizeFolded)
         {
             EditorGUILayout.BeginHorizontal();
             EditorGUILayout.Space();
             EditorGUILayout.BeginVertical();
             m_tmpGamefieldSize.x = Mathf.Clamp(EditorGUILayout.IntField("Columns", (int)m_tmpGamefieldSize.x), 1, int.MaxValue);
             m_tmpGamefieldSize.y = Mathf.Clamp(EditorGUILayout.IntField("Rows", (int)m_tmpGamefieldSize.y), 1, int.MaxValue);
             if (GUILayout.Button("Set Resolution"))
             {
                 m_gamefieldSize.vector2Value = m_tmpGamefieldSize;
                 m_gamefield.BuildGrid();
             }
             EditorGUILayout.EndVertical();
             EditorGUILayout.EndHorizontal();
         }
 
         EditorGUILayout.Separator();
 
         if (!m_isInEditMode)
         {
             m_isInEditMode = GUILayout.Button("Edit GameField");
         }
         else
         {
             m_isInEditMode = !(GUILayout.Button("Leave EditMode"));
         }
 
         if (GUI.changed)
         {
             EditorUtility.SetDirty(target);
         }
 
         serializedObject.ApplyModifiedProperties();
     }
 
     private void GameFieldUpdate(SceneView sv)
     {
         Event current = Event.current;
         int controlID = GUIUtility.GetControlID(FocusType.Passive);
 
         if (m_isInEditMode)
         {
             foreach (Cell cell in m_gamefield.cells)
             {
                 Handles.color = cell.isWalkable ? Color.green : Color.red;
                 Handles.SphereCap(controlID, cell.transform.position, cell.transform.rotation, 1.0f);
             }
 
             if (current.type == EventType.mouseUp && current.button == 0 && !current.alt)
             {
                 GameObject go = HandleUtility.PickGameObject(current.mousePosition, false);
                 Cell cell = null;
 
                 if (go != null)
                 {
                     cell = go.GetComponent<Cell>();
                 }
                 
                 if (cell == null)
                 {
                     foreach (Cell c in m_gamefield.cells)
                     {
                         if (HandleUtility.DistanceToCircle(c.transform.position, 1.0f) < 1.0f)
                         {
                             cell = c;
                             break;
                         }
                     }
                 }
 
                 if (cell != null)
                     cell.isWalkable = !cell.isWalkable;
                 current.Use();
             }
         }
         
         if (current.type == EventType.layout)
             HandleUtility.AddDefaultControl(controlID);
     }
 }
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

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

EditorGUILayout.PropertyField cannot draw custom class? 2 Answers

Inspector Overlapping Text Label at a Position 1 Answer

Custom editor window does not show prefab overrides 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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