为什么在场景视图中两个位置之间的线被删除时画线?

Why when drawing line in the scene view between two positions the lines deleted?

我想做的是在不删除线的情况下在两个位置和立方体移动路线之间画线。

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

public class SpawnObjects : MonoBehaviour
{
    public int numberOfObjects;
    public GameObject objectToPlace;
    public Vector3 newObjectsSize = new Vector3(5, 5, 5);
    public float speed;

    private int wallsLengthX;
    private int wallsLengthZ;
    private int wallsPosX;
    private int wallsPosZ;
    private int currentObjects;
    private List<GameObject> objects = new List<GameObject>();

    void Start()
    {
        var wi = GetComponent<WallsTest>();
        wallsLengthX = (int)wi.lengthX;
        wallsLengthZ = (int)wi.lengthZ;
        wallsPosX = (int)wi.wallsStartPosition.x;
        wallsPosZ = (int)wi.wallsStartPosition.z;
    }
    // Update is called once per frame
    void Update()
    {
        Spawn();

        var randpos = GenerateRandomPositions(objects[0]);

        objects[0].transform.position = Vector3.Lerp(objects[0].transform.position,
randpos, (Mathf.Sin(speed * Time.deltaTime)));

        Debug.DrawLine(objects[0].transform.position, randpos, Color.red);
    }

    private void Spawn()
    {
        if (currentObjects != numberOfObjects)
        {
            if (objects.Count != 1) // Why did i make a check if not = 1 ?
            {
                GameObject newObject = (GameObject)Instantiate(objectToPlace);
                newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
                newObject.transform.localPosition = GenerateRandomPositions(newObject);
                newObject.name = "Spawned Object";
                newObject.tag = "Spawned Object";
                objects.Add(newObject);

                currentObjects += 1;
            }
        }
    }

    private Vector3 GenerateRandomPositions(GameObject newObject)
    {
        float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
        float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ) / 2f;
        float originX = wallsPosX + paddingX - wallsLengthX / 2f;
        float originZ = wallsPosZ + paddingZ - wallsLengthZ / 2f;
        float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX);
        float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ);
        float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));

        return new Vector3(posx, posy, posz);
    }
}

当我使用 Debug.DrawLine:

Debug.DrawLine(objects[0].transform.position, randpos, Color.red);

它正在画线,但随后将其删除。我怎样才能让它保持线条?

我怎样才能画一条绿色的线并保留它,这样它就不会删除显示立方体移动路线的线?立方体是对象[0]

好吧,正如您自己所说,线条是绘制的,而不是创建的。

它们不是对象,不会有"persisting until deleted"这样的行为。它们是 "deleted" 每一帧。

您可以指定编辑器保持(重新)绘制它们的时间,使用第 4 个参数;但那是编辑负责每一帧重新绘制它们;它们在帧的末尾仍将是 "deleted"

这里是完整的方法签名:public static void DrawLine(Vector3 start, Vector3 end, Color color = Color.white,float duration = 0.0f, bool depthTest = true);

A duration of 0 表示该行仅针对当前帧呈现。 0.5f 的值意味着将在接下来的半秒内为所有帧(重新)渲染该行。