예제 #1
0
        public void OnSceneGUI(SceneView scnView)
        {
            /*
             * if (Event.current.type == EventType.Layout)
             * {
             *  HandleUtility.AddDefaultControl(0);
             * }
             */


            if (original != null)
            {
                Quaternion orientation = Quaternion.Euler(0, 0, 0);

                if (selectedAxis == 0)
                {
                    Handles.color = Color.red;
                    orientation   = Quaternion.Euler(0, 90, 0);
                }
                else if (selectedAxis == 1)
                {
                    Handles.color = Color.red;
                    orientation   = Quaternion.Euler(0, -90, 0);
                }
                if (selectedAxis == 2)
                {
                    Handles.color = Color.green;
                    orientation   = Quaternion.Euler(-90, 0, 0);
                }
                else if (selectedAxis == 3)
                {
                    Handles.color = Color.green;
                    orientation   = Quaternion.Euler(90, 0, 0);
                }
                else if (selectedAxis == 4)
                {
                    Handles.color = Color.blue;
                    orientation   = Quaternion.Euler(0, 0, 0);
                }
                else if (selectedAxis == 5)
                {
                    Handles.color = Color.blue;
                    orientation   = Quaternion.Euler(0, 180, 0);
                }

                // Handles.ArrowCap(0, original.transform.position, orientation,5);
            }

            /*
             * Event e = Event.current;
             *
             * //Check the event type and make sure it's left click.
             * if ((e.type == EventType.MouseDown) && e.button == 0)
             * {
             *  //cast a ray against mouse position
             *  Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
             *  RaycastHit hitInfo;
             *
             *  if (Physics.Raycast(worldRay, out hitInfo))
             *  {
             *      gizmoPosition = hitInfo.transform.position;
             *      Debug.Log(gizmoPosition);
             *      Handles.CubeCap(1,gizmoPosition, Quaternion.Euler(0, 0, 0),5);
             *      Selection.activeGameObject = hitInfo.collider.gameObject;
             *  }
             *
             *  e.Use();
             *
             * }
             */

            Event e = Event.current;

            if ((e.type == EventType.MouseUp) && e.button == 1 && e.shift)
            {
                toolbox = EditorWindow.GetWindow <Toolbox>();
                if (showPlacementGrid && toolbox != null)
                {
                    updateGrid(x_count, z_count, (int)distance, Toolbox.gizmoPosition);
                    drawGrid();
                }
                scnView.Repaint();
            }

            if (keepInSphere)
            {
                Handles.color = Color.red;
                drawGizmo(sphereRadius, spherePosition);
            }

            if (toolbox != null && showPlacementGrid)
            {
                drawGrid();
            }

            if (toolbox != null && limitHeight)
            {
                drawMaxHeight(Toolbox.gizmoPosition);
            }
        }
예제 #2
0
        private IEnumerator placeOnAxis(GameObject parent)
        {
            if (Selection.transforms.Length == 1 || prefabs.Count > 0)
            {
                List <GameObject> objects = new List <GameObject>();

                int xdir = 1;
                int ydir = 1;
                int zdir = 1;

                if (x_dir == 0)
                {
                    xdir = -1;
                }
                if (y_dir == 0)
                {
                    ydir = -1;
                }
                if (z_dir == 0)
                {
                    zdir = -1;
                }

                var xsize = distance;
                var ysize = distance;
                var zsize = distance;

                Vector3 current = Vector3.zero;

                for (var x = 0; x < (int)x_count; x++)
                {
                    for (var y = 0; y < (int)y_count; y++)
                    {
                        for (var z = 0; z < (int)z_count; z++)
                        {
                            SceneView.RepaintAll();

                            if (pickRandom)
                            {
                                int index = Random.Range(0, prefabs.Count);
                                original = prefabs[index];

                                toolbox = EditorWindow.GetWindow <Toolbox>();
                                if (toolbox != null)
                                {
                                    current = Toolbox.gizmoPosition;
                                }
                                else
                                {
                                    current = original.transform.position;
                                }
                            }
                            else
                            {
                                current = Selection.transforms[0].position;
                            }


                            float probability = Random.Range(0, 1f);

                            if (randomizePlacement && probability < targetProbability)
                            {
                                continue;
                            }

                            float randomDistX = distance;
                            float randomDistY = distance;
                            float randomDistZ = distance;

                            if (randomizePlacement)
                            {
                                randomDistX = Random.Range(0, randomDistance);
                                randomDistY = Random.Range(0, randomDistance);
                                randomDistZ = Random.Range(0, randomDistance);
                            }

                            float   _y     = current.y;
                            Vector3 normal = Vector3.zero;

                            if (useRay)
                            {
                                _y = getCurrentHeight((current.x - x * xdir * xsize) + randomDistX, (current.z - z * zdir * zsize) + randomDistZ, out normal);
                            }
                            else
                            {
                                _y = (current.y - y * ydir * ysize) + randomDistY;
                            }

                            Vector3 targetPos =
                                new Vector3(
                                    (current.x - x * xdir * xsize) + randomDistX,
                                    _y,
                                    (current.z - z * zdir * zsize) + randomDistZ);

                            float dist     = (spherePosition - targetPos).magnitude;
                            bool  inSphere = dist < sphereRadius;

                            if (!occupiedPlace(targetPos, parent.transform))
                            {
                                if (keepInSphere && !inSphere)
                                {
                                    continue;
                                }

                                if (limitHeight && targetPos.y > maxHeight)
                                {
                                    continue;
                                }

                                GameObject g = null;
                                g = Instantiate(original);

                                if (randomizePlacement)
                                {
                                    float scale = Random.Range(_minSize, _maxSize);
                                    g.transform.localScale = new Vector3(scale, scale, scale);
                                }

                                if (randomizeRotation)
                                {
                                    g.transform.Rotate(new Vector3(0, Random.Range(0, 90f), 0));
                                }
                                if (normalForRotation)
                                {
                                    g.transform.rotation = Quaternion.FromToRotation(g.transform.up, normal) * g.transform.rotation;
                                }

                                Undo.RegisterCreatedObjectUndo(g, g.name);
                                g.transform.position = targetPos;

                                objects.Add(g);

                                if (groupObjects && !combineObjects)
                                {
                                    g.transform.SetParent(parent.transform);
                                }

                                yield return(new WaitForSeconds(0.01f));
                            }
                        }
                    }
                }
                if (!groupObjects && combineObjects)
                {
                    ShapeFactory.combineObjects(objects.ToArray());

                    foreach (GameObject g in objects)
                    {
                        DestroyImmediate(g);
                    }

                    DestroyImmediate(parent);
                }
            }

            yield return(null);
        }