浏览代码

fix: Project won't compile due to a reference to UnityEditor (#157) (#169)

* fix: Project won't compile due to a reference to UnityEditor (#157)

* #157 improved code readability
/main
GitHub 4 年前
当前提交
aaf14e64
共有 2 个文件被更改,包括 50 次插入51 次删除
  1. 62
      UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs
  2. 39
      UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs

62
UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs


using UnityEditor;
using UnityEngine;
using UnityEngine;
[ExecuteInEditMode]
[AddComponentMenu("UOP1/Tools/Click to Place")]

[SerializeField] float _verticalOffset = 0.1f;
[SerializeField] private float _verticalOffset = 0.1f;
private Vector3 _spawnPosition;
private bool _displaySpawnPosition = false;
private Vector3 _targetPosition;
private delegate void ButtonAction();
private ButtonAction myButtonAction;
public bool IsTargeting { get; private set; }
if (_displaySpawnPosition)
if (IsTargeting)
Gizmos.DrawCube(_spawnPosition, Vector3.one * 0.3f);
Gizmos.DrawCube(_targetPosition, Vector3.one * 0.3f);
void OnMouseClick(SceneView scene)
public void BeginTargeting()
Event currentGUIEvent = Event.current;
Vector3 mousePos = currentGUIEvent.mousePosition;
float pixelsPerPoint = EditorGUIUtility.pixelsPerPoint;
mousePos.y = scene.camera.pixelHeight - mousePos.y * pixelsPerPoint;
mousePos.x *= pixelsPerPoint;
Ray ray = scene.camera.ScreenPointToRay(mousePos);
if (Physics.Raycast(ray, out RaycastHit hit))
{
_spawnPosition = hit.point + Vector3.up * _verticalOffset;
if (currentGUIEvent.type == EventType.MouseMove)
{
HandleUtility.Repaint();
}
if (currentGUIEvent.type == EventType.MouseDown
&& currentGUIEvent.button == 0) // Wait for Left mouse button down
{
myButtonAction();
SceneView.duringSceneGui -= OnMouseClick;
_displaySpawnPosition = false;
currentGUIEvent.Use(); // This consumes the event, so that other controls/buttons won't be able to use it
}
}
IsTargeting = true;
_targetPosition = transform.position;
public void SetSpawnLocationAtCursor()
public void UpdateTargeting(Vector3 spawnPosition)
Debug.Log("Use the LMB to position this object");
myButtonAction = SetTransform;
_displaySpawnPosition = true;
SceneView.duringSceneGui += OnMouseClick;
_targetPosition = spawnPosition + Vector3.up * _verticalOffset;
/// <summary>
/// The delegate called when the mouse is clicked in the viewport
/// </summary>
private void SetTransform()
public void EndTargeting()
transform.position = _spawnPosition;
Debug.Log("Object moved to " + _spawnPosition);
IsTargeting = false;
transform.position = _targetPosition;
}
}

39
UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs


[CustomEditor(typeof(ClickToPlaceHelper))]
public class ClickToPlaceHelperEditor : Editor
{
private ClickToPlaceHelper _clickHelper => target as ClickToPlaceHelper;
ClickToPlaceHelper myTarget = target as ClickToPlaceHelper;
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);
}
if (GUILayout.Button("Place at Mouse cursor"))
switch (currentGUIEvent.type)
myTarget.SetSpawnLocationAtCursor();
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;
}
}
}
正在加载...
取消
保存