Unity Editor Scripting - 使用MouseOver获取gameObject

时间:2016-10-26 08:00:41

标签: c# unity3d editor mouseevent

我正在尝试创建自定义编辑器,但遇到了一些麻烦。 我在场景中有一个基础GO - http://prntscr.com/cz2f2r

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

1 个答案:

答案 0 :(得分:1)

答案结束了!

void OnEnable()
{
    SceneView.onSceneGUIDelegate += this.OnSceneMouseOver;
}


void OnSceneMouseOver(SceneView view)
 {
  Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
  RaycastHit hit;
  //And add switch Event.current.type for checking Mouse click and switch tiles
  if (Physics.Raycast (ray, out hit, 100f)) 
  {
   Debug.DrawRay (ray.origin, hit.transform.position, Color.blue, 5f);
   Debug.Log (hit.transform.name);
   Debug.Log (hit.transform.position);
  }
 }