unity3d 将多个预设分配给仅允许添加一个预设的脚本

nvbavucw  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(168)

我有一个脚本,它把一个对象(预制)到一个预制的路径使用Leantween这工作得很好。
其工作方式是,您可以将一个对象分配给附加了Moveable脚本的"路径添加器"(MoveController)。
但是,我需要能够将运行时制作的新预制件添加到MoveController中,以便它们遵循路径。
那么我该如何在运行时将示例化对象附加到MoveController上呢?
谢谢你。
可移动脚本,也可示例化预制件

public class Moveable : MonoBehaviour
{
    [SerializeField] private float _speedMetersPerSecond = 25f;

    private Vector3? _destination;
    private Vector3 _startPosition;
    private float _totalLerpDuration;
    private float _elapsedLerpDuration;
    private Action _onCompleteCallback;
    public GameObject Electron;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
        var NextOnPath = Instantiate(Electron, transform.position, Quaternion.identity);
            NextOnPath.AddComponent<Moveable>();
        }

        if (_destination.HasValue == false)
            return;

        if (_elapsedLerpDuration >= _totalLerpDuration && _totalLerpDuration > 0)
            return;

        _elapsedLerpDuration += Time.deltaTime;
        float percent = (_elapsedLerpDuration / _totalLerpDuration);
        Debug.Log($"{percent} = {_elapsedLerpDuration} / {_totalLerpDuration}");

        transform.position = Vector3.Lerp(_startPosition, _destination.Value, percent);
    }

    public void MoveTo(Vector3 destination, Action onComplete = null)
    {
        var distanceToNextWaypoint = Vector3.Distance(transform.position, destination);
        _totalLerpDuration = distanceToNextWaypoint / _speedMetersPerSecond;

        _startPosition = transform.position;
        _destination = destination;
        _elapsedLerpDuration = 0f;
        _onCompleteCallback = onComplete;
    }
}

移动控制器脚本

using System.Linq;

public class MoverController : MonoBehaviour
{
    [SerializeField] private Moveable target;
    private List<Transform> _waypoints;
    private int _nextWaypointIndex;

    private void OnEnable()
    {
        _waypoints = GetComponentsInChildren<Transform>().ToList();
        _waypoints.RemoveAt(0);
        MoveToNextWaypoint();
    }

  private void MoveToNextWaypoint()
    {
        var targetWaypointTransform = _waypoints[_nextWaypointIndex];
        target.MoveTo(targetWaypointTransform.position, MoveToNextWaypoint);
        target.transform.LookAt(_waypoints[_nextWaypointIndex].position);
        _nextWaypointIndex++;

if (_nextWaypointIndex >= _waypoints.Count)
            _nextWaypointIndex = 0;
        
    }
}

如果我需要澄清什么就告诉我。
我知道这是一个相当负荷的问题,但我会非常感谢任何帮助!

8xiog9wr

8xiog9wr1#

是的,如果你使用的是Array,那么你可以添加任意数量的预制件。你仍然需要在脚本中设置代码来处理多个预制件。
虽然,听起来你可能不了解C#和Unity的基础知识,你可能想参加一个课程,阅读教程或观看Youtube视频。

更新

因为你的问题是如此广泛,我不能只给你代码。这是你应该付钱给一个编码器。但我会告诉你什么将需要做的一般想法。
1.在Moveable类中,你需要对它们进行track your var NextOnPath after instantiating操作,一个很好的方法是使用List〈〉(只要你不打算把任何东西转换成JSON),例如:

List<Moveable> moveables = new List<Moveable>();

void Update()
{
    //Some code before - with instantiation
    //It is best to track items by the script that you will use since you can just use moveables[i].gameObject to access the gameObject and moveables[i].transform to access transform
    moveables.Add(NextOnPath.GetComponent<Moveable>());
    //Some code after
}

1.您可能会节省adding the Moveable script to your Electron prefab的计算能力,因此您可以使用:第一个月
1.查找Singleton,您可以使用它轻松地为您的MoverController类赋值:

public static MoverController instance;
void Awake()
{
    if (instance == null)
    {
        instance = this;
    }
    else
    {
        Destroy(gameObject);
        return;
    }

    DontDestroyOnLoad(gameObject);
}

然后在Moveable类中,只需将其添加到MoverController

//Requires changing the above code

    void Update()
    {
        //Some code before - with instantiation
        MoverController.instance.targets.Add(NextOnPath.GetComponent<Moveable>());
        //Some code after
    }

相关问题