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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[HELP]I would like advice as to if and or how I can write this code more eloquently.

Discussion in 'Scripting' started by CodyG, May 2, 2015.

  1. CodyG

    CodyG

    Joined:
    May 2, 2015
    Posts:
    3
    Hey Guys and Gals I was wondering if I could get some advice on my code? I was hoping that someone could give me some pointers on how to write something like this more eloquently - I feel like too much of this is hard coded in - which seems to make this less extensible - This was three solid days of head scratching and hair pulling - Any advice would be greatly appreciated!! :D

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class TileMap : MonoBehaviour {
    5.  
    6.     //Reference to cube prefabs scale 1, 0.25, 1
    7.     public GameObject sPrefab;
    8.     public GameObject iPrefab;
    9.     public GameObject dPrefab;
    10.  
    11.     //Width x and height y of grid
    12.     public float gridX = 8f;
    13.     public float gridY = 8f;
    14.  
    15.     //Variable to store the traslations of our tiles
    16.     Vector3 subs;
    17.     Vector3 dntwn;
    18.     Vector3 inCit;
    19.  
    20.     void Start() {
    21.         //Place the districts
    22.         createSubs (0, 0);
    23.         createIC ((Mathf.FloorToInt(subs.x + 1)), 0); //farthest x coord of previous district
    24.         createDT ((Mathf.FloorToInt(subs.x + 1)), (Mathf.FloorToInt(inCit.z + 1))); //farthest x coord and farthest y coord of last district
    25.         createIC ((Mathf.FloorToInt(subs.x + 1)), (Mathf.FloorToInt(dntwn.z + 1))); //farthest x coord and farthest y coord of last district
    26.         createSubs ((Mathf.FloorToInt(inCit.x + 1)), 0); //farthest x coord of previous district
    27.      
    28.         //Move the camera ~ halfway up and across the map + up half the distance of x + y
    29.         int cameraHeight = Mathf.FloorToInt ((gridX + gridY) / 2);
    30.         Camera.main.transform.position = new Vector3(Mathf.FloorToInt((gridX/2)), cameraHeight, Mathf.FloorToInt((gridY/2)));
    31.      
    32.      
    33.     }//End of Start
    34.  
    35.     //set the Suburbs to be a third of the width of the map and the whole height
    36.     public void createSubs(int i, int k){
    37.         for (int y = 0; y < (Mathf.FloorToInt((gridY/3)) * 3); y++) {
    38.             for(int x = 0; x < Mathf.FloorToInt((gridX/3)); x++){
    39.                 subs = new Vector3(i + x, 0 ,k + y);
    40.                 sPrefab.renderer.material.color = Color.green;
    41.                 Instantiate(sPrefab, subs, Quaternion.identity);
    42.             }
    43.         }
    44.     }
    45.  
    46.     //set te Inner City to be a third of the width of the map and a third of the height
    47.     public void createIC(int i, int k){
    48.         for (int y = 0; y < Mathf.FloorToInt((gridY/3)); y++) {
    49.             for(int x = 0; x < Mathf.FloorToInt((gridX/3)); x++){
    50.                 inCit = new Vector3(i + x, 0 ,k + y);
    51.                 iPrefab.renderer.material.color = Color.blue;
    52.                 Instantiate(iPrefab, inCit, Quaternion.identity);
    53.             }
    54.         }
    55.     }
    56.  
    57.     //set te Downtown to be a third of the width of the map and a third of the height
    58.     public void createDT(int i, int k){
    59.         for (int y = 0; y < Mathf.FloorToInt((gridY/3)); y++) {
    60.             for(int x = 0; x < Mathf.FloorToInt((gridX/3)); x++){
    61.                 dntwn = new Vector3(i + x, 0 ,k + y);
    62.                 dPrefab.renderer.material.color = Color.cyan;
    63.                 Instantiate(dPrefab, dntwn, Quaternion.identity);
    64.             }
    65.         }
    66.     }
    67.  
    68. } //End of class
     
    Last edited: May 2, 2015
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Use code tags for a start.

    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Like so:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class TileMap : MonoBehaviour
    6. {
    7.  
    8.     #region Fields
    9.  
    10.     //Reference to cube prefabs scale 1, 0.25, 1
    11.     public GameObject sPrefab;
    12.     public GameObject iPrefab;
    13.     public GameObject dPrefab;
    14.     //Width x and height y of grid
    15.     public float gridX = 8f;
    16.     public float gridY = 8f;
    17.     //Variable to store the traslations of our tiles
    18.     Vector3 subs;
    19.     Vector3 dntwn;
    20.     Vector3 inCit;
    21.     //Store the farthest x and farthest y
    22.     int farthestX;
    23.     int farthestY;
    24.    
    25.     #endregion
    26.    
    27.     #region CONSTRUCTOR
    28.    
    29.     void Start() {
    30.         //Place the districts
    31.         createSubs (0, 0);
    32.         createIC ((Mathf.FloorToInt(subs.x + 1)), 0);
    33.         createDT ((Mathf.FloorToInt(subs.x + 1)), (Mathf.FloorToInt(inCit.z + 1)));
    34.         createIC ((Mathf.FloorToInt(subs.x + 1)), (Mathf.FloorToInt(dntwn.z + 1)));
    35.         createSubs ((Mathf.FloorToInt(inCit.x + 1)), 0);
    36.         //Move the camera ~ halfway up and across the map + up half the distance of x + y
    37.         int cameraHeight = Mathf.FloorToInt ((gridX + gridY) / 2);
    38.         Camera.main.transform.position = new Vector3(Mathf.FloorToInt((gridX/2)), cameraHeight, Mathf.FloorToInt((gridY/2)));
    39.     }
    40.    
    41.     #endregion
    42.    
    43.     #region Methods
    44.    
    45.     //set the Suburbs to be a third of the width of the map and the whole height
    46.     public void createSubs(int i, int k)
    47.     {
    48.         for (int y = 0; y < (Mathf.FloorToInt((gridY/3)) * 3); y++) {
    49.             for(int x = 0; x < Mathf.FloorToInt((gridX/3)); x++){
    50.                 subs = new Vector3(i + x, 0 ,k + y);
    51.                 sPrefab.renderer.material.color = Color.green;
    52.                 Instantiate(sPrefab, subs, Quaternion.identity);
    53.                 farthestX = (i+x);
    54.                 farthestY = (k+y);
    55.             }
    56.         }
    57.     }
    58.     //set te Inner City to be a third of the width of the map and a third of the height
    59.     public void createIC(int i, int k)
    60.     {
    61.         for (int y = 0; y < Mathf.FloorToInt((gridY/3)); y++) {
    62.             for(int x = 0; x < Mathf.FloorToInt((gridX/3)); x++){
    63.                 inCit = new Vector3(i + x, 0 ,k + y);
    64.                 iPrefab.renderer.material.color = Color.blue;
    65.                 Instantiate(iPrefab, inCit, Quaternion.identity);
    66.                 farthestX = (i+x);
    67.                 farthestY = (k+y);
    68.             }
    69.         }
    70.     }
    71.     //set te Downtown to be a third of the width of the map and a third of the height
    72.     public void createDT(int i, int k)
    73.     {
    74.         for (int y = 0; y < Mathf.FloorToInt((gridY/3)); y++) {
    75.             for(int x = 0; x < Mathf.FloorToInt((gridX/3)); x++){
    76.                 dntwn = new Vector3(i + x, 0 ,k + y);
    77.                 dPrefab.renderer.material.color = Color.cyan;
    78.                 Instantiate(dPrefab, dntwn, Quaternion.identity);
    79.                 farthestX = (i+x);
    80.                 farthestY = (k+y);
    81.             }
    82.         }
    83.     }
    84.    
    85.     #endregion
    86.    
    87. }
    88.  
     
    CodyG likes this.
  3. CodyG

    CodyG

    Joined:
    May 2, 2015
    Posts:
    3
    Thanks :D
     
  4. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    From what I've read, you're doing just fine. But if you really want to go extreme, you might want to design some form of a proper grid editor that exports and imports the data which defines the what to instantiate and where.