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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can i use a button in the inspector to generate new map ? And how to change vars in real time ?

Discussion in 'Scripting' started by Chocolade, Aug 11, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    In this script i create a kind of grid map. But i have some problems.

    First it's not filling the grid. It's doing only creating the outer width and height and only on two sides. But i want it to be filled like a grid map.

    Another problem is that if i put as width and height 4 it should create 4X4 = 16 width height grid. But it's creating over then 100 objects.

    Also how can i make that if i will change the variables values in the editor it will effect the objects in real time ?

    Last problem is how can i use the custom button ot generate each time a new grid ? Like reset.



    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [ExecuteInEditMode]
    7. public class LevelMap : MonoBehaviour
    8. {
    9.     public GameObject Node;
    10.     public Vector3 nodeSize;
    11.     public int mapWidth;
    12.     public int mapHeight;
    13.     public float Spacing = 2.0f;
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         for (int i = 0; i < mapWidth; i++)
    19.         {
    20.             Instantiate(Node);
    21.             float positionWidth = Spacing * (float)i;
    22.             Node.transform.localPosition = new Vector3(positionWidth, 0, 0);
    23.  
    24.             for (int x = 0; x < mapHeight; x++)
    25.             {
    26.                 Instantiate(Node);
    27.                 float positionHeight = Spacing * (float)x;
    28.                 Node.transform.localPosition = new Vector3(0, 0, positionHeight);
    29.             }
    30.         }
    31.     }
    32.  
    33.     public void GenerateNew()
    34.     {
    35.  
    36.     }
    37. }
    38.  
    And the button script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(LevelMap))]
    7. public class customButton : Editor
    8. {
    9.     public override void OnInspectorGUI()
    10.     {
    11.         DrawDefaultInspector();
    12.  
    13.         LevelMap lm = (LevelMap)target;
    14.         lm.GenerateNew();
    15.     }
    16. }
    17.  
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    It does that. Just change any public (or [SerializeField]) in the Inspector and it changes it during play, you dont have to do anything special.
    There are many ways:
    1. Create a UI
    2. Use the legacy UI (OnGUI)
    3. Use the ContextMenu attribute - Video <- I would probably use this one
     
  3. welpie21

    welpie21

    Joined:
    Dec 23, 2014
    Posts:
    49
    i have an issue what i ran to.. why would you instantiate in an update function.
    and above what TaleOf4Gamers said you could do that.

    but there is also a way to code when you click the button. make sure that you attach the script onto the button it self.
    Code (CSharp):
    1. gameObject.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => GenerateNew());
    but i also improved your spawning so it does not spawn over 100+ objects.
    you can call Start as an void but also as an IEnumerator is more effecient for your cpu to handle.

    Code (CSharp):
    1. // Update is called once per frame
    2.     IEnumerator Start()
    3.     {
    4.         for (int i = 0; i < mapWidth; i++)
    5.         {
    6.             for(int x = 0; x < mapHeight; x++)
    7.             {
    8.                 GameObject go = Instantiate(Node, this.transform, true);
    9.  
    10.                 float offsetWidth = Spacing * i;
    11.                 float offsetHeight = Spacing * x;
    12.  
    13.                 go.transform.localPosition = new Vector3(offsetWidth, 0, offsetHeight);
    14.             }
    15.         }
    16.  
    17.         yield return null;
    18.     }