What i want is some kind of reset button or generate new map button in the Inspector.
But i don’t want it to be using [ExecuteInEditMode] since i want to use it only when running the game.
And not also using CustomEditor since i don’t want to see the button in the inspector before running the game.
Maybe i should use [ExecuteInEditMode] ?
What i’m trying to do is to generate a map and i want also to be able to click on a button each time to generate a new map.
I can’t make a ui button in the game view window while the game is running since i’m using the mouse left click button for something else already that’s why i need a button in the Inspector.
So i asked before but didn’t get yet answer how to do it if at all. I asked and someone told me:
I quote: "
That’s true. However the whole approach seem to be just wrong. First of all the button in the inspector just triggers a runtime methods of the component. This can be implemented simply as context menu item. So it doesn’t require any editor script in the first place.
Next is that almost 90% when i see the use of “ExecuteInEditMode” it’s actually used in a wrong / problematic way. That attribute is ment to provide “runtime behaviour” at edit time. Though this should only be used to provide the user with a visual representation. So it makes sense to use it for simulating particle systems or a water wave simulation to show how they will look at runtime. Code that actually edits / modifies the scene shouldn’t be executed by such a script.
When you use ExecuteInEditMode, Start will be called each time the scene is loaded during edit time. Since you don’t clean up your old objects you will create additional objects each time Start is called during edit mode. Start is also called after an assembly reload which happens each time a script got changed outside of Unity and Unity recompiles it’s project assemblies…
You shouldn’t use ExecuteInEditMode at all in this case. Just call your Generate method once from the Reset callback. Reset is called only once when the object is created (or when you select “Reset” from the context menu). In addition you can simply add a ContextMenu to allow the user to manually trigger the creation. Also the code probably should remove the old objects (if there are any) before creating new objects."
But not sure how to do it and if using a button is wrong ?
This is the code i’m using for creating the map:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelMap : MonoBehaviour
{
public GameObject Node;
public Vector3 nodeSize;
public int Rows;
public int Columns;
public int mapWidth;
public int mapHeight;
public float Spacing = 2.0f;
public float spawnSpeed = 0;
private List<GameObject> objects = new List<GameObject>();
private void Start()
{
Generate();
}
private void Generate()
{
for (int x = 0; x < mapWidth; x++)
{
float positionWidth = Spacing * (float)x;
for (int z = 0; z < mapHeight; z++)
{
float positionWidth1 = Spacing * (float)z;
GameObject block = Instantiate(Node, Vector3.zero, Node.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.localPosition = new Vector3(positionWidth, 0, positionWidth1);
objects.Add(block);
}
}
}
public void GenerateNew()
{
for (int i = 0; i < objects.Count; i++)
{
DestroyImmediate(objects[i]);
}
Generate();
}
}
And this is a class for the button in the Inspector:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(LevelMap))]
public class GenerateButton : Editor
{
public override void OnInspectorGUI()
{
LevelMap Generate = (LevelMap)target;
if (GUILayout.Button("Generate Map"))
{
Generate.GenerateNew();
}
DrawDefaultInspector();
}
}
But i don’t want the button to show in the Inspector before running the game and i don’t want to use [ExecuteInEditMode] and i’m not sure if to use a button or a context menu like i was advised ?