浏览代码

Spawn location editor (#138)

* place spawn location with mouse

* commenting tooltip out

* Cleanup

Removed unused code, various renames, fixed usage of the event.Use() function, added AddComponentMenu attribut

Co-authored-by: Ciro Continisio <ciro@unity3d.com>
/main
GitHub 4 年前
当前提交
a2a278c8
共有 5 个文件被更改,包括 123 次插入0 次删除
  1. 14
      UOP1_Project/Assets/Prefabs/GameplayEssentials/SpawnSystem.prefab
  2. 69
      UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs
  3. 11
      UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs.meta
  4. 18
      UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs
  5. 11
      UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs.meta

14
UOP1_Project/Assets/Prefabs/GameplayEssentials/SpawnSystem.prefab


serializedVersion: 6
m_Component:
- component: {fileID: 2125786286893897154}
- component: {fileID: 4671891876261113940}
m_Layer: 0
m_Name: Location 01
m_TagString: Untagged

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
UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs


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
UOP1_Project/Assets/Scripts/ClickToPlaceHelper.cs.meta


fileFormatVersion: 2
guid: 14441bd075da9464e9c254e66e1849e3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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


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
UOP1_Project/Assets/Scripts/Editor/SpawnLocationEditor.cs.meta


fileFormatVersion: 2
guid: fa156b41e51cf024f91046b59f76a8cf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存