0

I have written a script for a procedural cube. Basically I want to make a building whose width, length and height I can control through parameters. So by changing parameters I can make different shapes like windows and door and roof and many more. But I don't have any idea how to do that. How to reuse this script for different shapes?

using UnityEngine;

public class genralWindows: MonoBehaviour 
{
 public float height = 0.8 f;

 public float width = 0.6 f;
 public float length = 0;

 void Start() 
 {
  cube();
  gameObject.GetComponent < Renderer > ().material.color = Color.white;
 }

 public void cube() 
 {

  MeshFilter mf1 = GetComponent < MeshFilter > ();
  Mesh mesh1 = mf1.mesh;
  //vertices
  Vector3[] vertices = new Vector3[] 
  {

   //front face
   new Vector3(-width, height, length), //left top front,  0

    new Vector3(width, height, length), //right top front,  height

    new Vector3(-width, -height, length), //left bottom front,   2

    new Vector3(width, -height, length), //right bottom front,   width

    //BACK FACE
    new Vector3(width, height, -length), //right top back,  4

    new Vector3(-width, height, -length), //left top back, length

    new Vector3(width, -height, -length), //right bottom back,  6

    new Vector3(-width, -height, -length), //left bottom back, 7

    //LEFT FACE
    new Vector3(-width, height, -length), //left top back,width

    new Vector3(-width, height, length), //left top front,9

    new Vector3(-width, -height, -length), //left bottom back,height0,

    new Vector3(-width, -height, length), //left bottom front,heightheight

    //RIGHT FACE
    new Vector3(width, height, length), //right top front height2

    new Vector3(width, height, -length), //right top back heightwidth

    new Vector3(width, -height, length), //right bottom front height4

    new Vector3(width, -height, -length), //right bottom back heightheight

    //TOP FACE
    new Vector3(-width, height, -length), //left top back height6

    new Vector3(width, height, -length), //right top back height7

    new Vector3(-width, height, length), //left top front heightwidth

    new Vector3(width, height, length), //right top front height9

    //BOTTOM FACE

    new Vector3(-width, -height, length), //left bottom front 20

    new Vector3(width, -height, length), //right bottom front 2height

    new Vector3(-width, -height, -length), //left bottom back 22

    new Vector3(width, -height, -length), //right bottom back 2width
  };

  //triangles// 3 points clockwise determines which side is visible

  int[] triangles = new int[] 
  {
   //front face
   0,
   2,
   3, //first triangle

   3,
   1,
   0, //second triangle

   //back face
   4,
   6,
   7, //first triangle

   7,
   5,
   4, //second triangle

   //left  face
   8,
   10,
   11, //first triangle

   11,
   9,
   8, //second triangle

   //right face
   12,
   14,
   15, //first triangle

   15,
   13,
   12, //second triangle

   //top face
   16,
   18,
   19, //first triangle

   19,
   17,
   16, //second triangle

   //bottom face
   20,
   22,
   23, //first triangle

   23,
   21,
   20 //second triangle

  };

  //UVs
  Vector2[] uvs1 = new Vector2[] 
  {

   //front face// 0,0 is bottom left, 1,1 is top right
    new Vector2(0, 1),
    new Vector2(0, 0),
    new Vector2(1, 1),
    new Vector2(1, 0),

    new Vector2(0, 1),
    new Vector2(0, 0),
    new Vector2(1, 1),
    new Vector2(1, 0),

    new Vector2(0, 1),
    new Vector2(0, 0),
    new Vector2(1, 1),
    new Vector2(1, 0),

    new Vector2(0, 1),
    new Vector2(0, 0),
    new Vector2(1, 1),
    new Vector2(1, 0),

    new Vector2(0, 1),
    new Vector2(0, 0),
    new Vector2(1, 1),
    new Vector2(1, 0),

    new Vector2(0, 1),
    new Vector2(0, 0),
    new Vector2(1, 1),
    new Vector2(1, 0)

  };

  mesh1.Clear();
  mesh1.vertices = vertices;
  mesh1.triangles = triangles;
  mesh1.uv = uvs1;
  mesh1.RecalculateNormals();

 }

 // Update is called once per frame
 void Update() 
 {
  cube();
 }

}
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Aqil
  • 59
  • 2
  • 6
  • I assume the script you are adding here is the script attached to each cube GameObject you want to modify, isn't? – Ignacio Alorre Aug 23 '17 at 12:24
  • And you should not be calling cube() in update() – Ignacio Alorre Aug 23 '17 at 15:02
  • i am calling cube() in update() because i want to change parameter of cube during run time and yeah this script is attached to GameObject.. i can make make GameObjects and attach the script but this isn't good way to do that.. i want to do it dynamically – Aqil Aug 23 '17 at 15:21
  • Well I give you more details in my answer below. But basically the update is call in everyfame, what means lots of calls per second. And it is not necesary that you add a script component to modify attibutes that you can access directly without any additional script. Please check the answer I gave and let me know if anything is not clear so I can keep helping you. – Ignacio Alorre Aug 23 '17 at 15:52

2 Answers2

1

You need to use inheritance.
Make a class Cube like this:

public class Cube:MonoBehaviour{
}

Then make other classes like this:

public class Door:Cube{
}

You can get more details here: https://unity3d.com/learn/tutorials/topics/scripting/inheritance

ZayedUpal
  • 1,603
  • 11
  • 12
1

You don't really need to program the dimensions of a cube or other elements in your scene from scratch. Unity already offers some primitives you can instantiate in your game, and access their attributes from your script to modify their values.

In case you want to create an Enviroment programatically using GameObjects with primitives ( like cubes, spheres...) or custom GameObjects (like 3D models you created on Blender) you can do something like this:

//You will need to pass in the inspector a GameObject as parameter in the correct field
public GameObject customGameObject;

void Start()
{
    generateEnviroment()
}


void generateEnviroment()
{
    //You instantiate a GameObject of type cube
    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    //You change its position
    cube.transform.position = new Vector3(0, 0.5F, 0);
    // Widen the object by 0.1
    cube.transform.localScale += new Vector3(0.1F, 0, 0);
    //Change material properties, assuming it has a material component
    Renderer rend = cube.GetComponent<Renderer>();
    rend.material.shader = Shader.Find("Specular");
    rend.material.SetColor("_SpecColor", Color.red);


    //In case you want to add other type of GameObject, like a car or sth you have created:
    GameObject myGameObject = GameObject.Instantiate (customGameObject);

}

You can make that function as long as complex as you want, just being tidy with names.

https://docs.unity3d.com/ScriptReference/GameObject.CreatePrimitive.html

Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94