1. 程式人生 > >Unity 使用LineRenderer做畫線遊戲

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<Vector2> points;

    private List<Vector3> linePoints;

    private List<LineRenderer> lines = new List<LineRenderer>();
    private List<GameObject> clones = new List<GameObject>();
    private List<List<Vector3>> linePoints_List = new List<List<Vector3>>();

    bool hasShot;

    public AutomaticLaunch automaticLaunch;

    public PhysicsMaterial2D lineMat;

    void Start()
    {
        Application.targetFrameRate = 200;

        hasShot = false;
        automaticLaunch = automaticLaunch.GetComponent<AutomaticLaunch>();
    }

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

            line = clone.GetComponent<LineRenderer>();
            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<PolygonCollider2D>();
            points = new List<Vector2>();
            linePoints = new List<Vector3>();
        }
        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<Rigidbody2D>();
            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<Vector3> 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<Vector3> 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好像並沒有去規劃整個線段的重心,如果使用的話,會造成畫出的線段掉下去之後立在地面上,看著相當詭異。

大概能分享就這些,第一次寫,不喜勿噴!