OnDrawGizmos처럼 상시동작하는점이 비슷하지만
OnDrawGizmos는 기즈모 드로우 옵션이 켜져있을때에만 동작하므로
기능코드는 가급적 OnSceneGUI에 넣는것이 좋다
1. Static에서 FindObjectsOfType를 이용해 가져와서 처리하는 방식
gist : https://gist.github.com/ahzkwid/3294e9d4cf3980ae2d1c135123db145c
public class SampleClass : MonoBehaviour
{
static void OnSceneGUI(SceneView sceneView)
{
foreach (var sampleClass in FindObjectsOfType<SampleClass>())
{
if (sceneView.camera != null)
{
var sceneCamera = sceneView.camera;
//CameraCode
}
//otherCode
}
}
}
//OtherSample: https://gist.github.com/ahzkwid/6647d3aec625afe96c54bb38b2671c72
2. SceneView.duringSceneGui에서 이벤트를 받는 방식
원문링크 : https://forum.unity.com/threads/solved-how-to-start-draganddrop-action-so-that-sceneview-and-hierarchy-accept-it-like-with-prefabs.822531/
아래 코드는 내가 만든게 아니고 위 링크에서 가져와 간소화 한것일뿐이다
[ExecuteInEditMode]
public class SampleClass : MonoBehaviour
{
void OnEnable()
{
SceneView.duringSceneGui += OnSceneGUI;
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
void OnDisable()
{
SceneView.duringSceneGui -= OnSceneGUI;
EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyGUI;
}
void OnSceneGUI(SceneView obj)
{
HandleDragAndDropEvents();
}
void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
HandleDragAndDropEvents();
}
void HandleDragAndDropEvents()
{
if (Event.current.type == EventType.DragUpdated)
{
OnDragUpdated();
}
if (Event.current.type == EventType.DragPerform)
{
OnDragPerform();
}
}
void OnDragUpdated()
{
Debug.Log("OnDragUpdated()");
}
void OnDragPerform()
{
Debug.Log("OnDragPerform()");
}
}