public override void OnInspectorGUI() { DrawDefaultInspector(); draggable = (GridDraggableObject)target; if (Application.isPlaying) { return; } draggable.UpdatePositionVars(); if (!mouseHook) { draggable.UpdatePosition(); } EditorUtility.SetDirty(draggable); }
void OnSceneGUI() { if (Application.isPlaying) { return; } Event evt = Event.current; if (evt.type == EventType.keyDown) { if (evt.keyCode == KeyCode.CapsLock) { draw = !draw; EditorUtility.SetDirty(target); } if (evt.keyCode == KeyCode.Space) { CorrectDraggablePosition(); } } if (draw) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); } if (!draw) { return; } if (drawObject == null || drawParent == null) { Debug.Log("Укажите что рисуем и где рисуем"); return; } if (evt.button != 0) { return; } switch (evt.type) { case EventType.mouseDrag: GridDraggableObject[] gridObjects = drawParent.GetComponentsInChildren <GridDraggableObject>(); if (gridObjects.Length <= 0) { Debug.Log("Поставьте хотябы 1 объект GridDraggableObject на сцену"); return; } GridDraggableObject draggable = gridObjects[0]; Vector2 mousePos = Event.current.mousePosition; mousePos.y = SceneView.GetAllSceneCameras()[0].pixelHeight - mousePos.y; Vector3 worldPosition = SceneView.GetAllSceneCameras()[0].ScreenPointToRay(mousePos).origin; Vector3 substractCoordinats = drawParent.localPosition; Transform parentTransform = drawParent.parent; while (parentTransform != null) { substractCoordinats += parentTransform.localPosition; parentTransform = parentTransform.parent; } GridObject.Position gridPosition = draggable.GetGridPosition(worldPosition - substractCoordinats); bool allowInstantiate = true; foreach (GridDraggableObject gridobject in gridObjects) { if (gridobject.position.Equals(gridPosition)) { allowInstantiate = false; break; } } if (allowInstantiate) { GridObject newGridObject = (GridObject)PrefabUtility.InstantiatePrefab(drawObject as GridObject); newGridObject.transform.parent = drawParent; newGridObject.position = gridPosition.Clone(); newGridObject.GetComponent <GridDraggableObject>().UpdatePosition(); newGridObject.GetComponent <SpriteRenderer>().color = color; EditorUtility.SetDirty(newGridObject); } break; default: break; } }