我有3个脚本,一个重复一个脚本,第三个应该获取创建的对象,但我无法获取此对象。我该如何获得物品?
第一个脚本是“生成楼梯单位”。该脚本附加到一个空的GameObject。
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;
private List<GameObject> units = new List<GameObject>();
// 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();
units.Add(Unit);
Unit.AddComponent<MoveObjects>();
}
}
// Update is called once per frame
void Update ()
{
}
}
GenerateStairsUnits脚本复制了第二个脚本Generate Stairs的预制件:
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;
public GameObject moveobjects;
private Vector3 stairsPosition;
private GameObject stairsParent;
// Use this for initialization
void Start()
{
moveobjects = GameObject.Find("Move Objects");
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 = transform;
stair.transform.localScale = stairSize;
yield return new WaitForSeconds(delay);
}
}
}
GenerateStairsUnits还添加了第三个脚本“移动对象”作为组件:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MoveObjects : MonoBehaviour
{
public List<GameObject> objectsToMove = new List<GameObject>();
public AnimationCurve curve;
public float stepsPerSecond = 1f;
public bool changeDirection = false;
private Vector3 trackStart;
private Vector3 trackEnd;
private Vector3 horizontalTravel;
private float verticalTravel;
private float divisor;
private float phase = 0f;
// Use this for initialization
public void Init()
{
if (curve == null)
{
curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
}
curve.preWrapMode = WrapMode.Clamp;
curve.postWrapMode = WrapMode.Clamp;
trackStart = objectsToMove[0].transform.position;
int count = objectsToMove.Count;
var span = objectsToMove[count - 1].transform.position - trackStart;
divisor = 1f / count;
horizontalTravel = (count + 1) * span * divisor;
horizontalTravel.y = 0f;
verticalTravel = span.y;
trackEnd = trackStart + (count + 1) * span / count;
}
// Update is called once per frame
void Update()
{
if (objectsToMove != null && objectsToMove.Count > 0 && curve != null)
{
AnimationCurve();
}
}
private void AnimationCurve()
{
phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);
for (int i = 0; i < objectsToMove.Count; i++)
{
float t = Mathf.Repeat(phase + i * divisor, 1f);
// Get the height of the curve at this step.
float curveHeight = curve.Evaluate(t) * verticalTravel;
if (changeDirection)
{
objectsToMove[i].transform.position = trackStart // First step
- horizontalTravel * t // evenly spaced horizontal
+ curveHeight * Vector3.up; // curving vertical
}
else
{
objectsToMove[i].transform.position = trackStart // First step
+ horizontalTravel * t // evenly spaced horizontal
+ curveHeight * Vector3.up; // curving vertical
}
}
}
private void StraightLineTrack()
{
float divisor = 1f / objectsToMove.Count;
// Compute the current phase of the escalator,
// from 0 (1st step at track start) to 1 (1st step at track end)
phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);
// Place each step a proportional distance along the track.
for (int i = 0; i < objectsToMove.Count; i++)
{
float t = Mathf.Repeat(phase + i * divisor, 1f);
objectsToMove[i].transform.position = Vector3.Lerp(trackStart, trackEnd, t);
}
}
}
现在是问题:
复制GenerateStairs并为每个Stairs Unit创建楼梯并添加Move Objects组件后,结果为:
在层次结构中,我有空的GameObject名称Stairs Units。在“楼梯单位”下有单位楼梯0、1、1、2、3、4。
每个单元(例如楼梯0)还附加了“移动对象”脚本。
现在,我的问题是如何使每个单元的楼梯进入“移动对象”的objectsToMove列表。
在“移动对象”中,我有一个列表名称“ objectsToMove”。例如,在“楼梯0”下有10个楼梯,我需要将这10个楼梯设置为“楼梯0”的objectsToMove。然后是“楼梯1”的下10个楼梯,依此类推。但是我不知道如何将每个单元的楼梯添加到objectsToMove。
最后,objectsToMove将移动每个楼梯单元的楼梯。
答案 0 :(得分:1)
由于您的Stairs N
游戏对象在其上附加了两个脚本GenerateStairs
和MoveObjects
,因此您可以在生成以下内容之前调用MoveObjects
以获取GetComponent
的引用Stair
s,并将其传递给BuildStairs
函数。
void Start()
{
...
MoveObjects moveObjects = gameObject.GetComponent<MoveObjects>();
StartCoroutine(BuildStairs(moveObjects));
}
通过获取MoveObjects
的引用,您可以将该引用传递到BuildStairs
函数中,并将生成stair
的引用添加到{{1 }}。
修改函数并像下面一样传递objectsToMove
:
MoveObjects
关于GetComponent。