1
我想創建一個自定義編輯器,但遇到了一些麻煩。 我對場景中的基礎GO - Unity編輯器腳本 - 使用MouseOver獲取遊戲對象
這GO調用外部方法,並創建場景的網格。在方法OnSceneGUI()
我總是可以獲得EditorManager(gameObject),當它在焦點。
如何在場景視圖上偵聽鼠標事件以識別光標下的不同對象?
我的代碼:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(GridManager))]
[CanEditMultipleObjects]
public class CIGridManager : Editor
{
private Vector2 _gridSize = new Vector2 (10, 10);
private GridManager _gridManager;
private bool _gridConfigureOpen = false;
private bool _gridCustomizerOpen = false;
private Color _selectedColor;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
_gridManager = target as GridManager;
if (!_gridManager.initialized())
{
EditorGUILayout.HelpBox ("Grid Manager is not Initialized. Please click init and go construct!", MessageType.Error);
if (GUILayout.Button ("Init Grid Manager"))
{
_gridManager.initialize (_gridManager.GetComponent<GridViewGenerator>(), _gridManager.GetComponent<GridSettingsStorage>());
}
}
else
{
_gridConfigureOpen = EditorGUILayout.Foldout (_gridConfigureOpen, "Grid Configuration Block");
_gridCustomizerOpen = EditorGUILayout.Foldout (_gridCustomizerOpen, "Grid Customize Block");
if (_gridConfigureOpen)
{
EditorGUILayout.LabelField("Grid Settings", EditorStyles.centeredGreyMiniLabel);
_gridSize = EditorGUILayout.Vector2Field("GridSize", _gridSize);
if(GUILayout.Button("Generate Grid"))
{
if (_gridSize.x > 0 && _gridSize.y > 0)
{
_gridManager.getGridSettingsStorage().setLayerSize (_gridSize);
_gridManager.generateGridLayer();
SceneView.RepaintAll();
}
else
{
}
}
if (GUILayout.Button ("Destroy All"))
{
_gridManager.destroyChank();
}
}
if (_gridCustomizerOpen)
{
EditorGUILayout.LabelField("Grid Customize", EditorStyles.centeredGreyMiniLabel);
_selectedColor = EditorGUILayout.ColorField (_selectedColor);
}
}
}
void OnSceneGUI()
{
//Problem!
// How to listen to mouse events on scene view to identify defferents object under the cursor? not only the EditorManager
}
}