为什么在两个位置之间的场景视图中绘制线条时删除了线条?

时间:2017-10-25 00:38:01

标签: c# unity3d unity5

我要做的是在两个位置和一个立方体移动路线之间绘制线条而不删除线条。

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]

1 个答案:

答案 0 :(得分:1)

嗯,正如你自己所说的那样,行是绘制,而不是创建。

它们不是对象,并且不会出现&#34;持久直到删除&#34; 之类的行为。它们&#34;已删除&#34; 每一帧。

您可以使用第4个参数指定编辑器保持(重新)绘制的时间;但编辑负责每一帧重绘它们;他们仍然会在框架的末尾&#34;删除&#34;

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

duration 0表示仅为当前帧渲染线条。值0.5f表示将在下半秒内为所有帧重新渲染该行。