// Spawns selected object in a random position and parents it accordingly private GridCheck SpawnObject(GameObject objToSpawn, Transform parent) { GridCheck gridCheck = null; int attemptCount = 10; while (attemptCount > 0) // Limit the number of attempts so we don't hang { GameObject go = PrefabUtility.InstantiatePrefab(objToSpawn) as GameObject; if (go != null) { go.transform.position = lg.RandomPosition; go.transform.rotation = Quaternion.identity; GridCheck gc = go.GetComponent <GridCheck>(); go.transform.position = gc.GetPosition(GridManager.Grid); if (!gc.CanPlace()) // Checks if the object can be placed in the specified location, destroys if it can't { DestroyImmediate(go); attemptCount--; // Minus attempt count so we can break out if the object can't find a position continue; } gridCheck = gc; go.transform.SetParent(parent); go.name = objToSpawn.name; lg.SpawnedObjectsCount++; } break; } return(gridCheck); }
// Draws a dragable handle in the scene view and detect if it has moved private void DrawHandle(GridCheck g, GeneratedObject ge, Color color, int hIndex) { EditorGUI.BeginChangeCheck(); // Draw a separate handle to visualize that this object has been selected if (objectSelected && hIndex == selectedHandleIndex + 1) { Handles.color = Color.red; Handles.FreeMoveHandle(9999, g.transform.position, Quaternion.identity, 0.55f, Vector3.one, Handles.CircleHandleCap); } Handles.color = color; // Se the colour of the actual handle startPos = g.transform.position; // Set the starting position before the handle starts moving // Draw the actual handle and save it's position in a variable //Vector3 dPos = Handles.FreeMoveHandle(hIndex, g.transform.position, Quaternion.identity, 0.45f, Vector3.one, Handles.CircleHandleCap); Vector3 dPos = Handles.FreeMoveHandle(hIndex, g.transform.position, Quaternion.identity, 0.225f, Vector3.one, Handles.DotHandleCap); if (dPos != startPos) // Check if this handle has moved and set the scene to dirty if it has, { // Because even though it's moving a gameobject the scene can't detect a chance so it doesn't save... EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); } g.transform.position = dPos; // Set the new position have to set this twice because of the way obstacles are set up dPos = g.GetPosition(lg.grid); // Get the nearest grid position of the set position g.transform.position = dPos; // Set the new position on the grid ge.position = dPos; // Set the position for the generator to track and save EditorGUI.EndChangeCheck(); }