Reflexed Mo
Reflexed Mo

Reputation: 19

How to create scriptable object from scene in unity editor

I am currently creating a 2D-game in Unity and facing troubles in level design. I would like to create about 100 levels, each with different prefabs at different positions.

In order to load up the proper levels I have built an architecture with scriptable objects. Tilemaps are being used to represent obstacles. So it is possible to have about 30 different tile-positions for each level. It seems wrong to me to fill in those informations on every scriptable object seperatly.

What I am now looking for is a way to create a level in the editor and save the data directly in a scriptable object. To have a button in editor which says: "Save current scene-layout in e.g. scriptable object level 3". And also being able to load every level to the scene in editor mode.

Upvotes: 0

Views: 1143

Answers (2)

Joca
Joca

Reputation: 45

I would suggest Raphael solutions, but in some cases this can be hard if you are deal with prefabs issues.

Other way to achive this would be create your custom editor to iterate your scene and create this ScriptableObject.

I use this code for something similar

        GameObject __PARENT = GameObject.Find("__PARENT");
        Vector3 centroid = Vector3.zero;
        Transform[] childs = __PARENT.transform.GetComponentsInChildren<Transform>();
        foreach (Transform go in childs)
        {
            Debug.Log(go.name);
            centroid += go.position;
        }
        centroid /= (__PARENT.transform.childCount);
        GameObject centerPivotObject = new GameObject();
        centerPivotObject.name = "CenterPivotObject";            
        centerPivotObject.transform.position = centroid;

        foreach (Transform go in childs)
        {
            go.parent = centerPivotObject.transform;
        }

In my case I was trying to center pivot in parent object, but you can combine this iterate over your parent game using this and create using this your ScriptableObject

https://wiki.unity3d.com/index.php/CreateScriptableObjectAsset

Upvotes: 0

Rapha&#235;l Roux
Rapha&#235;l Roux

Reputation: 366

You create your level in a prefab and reference this prefab into your ScriptableObject. So your level prefab contains your tilemap and other prefabs.

Upvotes: 0

Related Questions