Example #1
0
        static ActionResult MenuBooleanOperation(BooleanOperation operation, ProBuilderMesh lhs, ProBuilderMesh rhs)
        {
            if (lhs == null || rhs == null)
            {
                return(new ActionResult(ActionResult.Status.Failure, "Must Select 2 Objects"));
            }

            string op_string = operation == BooleanOperation.Union ? "Union" : (operation == BooleanOperation.Subtract ? "Subtract" : "Intersect");

            ProBuilderMesh[] sel = new ProBuilderMesh[] { lhs, rhs };

            UndoUtility.RecordSelection(sel, op_string);

            Mesh c;

            switch (operation)
            {
            case BooleanOperation.Union:
                c = CSG.Union(lhs.gameObject, rhs.gameObject);
                break;

            case BooleanOperation.Subtract:
                c = CSG.Subtract(lhs.gameObject, rhs.gameObject);
                break;

            default:
                c = CSG.Intersect(lhs.gameObject, rhs.gameObject);
                break;
            }

            GameObject go = new GameObject();

            go.AddComponent <MeshRenderer>().sharedMaterial = EditorUtility.GetUserMaterial();
            go.AddComponent <MeshFilter>().sharedMesh       = c;

            ProBuilderMesh pb = InternalMeshUtility.CreateMeshWithTransform(go.transform, false);

            DestroyImmediate(go);

            Selection.objects = new Object[] { pb.gameObject };

            return(new ActionResult(ActionResult.Status.Success, op_string));
        }