LineRenderer.SetPositions

Unity · Версия 2020.3 LTS · Scripting · LineRenderer

LineRenderer.SetPositions

Объявление

public void SetPositions(Vector3[] positions);

public void SetPositions(NativeArray positions);

public void SetPositions(NativeSlice positions);

Параметры

positions Массив позиций для установки.

Описание

Устанавливает положение всех вершин в линии.

Этот метод предпочтительнее SetPosition при установке всех позиций, так как более эффективно установить все позиции с помощью одной команды, чем устанавливать каждую позицию по отдельности. Обратите внимание, что positionCount должен вызываться до SetPositions. Также SetPositions игнорирует точки с индексами за пределами positionCount.

Смотрите так же: positionCount property, SetPosition function.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // Creates a line renderer that follows a Sin() function
    // and animates it.

    public Color c1 = Color.yellow;
    public Color c2 = Color.red;
    public int lengthOfLineRenderer = 20;

    void Start()
    {
        LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
        lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
        lineRenderer.widthMultiplier = 0.2f;
        lineRenderer.positionCount = lengthOfLineRenderer;

        // A simple 2 color gradient with a fixed alpha of 1.0f.
        float alpha = 1.0f;
        Gradient gradient = new Gradient();
        gradient.SetKeys(
            new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) },
            new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
        );
        lineRenderer.colorGradient = gradient;
    }

    void Update()
    {
        LineRenderer lineRenderer = GetComponent<LineRenderer>();
        var points = new Vector3[lengthOfLineRenderer];
        var t = Time.time;
        for (int i = 0; i < lengthOfLineRenderer; i++)
        {
            points[i] = new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f);
        }
        lineRenderer.SetPositions(points);
    }
}

Обновлено 17 ноября 2026 г.