您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
51 行
1.3 KiB
51 行
1.3 KiB
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
[CustomEditor(typeof(ClickToPlaceHelper))]
|
|
public class ClickToPlaceHelperEditor : Editor
|
|
{
|
|
private ClickToPlaceHelper _clickHelper => target as ClickToPlaceHelper;
|
|
|
|
public override void OnInspectorGUI()
|
|
{
|
|
base.OnInspectorGUI();
|
|
|
|
if (GUILayout.Button("Place at Mouse cursor") && !_clickHelper.IsTargeting)
|
|
{
|
|
_clickHelper.BeginTargeting();
|
|
SceneView.duringSceneGui += DuringSceneGui;
|
|
}
|
|
}
|
|
|
|
private void DuringSceneGui(SceneView sceneView)
|
|
{
|
|
Event currentGUIEvent = Event.current;
|
|
|
|
Vector3 mousePos = currentGUIEvent.mousePosition;
|
|
float pixelsPerPoint = EditorGUIUtility.pixelsPerPoint;
|
|
mousePos.y = sceneView.camera.pixelHeight - mousePos.y * pixelsPerPoint;
|
|
mousePos.x *= pixelsPerPoint;
|
|
|
|
Ray ray = sceneView.camera.ScreenPointToRay(mousePos);
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit))
|
|
{
|
|
_clickHelper.UpdateTargeting(hit.point);
|
|
}
|
|
|
|
switch (currentGUIEvent.type)
|
|
{
|
|
case EventType.MouseMove:
|
|
HandleUtility.Repaint();
|
|
break;
|
|
case EventType.MouseDown:
|
|
if (currentGUIEvent.button == 0) // Wait for Left mouse button down
|
|
{
|
|
_clickHelper.EndTargeting();
|
|
SceneView.duringSceneGui -= DuringSceneGui;
|
|
currentGUIEvent.Use(); // This consumes the event, so that other controls/buttons won't be able to use it
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|