版本: Unity 6 (6000.0)
语言英语
  • C#

LineRenderer.Simplify

建议更改

成功!

感谢您帮助我们改进 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实阅读用户提出的每项更改建议,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的更改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们改进 Unity 文档的质量。

关闭

取消

切换到手册

声明

public void Simplify(float tolerance);

参数

tolerance 此值用于评估哪些点应该从线中移除。较高的值会导致更简单的线(更少的点)。接近零的正值会导致几乎没有减少的线。零或更小的值没有任何效果。

描述

通过移除落在指定容差范围内的点,生成原始线的简化版本。

使用 LineUtility.Simplify 执行线简化。

此示例演示如何简化现有线

using UnityEngine;

// This example shows how to apply line simplification to a line that already contains points. [RequireComponent(typeof(LineRenderer))] public class SimpleExampleLineRenderer : MonoBehaviour { public float tolerance = 1; void Start() { var lineRenderer = GetComponent<LineRenderer>(); int pointsBefore = lineRenderer.positionCount; lineRenderer.Simplify(tolerance); Debug.Log("Line reduced from " + pointsBefore + " to " + lineRenderer.positionCount); } }

此示例生成一个正弦波形状的线,并提供一个 GUI 来定制线生成和简化参数。

using System.Collections.Generic;
using UnityEngine;

// This example creates a sine wave and then simplifies it using LineRenderer.Simplify. // The parameters can be adjusted through an in game GUI to allow for experimentation. [RequireComponent(typeof(LineRenderer))] public class LineRendererExample : MonoBehaviour { public int numberOfPoints = 1000; public float length = 50; public float waveHeight = 10; public float tolerance = 0.1f;

private LineRenderer lineRenderer; private List<Vector3> points = new List<Vector3>(); // Generated points before Simplify is used.

public void Start() { lineRenderer = GetComponent<LineRenderer>(); GeneratePoints(); }

// Generates the sine wave points. public void GeneratePoints() { points.Clear(); float halfWaveHeight = waveHeight * 0.5f; float step = length / numberOfPoints; for (int i = 0; i < numberOfPoints; ++i) { points.Add(new Vector3(i * step, Mathf.Sin(i * step) * halfWaveHeight, 0)); } lineRenderer.SetPositions(points.ToArray()); }

// Draw a simple GUI slider with a label. private static float GUISlider(string label, float value, float min, float max) { GUILayout.BeginHorizontal(GUILayout.Width(Screen.width / 2.0f)); GUILayout.Label(label + "(" + value + ") :", GUILayout.Width(150)); var val = GUILayout.HorizontalSlider(value, min, max); GUILayout.EndHorizontal(); return val; }

public void OnGUI() { GUILayout.Label("LineRenderer.Simplify", GUI.skin.box);

// We use GUI.changed to detect if a value was changed via the GUI, if it has we can then re-generate the points and simplify the line again. GUI.changed = false;

numberOfPoints = (int)GUISlider("Number of Points", numberOfPoints, 0, 1000); length = GUISlider("Length", length, 0, 100); waveHeight = GUISlider("Wave Height", waveHeight, 0, 100); if (GUI.changed) GeneratePoints();

tolerance = GUISlider("Simplify Tolerance", tolerance, 0, 2); if (GUI.changed) lineRenderer.Simplify(tolerance);

float percentRemoved = 100.0f - ((float)lineRenderer.positionCount / numberOfPoints) * 100.0f; if (tolerance > 0.0f) GUILayout.Label("Points after simplification: " + lineRenderer.positionCount + "(Removed " + percentRemoved.ToString("##.##") + "%)"); } }