Unity_画线_地面画框_类似汽车停车位_拖尾组件Trail Renderer_Demo

无聊做了个地面画框、画框的Demo,欢迎观赏哈哈

就是在地上画个圈23333

先来段视频

拜托点个赞就行 哈哈哈哈哈
B站传送点:https://www.bilibili.com/video/av69410313.

代码

按下空格开始划线,按下A键删除所有划线
// 控制生成点的脚本,挂载在摄像机上,脚本名字叫 N 哈
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class N : MonoBehaviour
{
    Vector3[] PointsLis;
    public List RList;
    public GameObject R;
    GameObject A;
    private void Start()
    {

        RList = new List();
        
    }
    void Update()
    {
         if (Input.GetKey(KeyCode.Space))
         {
            Cross_line1(new Vector3(Random.Range(2, 10), 0, Random.Range(2, 10)));
         }
        if (Input.GetKeyDown(KeyCode.A))
        {
            Erase();
        }
    }

    public void Cross_line1(Vector3 point)
    {
        if (GameObject.Find("Quans") != null)
        {
            PointsLis = new Vector3[4];
            GameObject temp = Instantiate(R, point, Quaternion.identity);
            RList.Add(temp);
            temp.transform.SetParent(A.transform);
            temp.transform.GetComponent().Cross_line2(Random.Range(2, 100), Random.Range(2, 100));
        }
        else
        {
            A = new GameObject();
            A.name = "Quans";
        }
        
    }

    public void Erase()
    {
        if (GameObject.Find("Quans") == null)
        {
            return;
        }
        else
        {
            Destroy(GameObject.Find("Quans"));
        }
    }
}

// 然后是生成的点的子物体按照随机的数值划线的脚本, 挂载在生成的物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HuaQuan : MonoBehaviour
{
    Vector3[] PointsLis;
    public Material Mtl;

    public void Cross_line2(int Width, int Height)
    {
        transform.localScale = new Vector3(Width, 0, Height);
        PointsLis = new Vector3[4];
        transform.Find("HH").GetComponent().material = Mtl;

        PointsLis[0] = transform.Find("HH").position;
        PointsLis[1] = new Vector3(PointsLis[0].x + Width, 0, PointsLis[0].z);
        PointsLis[2] = new Vector3(PointsLis[0].x + Width, 0, PointsLis[0].z - Height);
        PointsLis[3] = new Vector3(PointsLis[0].x, 0, PointsLis[0].z - Height);

        if (this.gameObject.activeSelf)
        {
            StartCoroutine("HH");
        }
        else
        {
            return;
        }
        
    }

    IEnumerator HH()
    {
        yield return new WaitForSeconds(0.01f);
        transform.Find("HH").GetComponent().emitting = true; 
        yield return new WaitForSeconds(0.01f);
        for (int i = 3; i > -1; i--)
        {
            transform.Find("HH").position = PointsLis[i];
            yield return new WaitForSeconds(0.01f);
        }
        transform.Find("HH").position = PointsLis[3];
    }

}

最后是百度云的连接,可以直接下载这个Demo,欢迎赏玩
链接:https://pan.baidu.com/s/1wn7ylVmWDAYS-UGxzNMIOw .
提取码:v2bp
新手上路,不足之处还望不吝赐教,不妥当的地方欢迎评价指出

你可能感兴趣的:(Unity_画线_地面画框_类似汽车停车位_拖尾组件Trail Renderer_Demo)