मेरे पास एक स्क्रिप्ट है जो एक कस्टम कंपाउंड मेष उत्पन्न करती है। लेकिन जब उपयोगकर्ता स्क्रिप्ट के गुणों को संपादित करता है जो कंपाउंड मेष उत्पन्न करता है तो मैं पिछले बनाए गए मेश को हटाना चाहता हूं। इसे पूरा करने के लिए मुझे स्क्रिप्ट का पालन करना होगा:

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

[ExecuteInEditMode]
public class BasicVoidVisualizationProperties : MonoBehaviour {

    public Vector3 Dimensions;
    public float Thickness;
    public float Spacing;

    private GameObject[] Corners = new GameObject[8];
    private EdgeBuilder[] Edges = new EdgeBuilder[12];

    // Use this for initialization
    void Start () {
        for (int i = 0; i < Corners.Length; i++)
        {
            Corners[i] = Instantiate(Resources.Load("cube") as GameObject);
            Corners[i].transform.SetParent(this.transform);
        }
        RebuildCorners(Corners, Dimensions, Thickness);
        for (int i = 0; i < Edges.Length; i++)
        {
            Edges[i] = new EdgeBuilder(this.transform);
        }
        RebuildEdges(Edges, Corners);
    }

    // Update is called once per frame
    void Update () {

    }

    private void RebuildCorners(GameObject[] Corners, Vector3 Dimensions, float Thickness)
    {
        Corners[0].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[0].transform.position = this.transform.position + new Vector3(Thickness/2, Thickness /2, Thickness/2);
        Corners[1].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[1].transform.position = this.transform.position + new Vector3(Dimensions.x - Thickness /2, Thickness /2, Thickness/2);
        Corners[2].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[2].transform.position = this.transform.position + new Vector3(Dimensions.x - Thickness /2, Thickness/2, Dimensions.z - Thickness /2);
        Corners[3].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[3].transform.position = this.transform.position + new Vector3(Thickness/2, Thickness/2, Dimensions.z - Thickness /2);
        Corners[4].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[4].transform.position = this.transform.position + new Vector3(Thickness/2, Dimensions.y - Thickness/2, Thickness/2);
        Corners[5].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[5].transform.position = this.transform.position + new Vector3(Dimensions.x - Thickness /2, Dimensions.y - Thickness/2, Thickness/2);
        Corners[6].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[6].transform.position = this.transform.position + new Vector3(Dimensions.x - Thickness /2, Dimensions.y - Thickness/2, Dimensions.z - Thickness /2);
        Corners[7].transform.localScale = new Vector3(Thickness, Thickness, Thickness);
        Corners[7].transform.position = this.transform.position + new Vector3(Thickness/2, Dimensions.y - Thickness/2, Dimensions.z - Thickness /2);
    }

    private void RebuildEdges(EdgeBuilder[] Edges, GameObject[] Corners)
    {
        Edges[0].RebuildEdge(Corners[0].transform.position, Corners[1].transform.position, Thickness, Spacing, Direction.right);
        Edges[1].RebuildEdge(Corners[3].transform.position, Corners[2].transform.position, Thickness, Spacing, Direction.right);
        Edges[2].RebuildEdge(Corners[4].transform.position, Corners[5].transform.position, Thickness, Spacing, Direction.right);
        Edges[3].RebuildEdge(Corners[7].transform.position, Corners[6].transform.position, Thickness, Spacing, Direction.right);

        Edges[4].RebuildEdge(Corners[0].transform.position, Corners[4].transform.position, Thickness, Spacing, Direction.up);
        Edges[5].RebuildEdge(Corners[1].transform.position, Corners[5].transform.position, Thickness, Spacing, Direction.up);
        Edges[6].RebuildEdge(Corners[2].transform.position, Corners[6].transform.position, Thickness, Spacing, Direction.up);
        Edges[7].RebuildEdge(Corners[3].transform.position, Corners[7].transform.position, Thickness, Spacing, Direction.up);

        Edges[8].RebuildEdge(Corners[0].transform.position, Corners[3].transform.position, Thickness, Spacing, Direction.forward);
        Edges[9].RebuildEdge(Corners[1].transform.position, Corners[2].transform.position, Thickness, Spacing, Direction.forward);
        Edges[10].RebuildEdge(Corners[4].transform.position, Corners[7].transform.position, Thickness, Spacing, Direction.forward);
        Edges[11].RebuildEdge(Corners[5].transform.position, Corners[6].transform.position, Thickness, Spacing, Direction.forward);
    }

    void OnValidate()
    {
        if(Corners[0] != null) RebuildCorners(Corners, Dimensions, Thickness);
        if(Corners[0] != null && Edges[0] != null)
        {
            foreach (var edge in Edges)
            {
                foreach (var eb in edge.EdgeBlocks)
                {
                    StartCoroutine(DestroyEdgeElement(eb));
                }
            }
            RebuildEdges(Edges, Corners);
        }
    }

    private IEnumerator DestroyEdgeElement(GameObject go)
    {
        yield return new WaitForEndOfFrame();
        Debug.Log("DESTROYED");           // IS NEVER CALLED (never logs to console)
        DestroyImmediate(go);
    }

    private class EdgeBuilder
    {
        private Transform Parent;
        public List<GameObject> EdgeBlocks = new List<GameObject>();

        public EdgeBuilder(Transform parent)
        {
            Parent = parent;
        }

        private List<GameObject> CalculateEdgeBlocks(Vector3 origin, Vector3 end, float thickness, float spacing)
        {
            List<GameObject> elements = new List<GameObject>();
            var dist = Vector3.Distance(origin, end) - thickness;
            var blocks = (int)Mathf.Round((dist - (thickness + spacing * 2)) / (thickness + spacing));
            for (int i = 0; i < blocks; i++)
            {
                var el = Instantiate(Resources.Load("cube") as GameObject);
                el.transform.localScale = new Vector3(thickness, thickness, thickness);
                el.transform.SetParent(Parent);
                elements.Add(el);
            }

            return elements;
        }

        public void RebuildEdge(Vector3 origin, Vector3 end, float thickness, float spacing, Direction dir)
        {
            // if(EdgeBlocks.Count > 0) ClearEdge(); 
            EdgeBlocks = CalculateEdgeBlocks(origin, end, thickness, spacing);

            if (dir == Direction.up)
            {
                var firstEl = new Vector3(origin.x, origin.y + thickness + spacing, origin.z);
                foreach (var el in EdgeBlocks)
                {
                    el.transform.position = firstEl;
                    firstEl += new Vector3(0, thickness + spacing, 0);
                }
            }
            else if(dir == Direction.right)
            {
                var firstEl = new Vector3(origin.x + thickness + spacing, origin.y, origin.z);
                foreach (var el in EdgeBlocks)
                {
                    el.transform.position = firstEl;
                    firstEl += new Vector3(thickness + spacing, 0, 0);
                }
            }
            else if(dir == Direction.forward)
            {
                var firstEl = new Vector3(origin.x, origin.y, origin.z + thickness + spacing);
                foreach (var el in EdgeBlocks)
                {
                    el.transform.position = firstEl;
                    firstEl += new Vector3(0, 0, thickness + spacing);
                }
            }
        }

        // private void ClearEdge()
        // {
        //  foreach (var eb in EdgeBlocks)
        //  {
        //      DestroyImmediate(eb, true);
        //  }
        //  EdgeBlocks.Clear();
        // }
    }

    private enum Direction
    {
        up,
        right,
        forward 
    }
}

मेरे पास जो समस्या है वह यह है कि Debug.log("Destroyed"); को कभी नहीं कहा जाता है (मैं Debug.log निष्पादित नहीं देखता)। ये क्यों हो रहा है?

आप एक कोरआउटिन का उपयोग क्यों कर सकते हैं जो आप पूछ सकते हैं, नीचे दिए गए लिंक को देखें कि मैं कोरआउटिन का उपयोग क्यों करता हूं: संपादक घटना से वस्तु को नष्ट करने के लिए कोरआउट

अगर अधिक जानकारी की आवश्यकता है तो मुझे बताएं ताकि मैं स्पष्ट कर सकूं!

0
FutureCake 24 नवम्बर 2018, 18:29

1 उत्तर

सबसे बढ़िया उत्तर

समस्या WaitForEndOfFrame से आती है। क्यों पर पूरी तरह से भरोसा नहीं है, लेकिन ऐसा लगता है कि यह ऑपरेशन संपादक मोड में रहते हुए वापस नहीं आता है, जैसा कि प्ले मोड में होने के विपरीत है।

यदि आप इसके बजाय yield return null का उपयोग करते हैं, तो इसे समान प्रभाव प्राप्त करना चाहिए।

निष्पादन आदेश चार्ट में किसी और के लिए उत्तर स्पष्ट हो सकता है: https://docs.unity3d.com/uploads/Main/monobehaviour_flowchart.svg

1
ryeMoss 24 नवम्बर 2018, 16:19