提问者:小点点

我如何简化GameObject键按下的代码与循环?


我试图找出如何简化创建游戏对象的循环。这是我在为如何使这本书更容易阅读而感到困惑之前所做的。

`使用系统。收藏;使用系统。收藏。通用的使用UnityEngine;

公共类游戏对象:单一行为{

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

}'

感谢您的帮助,并指出了我可以改进这一点的更好方法。


共2个答案

匿名用户

答案在于您导入的名称空间,但不使用:-)System。收藏。通用。使用列表或适合应用程序需要的内容。

理解序列中出现的模式,例如,对于每3个值后创建立方体,X值变为5,Y值递增1。对于每3个值,我们可以使用像_cubeOffset这样的变量

因此,由于您必须创建9个多维数据集,因此从XValue=4.0fYValue=-0.5f开始。现在,通过将i0取到8,开始创建9个多维数据集的循环。第一步是检查i%3==0,如果是,则设置XValue=4.0fYValue=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
}

匿名用户

您可以创建一个静态函数,如下所示

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