Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

What is the function called at the beginning of both NOT play mode and play mode?

Discussion in 'Scripting' started by mandaxyz, Apr 26, 2018.

  1. mandaxyz

    mandaxyz

    Joined:
    Apr 26, 2018
    Posts:
    6
    Hi, I'm new to this forum and to Unity.
    Version: Unity 2017.2.1f1
    My questions are in the title and in the code below.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour
    5. {
    6.     private Vector3[] _vertices;
    7.  
    8.     void Awake()
    9.     {
    10.         Generate();
    11.           // I want to call Generate() at the beginning of both edit mode and play mode.
    12.           // But this function is not called at the beginning of edit mode, what should I do?
    13.     }
    14.  
    15.     void Generate()
    16.     {
    17.         _vertices = new Vector3[2];
    18.         _vertices[0] = new Vector3(-1f, 0f, 0f);
    19.         _vertices[1] = new Vector3(1f, 0f, 0f);
    20.     }
    21.  
    22.     void OnDrawGizmos()
    23.     {
    24.         Debug.Log(_vertices.Length); // In edit mode, this logs the number 0,
    25.                                     // it means that the function Generate() is not called.
    26.  
    27.         if (_vertices == null)
    28.             return;
    29.  
    30.         Gizmos.color = Color.yellow;
    31.         for (int i = 0; i < _vertices.Length; i++)
    32.         {
    33.             Gizmos.DrawSphere(_vertices[i], 0.1f);
    34.         }
    35.     }
    36. }
     
    Last edited: Apr 26, 2018
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    817
  3. mandaxyz

    mandaxyz

    Joined:
    Apr 26, 2018
    Posts:
    6
    Thank you. I tested the code in the link you gave to me then it logs as follows:
    EnterPlayMode
    ExitingPlayMode
    EnteredEditMode

    I'm sorry guys to bother you, I posted a reply but I removed the explanation because I'm really stuck.
     
    Last edited: Apr 26, 2018
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    playModeStateChanges is not appropriate here for two reasons;
    - it's editor only, meaning that if these vertices are necessary for the game instead of just gizmos, it's not going to work.
    - the implementation would be a super-cumbersome "search through all objects, find the relevant objects, call the method on all of them" implementation.

    The easiest thing for sure would be to use the [ExecuteInEditMode] attribute. That might not be appropriate - if you have Update methods that moves the object around, it will now be moved around in edit mode, which you don't want. Probably.

    A more robust thing would be to make the vertices be lazy:

    Code (csharp):
    1.  
    2. public class NewBehaviourScript : MonoBehaviour
    3. {
    4.     private Vector3[] _vertices;
    5.     private Vector3[] vertices { get {
    6.         if(_vertices == null)
    7.             _vertices = Generate();
    8.         return _vertices;
    9.     }
    10.  
    11.     Vector3[] Generate()
    12.     {
    13.         var verts = new Vector3[2];
    14.         verts[0] = new Vector3(-1f, 0f, 0f);
    15.         verts[1] = new Vector3(1f, 0f, 0f);
    16.     }
    17.  
    18.     void OnDrawGizmos()
    19.     {
    20.         Gizmos.color = Color.yellow;
    21.         for (int i = 0; i < vertices.Length; i++)
    22.         {
    23.             Gizmos.DrawSphere(vertices[i], 0.1f);
    24.         }
    25.     }
    26. }
     
    mandaxyz likes this.
  5. mandaxyz

    mandaxyz

    Joined:
    Apr 26, 2018
    Posts:
    6
    Thank you so much,

    The code I showed you at the top is based on a tutorial, and the function Awake() is used in the tutorial, but I don't know what did the Author do to show the Gizmos in edit mode, maybe the new Unity has a bug? The calling of the function Generate() is inside Awake() as below:
    Code (CSharp):
    1. private void Awake()
    2. {
    3.     Generate();
    4. }
    Question: So how to use the function Awake() and make it to be called at the beginning of the edit mode?

    If you are curious about the tutorial then here is the link http://catlikecoding.com/unity/tutorials/procedural-grid/
    The tutorial's image below is in edit mode because of the XYZ.


    You can download the .unitypackage at the bottom of the tutorial page in the link above.
    .unitypackage Download Link: http://catlikecoding.com/unity/tuto...enerating-additional-vertex-data.unitypackage

    The steps to follow:
    - Import the package
    - Create an Empty GameObject at position (0, 0, 0)
    - Add the script "Grid" to the Empty
    - Change the X Size in Inspector to 10
    - Change the Y Size in Inspector to 5
    - Add the material "Material" to the Empty's Mesh Renderer
    - Add the following function to the script "Grid":
    Code (CSharp):
    1. private void OnDrawGizmos()
    2. {
    3.     if (vertices == null)
    4.     {
    5.         return;
    6.     }
    7.  
    8.     Gizmos.color = Color.black;
    9.     for (int i = 0; i < vertices.Length; i++)
    10.     {
    11.         Gizmos.DrawSphere(vertices[i], 0.1f);
    12.     }
    13. }
    Then the Gizmos Black Dots don't show in edit mode, because vertices is null and Generated() is not called, exactly as explained at the top.
     
    Last edited: Apr 26, 2018
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
     
    mandaxyz likes this.
  7. mandaxyz

    mandaxyz

    Joined:
    Apr 26, 2018
    Posts:
    6
    Baste, thank you very much.