Unity 使用LineRenderer做画线游戏

前段时间公司要求做一款画线游戏,经历了许多坑之后,最终确定使用LineRenderer来做,然后又是一系列的坑,好在最后还是做出来了,给大家分享下代码:

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

public class DrawLine : MonoBehaviour
{
    private GameObject clone;
    private LineRenderer line;
    private int i;
    public GameObject lineRenderPre;
    public Material renderMat;

    private PolygonCollider2D polygonCollider;
    private List points;

    private List linePoints;

    private List lines = new List();
    private List clones = new List();
    private List> linePoints_List = new List>();

    bool hasShot;

    public AutomaticLaunch automaticLaunch;

    public PhysicsMaterial2D lineMat;

    void Start()
    {
        Application.targetFrameRate = 200;

        hasShot = false;
        automaticLaunch = automaticLaunch.GetComponent();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            clone = (GameObject)Instantiate(lineRenderPre, lineRenderPre.transform.position, transform.rotation);

            line = clone.GetComponent();
            line.material = renderMat;
            line.startColor = Color.blue;
            line.endColor = Color.red;
            line.startWidth = 0.02f;
            line.endWidth = 0.02f;
            lines.Add(line);
            i = 0;
            polygonCollider = clone.AddComponent();
            points = new List();
            linePoints = new List();
        }
        if (Input.GetMouseButton(0))
        {
            i++;
            line.positionCount = i;
            line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5)));
            points.Add(new Vector2(line.GetPosition(i - 1).x, line.GetPosition(i - 1).y));
            linePoints.Add(line.GetPosition(i - 1));
        }
        if (Input.GetMouseButtonUp(0))
        {
            //将点位数X2-1,再将后续新加点位反向加入
            int a = points.Count;
            for (int i = 0; i < a; i+=2)
            {
                points.Add(points[a - (i + 1)]);
            }

            clones.Add(clone);
            linePoints_List.Add(linePoints);
            
            polygonCollider.points = points.ToArray();
            Rigidbody2D rig = clone.AddComponent();
            rig.mass = 1;
            rig.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
            
            if (!hasShot)
            {
                automaticLaunch.StartCreat();
                hasShot = true;
            }
        }
    }

    private void FixedUpdate()
    {
        if (line != null && clones.Count >= 1 && linePoints_List.Count >= 1 && lines.Count >= 1)
        {
            GameObject targetClone;
            List targetLinePoints;
            LineRenderer targetLine;
            for (int i = 0; i < clones.Count; i++)
            {
                targetClone = clones[i];
                targetLinePoints = linePoints_List[i];
                targetLine = lines[i];
                UpdateLinePosition(targetClone, targetLinePoints, targetLine);
            }
        }
    }

    void UpdateLinePosition(GameObject targetClone, List targetLinePoints, LineRenderer targetLine)
    {
        for (int j = 0; j < targetLinePoints.Count; j++)
        {
            Vector3 p = targetClone.transform.TransformPoint(targetLinePoints[j]);
            targetLine.SetPosition(j, p);
        }
    }
}

这个是2D的界面下的产物。

介绍一下坑,之前想过给LineRenderer添加EdgeCollision2D,但是最后确定这个碰撞器并不适用,因为最后画出的线段是要添加物理特性的,需要受到重力的影响,做到下坠,倒下等效果,但是EdgeCollision2D好像并没有去规划整个线段的重心,如果使用的话,会造成画出的线段掉下去之后立在地面上,看着相当诡异。

大概能分享就这些,第一次写,不喜勿喷!

你可能感兴趣的:(随笔,LineRenderer,画线,Unity)