A
A
Alexey2016-07-26 18:56:39
Game development
Alexey, 2016-07-26 18:56:39

How to create an object in the scene from the Inspector?

(Unity 5.3, C#)
I need:
I create a script -> hang it on an object -> change the values ​​of the variables of this script through the inspector (for example, the number of objects created) and immediately the objects are created in the scene, before the game itself starts .
I don't know if this is even possible in Unity.
So far I'm using Awake, specifying values ​​through the inspector. But this is too inconvenient, because. constantly need to run the game to check.
I searched on the Internet - I did not find it. Maybe I just didn't search well.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2016-07-27
@Alexeee

There are several options. If you want to use the same method in the editor and runtime, then you can make your own inspector with a button. For example, you want to call the Method method in the editor:

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        Method();
    }

    public void Method()
    {
        Debug.Log("Method");
    }
}

Create a class, inherit from UnityEditor.Editor and set the CustomEditor attribute . In the Editor folder put the script with the inspector:
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof (Test))]
public class TestEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if (GUILayout.Button("Method"))
        {
            ((Test) target).Method();
        }
    }
}

As a result, you will get the following button:
If the method is needed only in the editor, then you can make a menu using MenuItem . The script is also placed in the Editor folder.
using UnityEditor;
using UnityEngine;

public static class TestUtility
{
    [MenuItem("Tools/Method")]
    public static void Method()
    {
        Debug.Log("Method");
    }
}

Get this menu:
If you're feeling adventurous, you can also try EditorWindow , but I'd start with the inspector.
For specific generation, the HideInInspector and SerializeField attributes may come in handy . With their help, you can store links to the created objects in the generator script and not lose them when reloading the scene.
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    [HideInInspector, SerializeField]
    private List<GameObject> gameObjects = new List<GameObject>();

    private void Awake()
    {
        Generate();
    }

    public void Generate()
    {
        for (int i = 0; i < 100; i++)
        {
            var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
            gameObjects.Add(go);
        }
    }

    public void Clear()
    {
        foreach (var go in gameObjects)
        {
            if (Application.isPlaying)
            {
                Destroy(go);
            }
            else
            {
                DestroyImmediate(go);
            }
        }
        gameObjects.Clear();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question