Пример #1
0
    /// <summary>
    /// 오브젝트 생성 입력 확인 및 오브젝트 생성
    /// </summary>
    private void CheckCreateObejct()
    {
        Event e = Event.current;

        // 마우스 좌표 계산
        var mousePosition = Event.current.mousePosition * EditorGUIUtility.pixelsPerPoint;

        mousePosition.y = Camera.current.pixelHeight - mousePosition.y;

        var Ray = Camera.current.ScreenPointToRay(mousePosition);

        if (Physics.Raycast(Ray, out RaycastHit hit))
        {
            if (e.type == EventType.MouseDown)
            {
                _grid.OnSetGuideObject(Vector3.zero, false);

                if (e.control)
                {
                    Vector3 margin = (Camera.current.transform.position - hit.point).normalized * 0.1f;
                    Vector3 hitPos = _grid.CalGridPosition(hit.point + margin);

                    // Obejct 생성
                    if (e.button == 0)
                    {
                        if (_grid._curSelectIndex >= 0 &&
                            _grid._curSelectIndex < _grid._cachedObjects.Count &&
                            _grid._cachedObjects[_grid._curSelectIndex] != null)
                        {
                            _grid.InstantiateObject(hitPos, _grid._cachedObjects[_grid._curSelectIndex]);
                        }
                    }
                    // Obejct 제거
                    else if (e.button == 1)
                    {
                        _grid.DestroyObject(hit.transform.gameObject);
                    }

                    // Hierachy Focus를 Grid로 유지
                    int crtID = GUIUtility.GetControlID(FocusType.Passive);
                    GUIUtility.hotControl = crtID;

                    // Event 사용 처리
                    Event.current.Use();
                }
            }
            else if (e.type == EventType.MouseMove)
            {
                Vector3 margin = (Camera.current.transform.position - hit.point).normalized * 0.1f;
                Vector3 hitPos = _grid.CalGridPosition(hit.point + margin);

                _grid.OnSetGuideObject(hitPos);
            }
        }
        else
        {
            _grid.OnSetGuideObject(Vector3.zero, false);
        }
    }