2017-08-23 21 views
0

我已經爲程序化多維數據集編寫了腳本。基本上我想製作一個能夠通過參數控制的寬度,長度和高度的建築物。所以通過改變參數,我可以製作不同的形狀,如窗戶,門和屋頂等等。但我不知道該怎麼做。如何重用這個腳本的不同形狀?如何在統一C中重用不同形狀的腳本C#

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(); 
} 

} 
+0

我假設你加入這裏的腳本是附加到遊戲對象要修改每個立方體的腳本,是不是? –

+0

你不應該調用update() –

+0

中的cube(),因爲我想在運行時更改多維數據集的參數,並且這個腳本連接到GameObject,所以我在update()中調用cube()。我可以使GameObjects並附上腳本,但這不是好的方法來做到這一點..我想動態地做到這一點 – Aqil

回答

1

你並不真的需要編程從頭開始,場景中的立方體或其他元素的尺寸。 Unity已經提供了一些可以在遊戲中實例化的基元,並從腳本訪問它們的屬性來修改它們的值。

如果你想創建一個環境編程方式使用GameObjects與原語(如立方體,球體...)或自定義GameObjects(如3D模型你攪拌機創建的),你可以做這樣的事情:

//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); 

} 

您可以根據需要隨意修改該功能,只需對名稱進行整理即可。

​​

相關問題