这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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;
}
}
}