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

Question How do I load tiles at runtime?

Discussion in 'Scripting' started by woodkir000, Mar 25, 2022.

  1. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Hi all, I'm trying to make a turn-based tile strategy game, and I'm currently trying (and failing) to figure out how to efficiently create a ground system without just doing a bunch of duplicating. Is there a good, simple guide out there on how to do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    You can create them all by hand and save them in a scene, always the simplest fastest way to start.

    Or you can Instantiate<T>() things at runtime, which is "a bunch of duplicating."

    News flash: pretty much the ONLY thing a computer actually does is "a bunch of duplicating." Even with a pre-made scene, it's just using the data stored in the scene file to duplicate everything into being for you. That's it. It's all just data.
     
  3. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Thanks. I guess I could've been more specific but by duplicating I meant essentially just clicking and pressing Ctrl + D, which would make a huge mess in the hierarchy with something like "Cube, Cube (1), Cube (2), Cube (3), etc
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Then write a cheesy quick editor script to do it for you!

    You can find a quick starter example in my MakeGeo project, specifically in the
    Bitmap2Grid
    scene and script.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. using UnityEditor.SceneManagement;
    8. #endif
    9.  
    10. // to use:
    11. //    create blank GameObject in scnee
    12. //    put this Bitmap2Grid script on it
    13. //    create texture
    14. //        - no compression
    15. //        - non power of two : NONE
    16. //        - mark read/write enabled
    17. //        - slot texture into Bitmap2Grid script
    18. //    create B2GConfig scriptable object
    19. //        - select colors and prefabs
    20. //        - slot B2GConfig instance into Bitmap2Grid script
    21. //    choose dimensional spacing (defaults to 1,1)
    22. //    go to inspector window for this script, hit GENERATE
    23. //
    24. // @kurtdekker
    25. //
    26.  
    27. public class Bitmap2Grid : MonoBehaviour
    28. {
    29.     [Header( "Texture to use for layout.")]
    30.     public Texture2D Bitmap;
    31.  
    32.     [Header( "B2GConfig to use.")]
    33.     public B2GConfig Config;
    34.  
    35.     [Header( "Dimensional spacing.")]
    36.     public Vector2 Spacing = Vector3.one;
    37.  
    38.     // do your own axis remapping here if you want X/Z for instance
    39.     Vector3 ComputePosition( int i, int j)
    40.     {
    41.         float x = i * Spacing.x;
    42.         float y = j * Spacing.y;
    43.  
    44.         // optionally center?
    45.         x -= (Bitmap.width - 1) * Spacing.x / 2;
    46.         y -= (Bitmap.height - 1) * Spacing.y / 2;
    47.  
    48.         return new Vector3( x, y, 0);
    49.     }
    50.  
    51.     void RegenerateGrid()
    52.     {
    53.         // TODO: destroy the old one... I'll leave you to do this by hand!
    54.  
    55.         Transform parent = new GameObject( "Bitmap2Grid-Generated").transform;
    56.         parent.SetParent( transform);
    57.         parent.localPosition = Vector3.zero;
    58.         parent.localRotation = Quaternion.identity;
    59.  
    60.         for (int j = 0; j < Bitmap.height; j++)
    61.         {
    62.             for (int i = 0; i < Bitmap.width; i++)
    63.             {
    64.                 // if you get an error here, check your texture import settings to be read/write
    65.                 var c = Bitmap.GetPixel( i, j);
    66.  
    67.                 var prefab = Config.LookupColor( c);
    68.  
    69.                 if (prefab)
    70.                 {
    71.                     var copy = Instantiate<GameObject>( prefab, parent);
    72.                     copy.transform.localPosition = ComputePosition( i, j);
    73.                 }
    74.             }
    75.         }
    76.     }
    77.  
    78. #if UNITY_EDITOR
    79.     [CustomEditor( typeof( Bitmap2Grid))]
    80.     public class Bitmap2GridEditor : Editor
    81.     {
    82.         public override void OnInspectorGUI()
    83.         {
    84.             Bitmap2Grid b2g = (Bitmap2Grid)target;
    85.  
    86.             DrawDefaultInspector();
    87.  
    88.             EditorGUILayout.BeginVertical();
    89.  
    90.             if (GUILayout.Button( "REGENERATE GRID"))
    91.             {
    92.                 b2g.RegenerateGrid();
    93.  
    94.                 EditorSceneManager.MarkSceneDirty(
    95.                     UnityEngine.SceneManagement.SceneManager.GetActiveScene());
    96.             }
    97.  
    98.             EditorGUILayout.EndVertical();
    99.         }
    100.     }
    101. #endif
    102. }
    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo