Ejemplo n.º 1
0
    public static CubeGrid ToGrid(string path)
    {
        CubeGridXML XMLObject = LoadFromFile(path);
        CubeGrid    _grid     = ScriptableObject.CreateInstance <CubeGrid>();

        _grid.m_CubeLibrary = AssetDatabase.LoadAssetAtPath(XMLObject.CubeLibraryPath, typeof(CubeLibrary)) as CubeLibrary;
        _grid.gridSize      = XMLObject.CubeSize;

        CubeLibrary _lib = _grid.m_CubeLibrary;

        KeyValuePair <string, GameObject>[] LibraryList = _lib.GetObjectsAndGuids();
        List <StructNeedTarget>             NeedTarget  = new List <StructNeedTarget>();


        foreach (SerializableXMLElement iterator in XMLObject.Elements)
        {
            GameObject NewCube = null;
            string     NewGuid = "";
            int        index   = 0;
            foreach (KeyValuePair <string, GameObject> _pairiterator in LibraryList)
            {
                if (_pairiterator.Key == iterator._guid)
                {
                    NewCube = _lib.GetGameObjectByIndex(index);
                    NewGuid = iterator._guid;
                    break;
                }
                index++;
            }

            GameObject CreatedCube = null;
            if (NewCube == null)
            {
                continue;
            }

            _grid.currentPrefab     = NewCube;
            _grid.currentPrefabGuid = NewGuid;
            _grid.CreateCubeAt(iterator._position.V3, out CreatedCube);

            CreatedCube.transform.forward = iterator._forward.V3;
            CreatedCube.transform.up      = iterator._up.V3;
            CreatedCube.transform.right   = iterator._right.V3;

            if (iterator._usetarget)
            {
                StructNeedTarget CurrentTarget = new StructNeedTarget();
                CurrentTarget.SetArgs(CreatedCube, iterator._target.V3);
                NeedTarget.Add(CurrentTarget);
            }
        }

        // пройдемся и проставим таргет по позиции
        foreach (StructNeedTarget targetObj in NeedTarget.ToArray())
        {
            targetObj.comp.SetTarget(_grid.GetCubeAt(targetObj.targetPos));
        }

        return(_grid);
    }
Ejemplo n.º 2
0
    static void PickCubeLibrary()
    {
        // Попытаемся открыть закрепленную за сеткой библиотеку.
        if (m_LibraryPath.Length == 0)
        {
            m_LibraryPath = EditorUtility.SaveFilePanelInProject("Select library path", "New library", "asset", "Select a file", m_LibraryPath);
        }

        if (m_LibraryPath.Length != 0)
        {
            int answer = EditorUtility.DisplayDialogComplex("Error", "Cant open library asset. Create new base?", "Yes", "Cancel", "Pick another");
            switch (answer)
            {
            case 0: {
                m_Library = ScriptableObject.CreateInstance <CubeLibrary>();
                AssetDatabase.CreateAsset(m_Library, m_LibraryPath);
                break;
            }

            case 2: {
                m_LibraryPath = "";
                PickCubeLibrary(); break;
            }
            }
        }
    }
Ejemplo n.º 3
0
    void Awake()
    {
        _editor       = GlobalOptions.GetEditorComponent();
        _library      = _editor.Library;
        _cubeList     = _library.GetList();
        _previewArray = _library.GetImageList();

        RebuildMenu();
    }
Ejemplo n.º 4
0
    public override void OnInspectorGUI()
    {
        CubeLibrary TargetLibrary = (CubeLibrary)target;
        List <Cube> CubeList      = TargetLibrary.GetList();

        DrawDefaultInspector();

        if (GUILayout.Button("Add"))
        {
            //Cube obj = ScriptableObject.CreateInstance<Cube>();
            Cube obj = new Cube();
            CubeList.Add(obj);
        }

        if (GUILayout.Button("Clear"))
        {
            CubeList.Clear();
        }
    }
Ejemplo n.º 5
0
 void Start()
 {
     _grid     = GlobalOptions.Grid;
     _library  = _grid.m_CubeLibrary;
     _gridSize = _grid.gridSize;
 }
Ejemplo n.º 6
0
    void OnSceneGUI(SceneView _view)
    {
        // нет выделенного объекта - нет инспектора.
        if (!target)
        {
            return;
        }

        // Получаем указатель на выделенныйобъект.
        m_Instance = ((CubeGrid)target);

        if (!m_Instance)
        {
            return;
        }
        if (m_Instance.m_CubeLibrary == null)
        {
            return;
        }
        ;

        m_Library = m_Instance.m_CubeLibrary;

        if (!m_Instance.currentPrefab)
        {
            //Попытаемся получить первый куб из библиотеки, если он не выбран.
            m_Instance.currentPrefab = m_Library.GetGameObjectByIndex(0);
            if (!m_Instance.currentPrefab)
            {
                // Получить не удалось, в библиотеке нет элементов.
                return;
            }
        }
        ;

        if (processGUIMenus(_view))
        {
            return;
        }

        float blockSize = m_Instance.gridSize;
        //float halfBlockSize = blockSize / 2.0f;

        Quaternion noRotation = Quaternion.Euler(0, 0, 0);

        if (Event.current.type == EventType.layout)
        {
            if (!m_marker)
            {
                // Создаем маркер
                GameObject blockInst = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Tiles/pre_Cursor.prefab");

                blockInst.transform.localScale = new Vector3(blockSize, blockSize, blockSize);

                m_marker           = Instantiate(blockInst, intersectionPoint, noRotation) as GameObject;
                m_marker.name      = "MARKER";
                m_marker.hideFlags = HideFlags.HideAndDontSave | HideFlags.NotEditable | HideFlags.HideInInspector | HideFlags.HideInHierarchy;
                DestroyImmediate(m_marker.GetComponent <BoxCollider>());
                meshRenderer     = m_marker.GetComponent <MeshRenderer>();
                m_markerMaterial = new Material(meshRenderer.sharedMaterial);

                m_markerMaterial.color      = Color.Lerp(Color.green, meshRenderer.sharedMaterial.color, 0.85f);
                meshRenderer.sharedMaterial = m_markerMaterial;
            }

            // Запрещаем выделение других объектов.
            HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
        }

        // Обрабатываем события редактора.
        switch (Event.current.type)
        {
            #region MouseMove
        case EventType.mouseMove:
        {
            m_currentObject = null;

            switch (m_editMode)
            {
            case EditMode.addBlock:
            case EditMode.delBlock: {
                RaycastHit hitInfo = new RaycastHit();

                Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

                if (Physics.Raycast(ray, out hitInfo))
                {
                    if (m_editMode == EditMode.addBlock)
                    {
                        Vector3 pointOnCollider = hitInfo.transform.position + hitInfo.normal * blockSize;
                        intersectionPoint = pointOnCollider;
                    }
                    else
                    {
                        m_currentObject   = hitInfo.transform.gameObject;
                        intersectionPoint = hitInfo.transform.position;
                    }

                    intersectionPoint.x = intersectionPoint.x - intersectionPoint.x % blockSize;
                    intersectionPoint.y = intersectionPoint.y - intersectionPoint.y % blockSize;
                    intersectionPoint.z = intersectionPoint.z - intersectionPoint.z % blockSize;
                }
                else
                {
                    Plane hPlane   = new Plane(Vector3.up, Vector3.zero);
                    float distance = 0;

                    if (hPlane.Raycast(ray, out distance))
                    {
                        // make this local
                        pointOnXY = ray.GetPoint(distance);

                        intersectionPoint = new Vector3(pointOnXY.x, pointOnXY.y, pointOnXY.z);

                        intersectionPoint.x = intersectionPoint.x - intersectionPoint.x % blockSize;
                        intersectionPoint.y = intersectionPoint.y - intersectionPoint.y % blockSize;
                        intersectionPoint.z = intersectionPoint.z - intersectionPoint.z % blockSize;

                        if (Physics.Raycast(pointOnXY, intersectionPoint, out hitInfo, Vector3.Distance(pointOnXY, intersectionPoint)))
                        {
                            intersectionPoint = hitInfo.point;

                            intersectionPoint.x = intersectionPoint.x - intersectionPoint.x % blockSize;
                            intersectionPoint.y = intersectionPoint.y - intersectionPoint.y % blockSize;
                            intersectionPoint.z = intersectionPoint.z - intersectionPoint.z % blockSize;
                        }
                    }
                }

                Event.current.Use();
                break;
            }
            }
            break;
        }
            #endregion

            #region MouseUp
        case EventType.mouseUp: {
            if (Event.current.button == 0)
            {
                if (!(m_editMode == EditMode.addBlock ||
                      m_editMode == EditMode.delBlock))
                {
                    break;
                }

                switch (m_editMode)
                {
                case EditMode.addBlock: {
                    GameObject CreatedCube;
                    m_Instance.CreateCubeAt(intersectionPoint, out CreatedCube);

                    Event.current.Use(); break;
                }

                case EditMode.delBlock: {
                    if (m_currentObject)
                    {
                        m_Instance.DeleteCubeAt(m_currentObject.transform.position);
                    }

                    Event.current.Use(); break;
                }
                }
            }

            break;
        }
            #endregion

            #region KeyUp
        case EventType.keyUp:
        {
            switch (Event.current.keyCode)
            {
            case KeyCode.Escape: {
                DeselectThisGrid();

                Event.current.Use();
                break;
            }

            case KeyCode.Space: {
                SwitchState();

                Event.current.Use();
                break;
            }

            default: break;
            }


            break;
        }

        default: break;
        }
        #endregion
        DrawBlockMarker(blockSize, noRotation);
    }