c# - 如何使用循环简化 GameObject 按键代码?

标签 c# unity-game-engine

我正在尝试找出如何将游戏对象的创建简化为循环。这是我在不知如何做才能更容易阅读之前所得到的。

`使用 System.Collections; 使用 System.Collections.Generic; 使用UnityEngine;

公共(public)类 game_Objects: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);
    }
}

}'

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

最佳答案

答案在于您导入但未使用的命名空间:-) System.Collections.Generic。使用 List 或适合应用程序需求的内容。

了解序列中出现的模式,例如对于每 3 个值之后创建的 Cube,X 值变为 5,Y 值增加 1。对于每 3 个值,我们可以使用像 _cubeOffset

这样的变量

因此,由于您必须创建 9 个立方体,因此从 XValue = 4.0fYValue = -0.5f 开始。现在,通过将 i08 开始创建 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>" );
}

类似地,我们可以为 Cylinders 创建逻辑,如下所示:

_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
}

关于c# - 如何使用循环简化 GameObject 按键代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69702570/

相关文章:

c# - 使用 JsonFX 反序列化字典

c# - WPF-mvvm。我怎样才能实现这样的 View (设计,图形用户界面)?

c# - 使用递归函数解析xml值时出错

c# - 为什么在代码隐藏中将 display 设置为 none 不会将其添加到 HTML 中?

unity-game-engine - Android AR场景中的透明视频

c# - 如何在 Unity3D android 游戏中添加动画视频作为启动画面?

animation - Unity动画循环偏移不起作用

c# - 列表 'Except' 比较 - 忽略大小写

c# - 是否真的需要只为托管资源实现处置模式

unity-game-engine - 如何知道Unity Build应用程序是32位还是64位