Search Unity

My painful approach to creating predefined levels through code, using prefabs.

Discussion in 'General Discussion' started by nobluff67, Aug 29, 2019.

  1. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    I am creating a level based game where the levels are predefined. The levels use prefabs, and are large enough that if I create the level and keep it in the scene, the build size bloats significantly, even though the levels use the same prefabs. I have, because of this, created level setup through code, which follows the process of :

    1. Creating the level in the scene.
    2. Code the instantiate base on the transforms of each prefab in point 1.
    3. Deleting the scene level in point 1.

    I am finding the task in point 2, extremely tedious and error prone, as it requires the correct setting of position, rotation and scale.

    I would like to know from you, if know of a way to refine/speed up point 2?

    Or if you can think of another solution that creates the level, and doesn't increase the size of the build significantly?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    There is various way to do it.
    But for example, placing on the scene only placeholders, and use LOD / scripts, to load appropriate model.
    Reduce model complexity / size.
    You should be able to place by hand thousands of objects.
    Split large scene into smaller sub scenes.
    Load scene in sections.
    Etc.
     
  3. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    Thanks, using scripts currently, its just the tedious and error prone task of making sure that the transforms for each gameobject is correct, when instantiated through code. I'm not sure if that the coding approach you are talking about?

    The game is level based, similar to a golf course, so each hole would be a level. It is only basically one scene, many levels.
     
  4. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    I actually had a brainwave. Create empty gameobject with children that are the gameobjects of constructed level. Attach scripts to parent. In start of script iterate on parent and construct a code-line, and report the code-line through debug.log.
     
  5. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    This works exceptionally well for me:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class CreateCode : MonoBehaviour
    7. {
    8.     public TextMeshProUGUI codeText;
    9.  
    10.     void Start()
    11.     {
    12.         codeText.text = "";
    13.         foreach (Transform child in transform)
    14.         {
    15.             codeText.text += child.gameObject.name + ".transform.localScale = new Vector3(" +
    16.                 child.gameObject.transform.localScale.x + "f," +
    17.                 child.gameObject.transform.localScale.y + "f," +
    18.                 child.gameObject.transform.localScale.z + "f);\n" +
    19.                 "Instantiate(" +
    20.                 child.gameObject.name + ", new Vector3(" +
    21.                 child.gameObject.transform.position.x + "f," +
    22.                 child.gameObject.transform.position.y + "f," +
    23.                 child.gameObject.transform.position.z + "f), Quaternion.Euler(" +
    24.                 child.gameObject.transform.localEulerAngles.x + "f," +
    25.                 child.gameObject.transform.localEulerAngles.y + "f," +
    26.                 child.gameObject.transform.localEulerAngles.z + "f), transform);\n";
    27.  
    28.         }
    29.     }
    30. }
    31.  
    The only minor issue that you might encounter is a duplicate gameobject, which will be prefixed with a (1), etc. A quick, manual, search and replace sorts that out.