当前位置: 首页 > 知识库问答 >
问题:

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

周辰沛
2023-03-14

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

`使用系统。收藏;使用系统。收藏。通用的使用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个答案

文自怡
2023-03-14

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

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");
越学义
2023-03-14

答案在于您导入的名称空间,但不使用:-)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
}
 类似资料:
  • 我希望直接使用div,progress,它们需要保持响应式,如何简化呢? ...

  • 这是我的代码: 我所要做的就是使esc按钮与backspace按钮完全相同。所以我想删除

  • 问题内容: 一个简单的程序,用于计算数字平方并存储结果: 这两个选项的数组处理是否可能有所不同?我的实际程序会有一些更复杂的东西,但这 是我需要尽可能简单地并行化的一种计算方式 ,但没有这种结果。 问题答案: 从以下文档中: 如果您知道所调用的函数基于已编译的扩展,该扩展会在大多数计算过程中释放Python全局解释器锁(GIL)… 问题在于,在这种情况下,您不知道这一点。Python本身仅允许一次

  • 下面是我想在用户健康和龙健康时循环的功能

  • 我想重构这两个方法,除了“maxResult ()”之外,它们实际上是相同的,这两个方法指的是两个不同的 get,一个返回我单个用户,另一个返回列表。我如何简化这两种方法(总是如果有意义的话)这是两种方法: 第一种方法: 第二种方法:

  • 问题内容: 我只是在尝试简单绘图程序的基础知识。我有一个程序可以绘制一个矩形,然后让我向任意方向移动它。我使用线程进行平滑操作。但是,令我困扰的是,如果我按下三个键并释放其中两个,程序将不会检测到第三个,从而使矩形保持静止。 我在这里要求太多,还是与硬件有关?我有一个很便宜的键盘。 如果您想兜风,这里是完整的程序。 问题答案: 正如在类似问题中所讨论的,不要使用KeyListener,而要使用键绑