Skip to content

Spawn location editor #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions UOP1_Project/Assets/Prefabs/GameplayEssentials/SpawnSystem.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 2125786286893897154}
- component: {fileID: 4671891876261113940}
m_Layer: 0
m_Name: Location 01
m_TagString: Untagged
Expand All @@ -80,3 +81,16 @@ Transform:
m_Father: {fileID: 2125786285293829335}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &4671891876261113940
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2125786286893897213}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 14441bd075da9464e9c254e66e1849e3, type: 3}
m_Name:
m_EditorClassIdentifier:
_verticalOffset: 0.1
69 changes: 69 additions & 0 deletions UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using UnityEditor;
using UnityEngine;

[ExecuteInEditMode]
[AddComponentMenu("UOP1/Tools/Click to Place")]
public class ClickToPlaceHelper : MonoBehaviour
{
[Tooltip("Vertical offset above the clicked point. Useful to avoid spawn points to be directly ON the geometry which might cause issues.")]
[SerializeField] float _verticalOffset = 0.1f;

private Vector3 _spawnPosition;
private bool _displaySpawnPosition = false;

private delegate void ButtonAction();
private ButtonAction myButtonAction;

private void OnDrawGizmos()
{
if (_displaySpawnPosition)
{
Gizmos.color = Color.green;
Gizmos.DrawCube(_spawnPosition, Vector3.one * 0.3f);
}
}

void OnMouseClick(SceneView scene)
{
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.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
}
}
}

public void SetSpawnLocationAtCursor()
{
Debug.Log("Use the LMB to position this object");
myButtonAction = SetTransform;
_displaySpawnPosition = true;
SceneView.duringSceneGui += OnMouseClick;
}

/// <summary>
/// The delegate called when the mouse is clicked in the viewport
/// </summary>
private void SetTransform()
{
transform.position = _spawnPosition;
Debug.Log("Object moved to " + _spawnPosition);
}
}
11 changes: 11 additions & 0 deletions UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ClickToPlaceHelper))]
public class ClickToPlaceHelperEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

ClickToPlaceHelper myTarget = target as ClickToPlaceHelper;

if (GUILayout.Button("Place at Mouse cursor"))
{
myTarget.SetSpawnLocationAtCursor();
}
}
}
11 changes: 11 additions & 0 deletions UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.