2016-10-26 16 views
1

私はカスタムエディタを作成しようとしていますが、何か問題がありました。 私はBase GO on sceneを持っています - http://prntscr.com/cz2f2rUnity Editorスクリプティング - MouseOverでgameObjectを取得

このGOは外部メソッドを呼び出し、シーンにグリッドを作成します。メソッドでは、フォーカスがあるときにいつでもEditorManager(gameObject)を取得できます。

シーンビューでマウスイベントを聴いて、カーソルの下にある別のオブジェクトを識別する方法はありますか?

私のコード:finded

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

が回答!

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); 
    } 
} 
関連する問題