我想弄清楚如何將創建游戲物件簡化為回圈。這是我在陷入困境之前所做的事情,以便更容易閱讀。
`使用 System.Collections; 使用 System.Collections.Generic; 使用 UnityEngine;
公共類游戲物件:MonoBehaviour {
public GameObject cube1, cube2, cube3, cube4, cube5, cube6, cube7, cube8, cube9;
public GameObject col1P1, col1P2, col1P3, col1P4, col1P5, col1P6;
public GameObject col2P1, col2P2, col2P3, col2P4, col2P5, col2P6;
public GameObject col3P1, col3P2, col3P3, col3P4, col3P5, col3P6;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyUp (KeyCode.G))
{
GameObject cube1 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube1.transform.position = new Vector3(5f, 0.5f, 0);
GameObject cube2 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube2.transform.position = new Vector3 (6f, 0.5f, 0);
GameObject cube3 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube3.transform.position = new Vector3 (7f, 0.5f, 0);
GameObject cube4 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube4.transform.position = new Vector3 (5f, 1.5f, 0);
GameObject cube5 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube5.transform.position = new Vector3 (6f, 1.5f, 0);
GameObject cube6 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube6.transform.position = new Vector3 (7f, 1.5f, 0);
GameObject cube7 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube7.transform.position = new Vector3 (5f, 2.5f, 0);
GameObject cube8 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube8.transform.position = new Vector3 (6f, 2.5f, 0);
GameObject cube9 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube9.transform.position = new Vector3 (7f, 2.5f, 0);
GameObject col1P1 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P1.transform.position = new Vector3 (0, 1f, -5f);
GameObject col1P2 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P2.transform.position = new Vector3 (0, 3f, -5);
GameObject col1P3 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P3.transform.position = new Vector3 (0, 5f, -5f);
GameObject col1P4 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P4.transform.position = new Vector3 (0, 7f, -5f);
GameObject col1P5 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P5.transform.position = new Vector3 (0, 9f, -5f);
GameObject col1P6 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P6.transform.position = new Vector3 (0, 11f, -5f);
GameObject col2P1 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P1.transform.position = new Vector3 (0, 1f, 0);
GameObject col2P2 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P2.transform.position = new Vector3 (0, 3f, 0);
GameObject col2P3 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P3.transform.position = new Vector3 (0, 5f, 0);
GameObject col2P4 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P4.transform.position = new Vector3 (0, 7f, 0);
GameObject col2P5 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P5.transform.position = new Vector3 (0, 9f, 0);
GameObject col2P6 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P6.transform.position = new Vector3 (0, 11f, 0);
GameObject col3P1 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P1.transform.position = new Vector3 (0, 1f, 5f);
GameObject col3P2 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P2.transform.position = new Vector3 (0, 3f, 5);
GameObject col3P3 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P3.transform.position = new Vector3 (0, 5f, 5f);
GameObject col3P4 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P4.transform.position = new Vector3 (0, 7f, 5f);
GameObject col3P5 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P5.transform.position = new Vector3 (0, 9f, 5f);
GameObject col3P6 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P6.transform.position = new Vector3 (0, 11f, 5f);
}
}
}'
感謝您的幫助并指出我可以改進的更好方法。
uj5u.com熱心網友回復:
答案在于您匯入但未使用的命名空間 :-) System.Collections.Generic。使用List或適合應用程式需要的東西。
了解序列中發生的模式,例如,每 3 個值后創建 Cube,X 值變為 5,Y 值增加 1。對于每 3 個值,我們可以使用一個變數,如 _cubeOffset
因此,由于您必須創建 9 個立方體,因此從XValue = 4.0f和開始YValue = -0.5f。現在通過i從0to開始創建 9 個立方體的回圈8。第一步是檢查是否i % 3 == 0,如果是,然后設定XValue = 4.0f和YValue = 0.5f
_cubeOffset = 3;
_startXCube = 4.0f;
_startYCube = -0.5f;
_cubes = new List < GameObject > ( TOTAL_CUBES );
for ( int i = 0; i < TOTAL_CUBES; i ) {
/*
* This below check will run when i = 0, 3, 6
* at this point we set the _startXCube to 4.0f and
* increment the previous value of _startYCube by 1.
* Therefore at i = 0
* _startXCube = 4.0f
* _startYCube = 0.5f, since the previous value of _startYCube is -0.5f
*
* i = 3
* _startXCube = 4.0f
* _startYCube = 1.5f, since the previous value of _startYCube is 0.5f
*
* i = 6
* _startXCube = 4.0f
* _startYCube = 2.5f, since the previous value of _startYCube is 1.5f
*/
if ( i % _cubeOffset == 0 ) {
_startXCube = 4.0f;
_startYCube = 1.0f;
}
_startXCube = 1.0f; // Incrementing _startXCube since we need 5.0f, 6.0f and 7.0f values
GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cube );
gb.transform.position = new Vector3 ( _startXCube, _startYCube, 0.0f );
_cubes.Add ( gb );
Debug.Log ( $"<color=lightblue>Creating Cube -> X:{_startXCube} Y:{_startYCube} Z:0</color>" );
}
同樣,我們可以為圓柱體創建邏輯,如下所示:
_startZCylinder = -10.0f;
_cylinders = new List < List < GameObject > > ( TOTAL_COLUMNS );
for ( int i = 0; i < TOTAL_COLUMNS; i ) {
_cylinders.Add ( new List < GameObject > ( TOTAL_CYLINDERS ) );
_startYCylinder = -1.0f;
_startZCylinder = 5.0f;
for ( int j = 0; j < TOTAL_CYLINDERS; j ) {
_startYCylinder = 2.0f;
GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cylinder );
gb.transform.position = new Vector3 ( 0.0f, _startYCylinder, _startZCylinder );
_cylinders [ _cylinders.Count - 1 ].Add ( gb );
Debug.Log ( $"<color=lightblue>Creating Cylinder -> X:{0.0f} Y:{_startYCylinder} Z:{_startZCylinder}</color>" );
}
}
請注意,對于這些代碼片段,我使用了如下串列資料結構:
private List < GameObject > _cubes;
private List < List < GameObject > > _cylinders;
這是整個腳本
using System.Collections.Generic;
using UnityEngine;
public class CreatePrimitive : MonoBehaviour {
#region Private Field
private int _cubeOffset;
private float _startXCube;
private float _startYCube;
private List < GameObject > _cubes;
private int _cylinderOffset;
private float _startYCylinder;
private float _startZCylinder;
private List < List < GameObject > > _cylinders;
private const int TOTAL_CUBES = 9;
private const int TOTAL_COLUMNS = 3;
private const int TOTAL_CYLINDERS = 6;
#endregion Private Field
#region MonoBehaviour Callback
private void Start () {
_cubeOffset = 3;
_startXCube = 4.0f;
_startYCube = -0.5f;
_cubes = new List < GameObject > ( TOTAL_CUBES );
for ( int i = 0; i < TOTAL_CUBES; i ) {
/*
* This below check will run when i = 0, 3, 6
* at this point we set the _startXCube to 4.0f and
* increment the previous value of _startYCube by 1.
* Therefore at i = 0
* _startXCube = 4.0f
* _startYCube = 0.5f, since the previous value of _startYCube is -0.5f
*
* i = 3
* _startXCube = 4.0f
* _startYCube = 1.5f, since the previous value of _startYCube is 0.5f
*
* i = 6
* _startXCube = 4.0f
* _startYCube = 2.5f, since the previous value of _startYCube is 1.5f
*/
if ( i % _cubeOffset == 0 ) {
_startXCube = 4.0f;
_startYCube = 1.0f;
}
_startXCube = 1.0f; // Incrementing _startXCube since we need 5.0f, 6.0f and 7.0f values
GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cube );
gb.transform.position = new Vector3 ( _startXCube, _startYCube, 0.0f );
_cubes.Add ( gb );
Debug.Log ( $"<color=lightblue>Creating Cube -> X:{_startXCube} Y:{_startYCube} Z:0</color>" );
}
_startZCylinder = -10.0f;
_cylinders = new List < List < GameObject > > ( TOTAL_COLUMNS );
for ( int i = 0; i < TOTAL_COLUMNS; i ) {
_cylinders.Add ( new List < GameObject > ( TOTAL_CYLINDERS ) );
_startYCylinder = -1.0f;
_startZCylinder = 5.0f;
for ( int j = 0; j < TOTAL_CYLINDERS; j ) {
_startYCylinder = 2.0f;
GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cylinder );
gb.transform.position = new Vector3 ( 0.0f, _startYCylinder, _startZCylinder );
_cylinders [ _cylinders.Count - 1 ].Add ( gb );
Debug.Log ( $"<color=lightblue>Creating Cylinder -> X:{0.0f} Y:{_startYCylinder} Z:{_startZCylinder}</color>" );
}
}
}
#endregion MonoBehaviour Callback
}
uj5u.com熱心網友回復:
你可以創建一個靜態函式,就像這樣
public class CubeHelper
{
public static GameObject Creating(Vector3 position, PrimitiveType primitiveType)
{
GameObject createdGameObject = GameObject.CreatePrimitive(primitiveType)
createdGameObject.transform.position = position;
return createdGameObject;
}
}
然后只是傳遞引數:
Gameobject sthg = CubeHelper.Creating("your vector3", "your primitive type");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/336651.html
