示例#1
0
        protected virtual void OnSceneGUI()
        {
            SimplePatrolPath patrol = (SimplePatrolPath)target;

            if (patrol == null)
            {
                return;
            }

            EditorGUI.BeginChangeCheck();
            List <Vector3> list = new List <Vector3>();

            //draw handles
            for (int i = 0; i < patrol.Count; i++)
            {
                list.Add(Handles.PositionHandle(patrol[i], Quaternion.identity)); //position of points
                Handles.Label(list[i], i.ToString());                             //index of points
            }

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(patrol, "Change patrol path point position");
                patrol.points = list;
            }
        }
示例#2
0
        public static void GroundPatrolPath(SimplePatrolPath target, float height)
        {
            List <Vector3> list = new List <Vector3>();

            for (int i = 0; i < target.Count; i++)
            {
                RaycastHit hit;
                if (Physics.Raycast(target[i] + new Vector3(0, height, 0), Vector3.down, out hit, height * 2, target.mask.value))
                {
                    list.Add(hit.point);
                }
                else
                {
                    list.Add(target[i]);
                }
            }

            Undo.RecordObject(target, "Ground patrol path");
            target.points = list;
        }
示例#3
0
        public override void OnInspectorGUI()
        {
            SimplePatrolPath patrol = (SimplePatrolPath)target;

            if (patrol == null)
            {
                return;
            }

            //grounding points
            var serializedObject = new SerializedObject(patrol);
            var layersProperty   = serializedObject.FindProperty("mask");

            EditorGUILayout.PropertyField(layersProperty, true);
            if (GUILayout.Button("Set points grounded"))
            {
                GroundPatrolPath(patrol, groundedDistance);
            }
            groundedDistance = EditorGUILayout.FloatField("Max ground distance", groundedDistance);


            patrol.color = EditorGUILayout.ColorField(patrol.color);
            if (GUI.changed)
            {
                Undo.RecordObject(patrol, "Patrol serialization");
            }

            //adding points
            if (GUILayout.Button("Add Point"))
            {
                int            count     = patrol.Count;
                List <Vector3> newPoints = new List <Vector3>();

                switch (count)
                {
                case 0:    //if no points then add point where object are
                    newPoints.Add(patrol.transform.position);
                    break;

                case 1:    //if one point then add point next to it
                    newPoints.Add(patrol[0]);
                    newPoints.Add(patrol[0] + Vector3.right);
                    break;

                default:    //if more than one then add point in direction of last two points
                    for (int i = 0; i < count; i++)
                    {
                        newPoints.Add(patrol[i]);
                    }
                    newPoints.Add(patrol[count - 1] + (patrol[count - 1] - patrol[count - 2]).normalized);
                    break;
                }

                //serialize
                Undo.RecordObject(patrol, "Add point to patrol path");
                patrol.points = newPoints;
                //ground all points
                GroundPatrolPath(patrol, groundedDistance);
            }

            //removing points
            //using that cause i cant remove points in middle of iteration
            bool remove      = false;
            int  removeIndex = 0;

            for (int i = 0; i < patrol.Count; i++)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(string.Format("[{0}]", i));
                patrol[i] = EditorGUILayout.Vector3Field(string.Empty, patrol[i]);
                if (GUILayout.Button("Remove"))
                {
                    remove      = true;
                    removeIndex = i;
                }
                GUILayout.EndHorizontal();
            }

            if (remove)
            {
                List <Vector3> temp = new List <Vector3>(patrol.points);
                temp.RemoveAt(removeIndex);
                Undo.RecordObject(patrol, "Remove point at patrol path");
                patrol.points = temp;
            }
        }