Exemple #1
0
        private void GetPoints()
        {
            // find curved points in children
            // scan only the first hierarchy level to allow nested curved lines (like modelling a tree or a coral)
            List <CurvedLinePoint> curvedLinePoints = new List <CurvedLinePoint>();

            for (int i = 0; i < transform.childCount; i++)
            {
                CurvedLinePoint childPoint = transform.GetChild(i).GetComponent <CurvedLinePoint>();
                if (childPoint != null)
                {
                    curvedLinePoints.Add(childPoint);
                }
            }
            linePoints = curvedLinePoints.ToArray();

            //add positions
            if (linePositions.Length != linePoints.Length)
            {
                linePositions = new Vector3[linePoints.Length];
            }
            for (int i = 0; i < linePoints.Length; i++)
            {
                linePositions[i] = linePoints[i].transform.position;
            }
        }
        private static void AddControlPointCommand(MenuCommand menuCommand)
        {
            CurvedLinePoint linePoint    = menuCommand.context as CurvedLinePoint;
            CurvedLinePoint newLinePoint = InsertControlPoint(linePoint, false);

            Selection.activeGameObject = newLinePoint.gameObject;
        }
        private static bool ValidateInsertControlPointCommand(MenuCommand menuCommand)
        {
            CurvedLinePoint    linePoint  = menuCommand.context as CurvedLinePoint;
            CurvedLineRenderer curvedLine = linePoint.GetComponentInParent <CurvedLineRenderer>();

            return(curvedLine.LinePoints.Length > 1);
        }
        private static void AddLineControlPointCommand(MenuCommand menuCommand)
        {
            CurvedLineRenderer curvedLine   = menuCommand.context as CurvedLineRenderer;
            CurvedLinePoint    newLinePoint = AddControlPoint(curvedLine);

            Selection.activeGameObject = newLinePoint.gameObject;
        }
        private static CurvedLinePoint InsertControlPoint(CurvedLinePoint linePoint, bool before)
        {
            CurvedLineRenderer     curvedLine    = linePoint.GetComponentInParent <CurvedLineRenderer>();
            CurvedLinePoint        newLinePoint  = AddControlPoint(curvedLine);
            List <CurvedLinePoint> linePointList = new List <CurvedLinePoint>(curvedLine.LinePoints);
            int idx          = linePointList.IndexOf(linePoint);
            int siblingIndex = linePoint.transform.GetSiblingIndex();

            if (before)
            {
                CurvedLinePoint prevLinePoint = curvedLine.LinePoints[idx - 1];
                newLinePoint.transform.SetSiblingIndex(siblingIndex);
                linePoint.transform.SetSiblingIndex(siblingIndex + 1);
                // set the new point in the middle between the current and the prervious point
                newLinePoint.transform.position = (prevLinePoint.transform.position + linePoint.transform.position) * 0.5f;
            }
            else
            {
                if (curvedLine.LinePoints.Length > idx + 1)
                {
                    CurvedLinePoint nextLinePoint = curvedLine.LinePoints[idx + 1];
                    newLinePoint.transform.SetSiblingIndex(siblingIndex + 1);
                    // set the new point in the middle between the current and the next point
                    newLinePoint.transform.position = (nextLinePoint.transform.position + linePoint.transform.position) * 0.5f;
                }
            }
            curvedLine.Update();
            RenameControlPoints(curvedLine.LinePoints);

            return(newLinePoint);
        }
        private static void NewCurvedLineCommand(MenuCommand menuCommand)
        {
            GameObject newObj = new GameObject("CurvedLine");

            Undo.RegisterCreatedObjectUndo(newObj, "New curved line");
            GameObject selectedObject     = Selection.activeGameObject;
            Material   parentLineMaterial = null;

            if (selectedObject != null && selectedObject.GetComponent <CurvedLineRenderer>() == null)
            {
                newObj.transform.SetParent(selectedObject.transform, false);
                CurvedLinePoint parentLinePoint = selectedObject.GetComponent <CurvedLinePoint>();
                if (parentLinePoint != null)
                {
                    CurvedLineRenderer parentCurvedLine = parentLinePoint.GetComponentInParent <CurvedLineRenderer>();
                    if (parentCurvedLine != null)
                    {
                        parentLineMaterial = parentCurvedLine.GetComponent <LineRenderer>().sharedMaterial;
                    }
                }
            }
            CurvedLineRenderer curvedLine = newObj.AddComponent <CurvedLineRenderer>();

            Selection.activeGameObject = newObj;
            UnityEditorInternal.ComponentUtility.MoveComponentUp(curvedLine);
            AddControlPoint(curvedLine);
            AddControlPoint(curvedLine);
            if (parentLineMaterial != null)
            {
                newObj.GetComponent <LineRenderer>().sharedMaterial = parentLineMaterial;
            }
            curvedLine.Update();
        }