如何将每组楼梯作为孩子添加到父级中,使其成为正确的父级?

时间:2018-11-19 22:47:45

标签: c# unity3d

在第一个脚本中:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateStairsUnits : MonoBehaviour
{
    [Header("Stairs Units Prefab")]
    public GameObject stairsUnitsPrefab;
    [Space(5)]
    [Header("Settings")]
    [Range(1, 20)]
    public int numberOfUnits = 1;
    public static GameObject Unit;

    private int oldNumberOfUnits = 0;

    // Use this for initialization
    void Start ()
    {
        oldNumberOfUnits = numberOfUnits;

        var unitsParent = GameObject.Find("Stairs Units");
        for (int i = 0; i < numberOfUnits; i++)
        {
            Unit = Instantiate(stairsUnitsPrefab, unitsParent.transform);
            Unit.name = "Stairs " + i.ToString();
        }
    }

    // Update is called once per frame
    void Update ()
    {

    }
}

因此,现在在“楼梯单位”下,例如,有5个楼梯(楼梯0,楼梯1,楼梯2,楼梯3,楼梯4)是该脚本的副本:

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class GenerateStairs : MonoBehaviour
{
    [Header("Stairs Prefb")]
    public GameObject stairsPrefab;
    [Space(5)]
    [Header("Platforms")]
    public bool addPlatforms = false;
    public GameObject platformsPrefab;
    [Space(5)]
    [Header("Settings")]
    public float delay = 3;
    public int stairsNumber = 5;
    public Vector3 stairsStartPosition;
    public Vector3 stairSize;
    public Vector3 stairsSize;
    public float stepWidthFactor = 1f;

    private Vector3 stairsPosition;
    private GameObject stairsParent;

    // Use this for initialization
    void Start()
    {
        stairsParent = new GameObject();
        stairsParent.name = "Stairs";
        stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;
        StartCoroutine(BuildStairs());
    }

    // Update is called once per frame
    void Update()
    {

    }

    private IEnumerator BuildStairs()
    {
        for (int i = 1; i <= stairsNumber; i++)
        {

            stairsPosition = new Vector3(
                    stairsStartPosition.x,
                    stairsStartPosition.y + (i * stairsSize.y),
                    stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

            GameObject stair = Instantiate(
                    stairsPrefab,
                    stairsPosition,
                    Quaternion.identity);

            stair.tag = "Stair";
            stair.transform.parent = stairsParent.transform;
            stair.transform.localScale = stairSize;

            yield return new WaitForSeconds(delay);
        }

        stairsParent.AddComponent<MoveObjects>().Init();
    }
}

现在在第二个脚本中,我想成为每个楼梯单元的子级的“楼梯”: 例如,在“楼梯”下,“ 0”应为“楼梯”。然后在楼梯1下也应该有楼梯,在楼梯2和3和4下也应该有楼梯。

但是我尝试这样做的方式:

stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;

不要将每个楼梯放在楼梯0、1、2、3、4之下

1 个答案:

答案 0 :(得分:1)

所以我能看到的是您实例化了5个楼梯,每个楼梯都包含实例化更多楼梯的类。换句话说,“楼梯号”是其他楼梯需要变换的变换。将当前其他楼梯设置为“楼梯单位”,您需要做的就是不带.parent的Stair#变换。同样在开始时删除。快速模拟楼梯单元/楼梯#<--- transform.parent会将其他楼梯设置为楼梯单元,我想您是楼梯单元/楼梯#/更多楼梯,希望对视觉效果有所帮助。 >

stairsParent.transform.parent = GenerateStairsUnits.Unit.transform;

然后

private IEnumerator BuildStairs()
{
    for (int i = 1; i <= stairsNumber; i++)
    {

        stairsPosition = new Vector3(
                stairsStartPosition.x,
                stairsStartPosition.y + (i * stairsSize.y),
                stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

        GameObject stair = Instantiate(
                stairsPrefab,
                stairsPosition,
                Quaternion.identity);

        stair.tag = "Stair";
        stair.SetParent(transform);//just use this transform I believe this would be Stairs#
        stair.transform.localScale = stairSize;

        yield return new WaitForSeconds(delay);
    }

    stairsParent.AddComponent<MoveObjects>().Init();
}