簡體   English   中英

如何從已繪制的任何點/線中清除 linerenderer 並使用標志返回點和線?

[英]How can I clear a linerenderer from any points/lines that have beem drawn and return the points and lines back using a flag?

using UnityEngine;
using System.Collections;

[ExecuteAlways]

[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAroundTurret : MonoBehaviour
{
    [Range(1, 50)]
    public int segments = 50;
    [Range(1, 5)]
    public float xRadius = 5;
    [Range(1, 5)]
    public float yRadius = 5;
    [Range(0.1f, 5f)]
    public float width = 0.1f;
    LineRenderer line;
    public bool controlBothXradiusYradius = false;
    public bool draw = true;

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();

        line.enabled = true;
        line.positionCount = segments + 1;
        line.widthMultiplier = width;
        line.useWorldSpace = false;
        CreatePoints();
    }

    private void Update()
    {
        
    }

    public void CreatePoints()
    {
        line.widthMultiplier = width;

        float x;
        float y;
        float z;

        float angle = 20f;

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

            line.SetPosition(i, new Vector3(x, 0f, y));

            angle += (380f / segments);
        }
    }

#if UNITY_EDITOR
    float prevXRadius, prevYRadius;
    int prevSegments;
    float prevWidth;

    void OnValidate()
    {
        // Can't set up our line if the user hasn't connected it yet.
        if (line == null)
            return;

        if (draw == false)
        {
            line.positionCount = 0;
        }
        else
        {
            // If any values have changed, update our points.
            if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth)
            {
                CreatePoints();

                // Cache our most recently used values.
                prevXRadius = xRadius;
                prevYRadius = yRadius;
                prevSegments = segments;
                prevWidth = width;
            }

            if (controlBothXradiusYradius == true)
            {
                yRadius = xRadius;
            }
        }
    }
#endif
}

我正在使用標志繪制,如果它是錯誤的,它就像清理 linerenderer 一樣。 問題是,如果我將繪制標志設置回 true,它不會繪制任何東西,我希望它在將標志設置為 false 之前繪制已繪制的內容。

虛假的讓它喜歡清理,但當它是真實的時候,它就會收回原來的東西。

為什么不簡單地保持位置不變,而是禁用/啟用組件:

using UnityEngine;
using System.Collections;

[ExecuteAlways]
[RequireComponent(typeof(LineRenderer))]
public class DrawRadiusAroundTurret : MonoBehaviour
{
    [Range(1, 50)] public int segments = 50;
    [Range(1, 5)] public float xRadius = 5;
    [Range(1, 5)] public float yRadius = 5;
    [Range(0.1f, 5f)] public float width = 0.1f;
    public bool controlBothXradiusYradius = false;
    public bool draw = true;

    [SerilaizeField] private LineRenderer line;

    private void Start()
    {
        if(!line) line = GetComponent<LineRenderer>();

        CreatePoints();
    }

    //private void Update()
    //{
    //    
    //}

    public void CreatePoints()
    {
        line.enabled = true;
        line.widthMultiplier = width;
        line.useWorldSpace = false;
        line.widthMultiplier = width;
        line.positionCount = segments + 1;

        float x;
        float y;
        float z; // ?

        var angle = 20f;
        var points = new Vector3[segments + 1];
        for (int i = 0; i < segments + 1; i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

            points[i] = new Vector3(x, 0f, y);

            angle += (380f / segments);
        }

        // it's way more efficient to do this in one go!
        line.SetPositions(points);
    }

#if UNITY_EDITOR
    private float prevXRadius, prevYRadius;
    private int prevSegments;
    private float prevWidth;

    private void OnValidate()
    {
        // Can't set up our line if the user hasn't connected it yet.
        if(!line) line = GetComponent<LineRenderer>();
        if(!line) return;

        if(!draw)
        {
            // instead simply disable the component
            line.enabled = false;
        }
        else
        {
            // Otherwise re-enable the component
            // This will simply re-use the previously created points
            line.enabled = true;

            if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth)
            {
                CreatePoints();

                // Cache our most recently used values.
                prevXRadius = xRadius;
                prevYRadius = yRadius;
                prevSegments = segments;
                prevWidth = width;
            }

            if (controlBothXradiusYradius)
            {
                yRadius = xRadius;
            }
        }
    }
#endif
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM