コード例 #1
0
        void LateUpdate()
        {
            if (m_TrailsStart == null || m_TrailsEnd == null)
            {
                //Debug.LogWarning("Either Start or End sink is Null");
                return;
            }

            float factorwidth  = Screen.width / CameraWidth;
            float factorheight = Screen.height / CameraHeight;

            Measurement <List <Vector2> > liststart = m_TrailsStart.getList();
            Measurement <List <Vector2> > listend   = m_TrailsEnd.getList();

            if (liststart == null || listend == null)
            {
                return;
            }
            if (liststart.time() != listend.time())
            {
                Debug.Log("mismatch:" + liststart.time() + " to " + listend.time());
                return;
            }

            IEnumerator <Vector2> enumstart = liststart.data().GetEnumerator();
            IEnumerator <Vector2> enumend   = listend.data().GetEnumerator();

            LineRenderer renderer;
            Vector3      position;

            int c = 0;


            while (enumstart.MoveNext() && enumend.MoveNext())
            {
                if (c < m_RendererObjects.Count)
                {
                    renderer = m_RendererObjects[c];
                }
                else
                {
                    GameObject obj = (GameObject)Instantiate(LineObject);
                    renderer = (LineRenderer)obj.GetComponent <LineRenderer>();
                    m_RendererObjects.Add(renderer);
                }

                /* just to be on the safe side... as there were some spurious null reference exceptions -.- */
                if (enumstart == null || enumend == null || Camera.main == null)
                {
                    Debug.Log("suff --------------------");
                    continue;
                }
                float zPosition = Camera.main.nearClipPlane;
                position = new Vector3(enumstart.Current.x * factorwidth, (CameraHeight - enumstart.Current.y) * factorheight, zPosition);
                renderer.SetPosition(0, Camera.main.ScreenToWorldPoint(position));

                position = new Vector3(enumend.Current.x * factorwidth, (CameraHeight - enumend.Current.y) * factorheight, zPosition);
                renderer.SetPosition(1, Camera.main.ScreenToWorldPoint(position));

                c++;
            }

            //Debug.Log("Number of trails:"+c);

            if (c < m_RendererObjects.Count)
            {
                for (int i = m_RendererObjects.Count - 1; i >= c; i--)
                {
                    Destroy(m_RendererObjects[i].gameObject);
                    m_RendererObjects.RemoveAt(i);
                }
            }
        }