Пример #1
0
        private void OnInspectorGUI_Collider()
        {
            using (var check = new EditorGUIBeginChangeCheckScope())
            {
                EditorGUILayout.Space();

                using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
                {
                    EditorGUILayout.LabelField("tileMapColliderType:", EditorStyles.boldLabel);
                    using (new EditorGUIIndentLevelScope(2))
                    {
                        SerializedProperty tileMapColliderType_property = serializedObject.FindProperty("tileMapColliderType");
                        string[]           tileMapColliderType_names    = new List <string>(Enum.GetNames(typeof(TileMapColliderType)).Select(x => x.Replace('_', ' '))).ToArray();
                        tileMapColliderType_property.intValue = GUILayout.Toolbar(tileMapColliderType_property.intValue, tileMapColliderType_names);
                    }
                    EditorGUILayout.Space();
                }

                EditorGUILayout.Space();
                if (tileMap.tileMapColliderType == TileMapColliderType._3D)
                {
                    SerializedProperty collider_depth_property = serializedObject.FindProperty("collider_depth");
                    EditorGUILayout.PropertyField(collider_depth_property);
                    collider_depth_property.floatValue = Mathf.Clamp(collider_depth_property.floatValue, Vector3.kEpsilon, Mathf.Max(collider_depth_property.floatValue));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("physicMaterial"));
                }
                else if (tileMap.tileMapColliderType == TileMapColliderType._2D)
                {
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("tileMap2DColliderType"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("is_show_collider_normals"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("physicsMaterial2D"));
                }

                EditorGUILayout.PropertyField(serializedObject.FindProperty("is_trigger"));
                EditorGUILayout.PropertyField(serializedObject.FindProperty("tileMapColliderDisplayMode"));

                if (tileMap.is_trigger)
                {
                    EditorGUILayout.HelpBox("Activating IsTrigger could generate wrong collider lines if not used properly. Use Show Tilechunks in Map section to display the real collider lines.", MessageType.Warning);
                }
                else if (tileMap.tileMapColliderType == TileMapColliderType._2D && tileMap.tileMap2DColliderType == TileMap2DColliderType.PolygonCollider2D)
                {
                    EditorGUILayout.HelpBox("Using Polygon colliders could generate wrong collider lines if not used properly. Use Show Tilechunks in Map section to display the real collider lines.", MessageType.Warning);
                }

                if (check.IsChanged)
                {
                    serializedObject.ApplyModifiedProperties();
                    tileMap.Refresh(false, true);
                }
            }

            EditorGUILayout.Space();

            if (GUILayout.Button("Update Collider Mesh"))
            {
                tileMap.Refresh(false, true);
            }
        }
Пример #2
0
        public void DrawLeftTop()
        {
            using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar))
            {
                using (var check1 = new EditorGUIBeginChangeCheckScope())
                {
                    GUILayout.Toggle(_isPlaying, EditorIconGUIContent.GetSystem(EditorIconGUIContentType.PlayButton),
                                     EditorStyles.toolbarButton);            //Play
                    if (check1.IsChanged)
                    {
                        SwitchPlay();
                    }
                }


                using (var check3 = new EditorGUIBeginChangeCheckScope())
                {
                    GUILayout.Toggle(AnimationMode.InAnimationMode(),
                                     EditorIconGUIContent.GetSystem(EditorIconGUIContentType.Animation_Record),
                                     EditorStyles.toolbarButton);            //Animate
                    if (check3.IsChanged)
                    {
                        if (AnimationMode.InAnimationMode())
                        {
                            AnimationMode.StopAnimationMode();
                        }
                        else
                        {
                            AnimationMode.StartAnimationMode();
                        }
                    }
                }

                using (new EditorGUIUtilityLabelWidthScope(30))
                {
                    _frameCountPerSecond =
                        EditorGUILayout.IntField("FPS", _frameCountPerSecond, GUILayout.Width(30 + 25));
                    if (_frameCountPerSecond <= 1)
                    {
                        _frameCountPerSecond = 1;
                    }
                }

                using (new EditorGUIUtilityLabelWidthScope(70))
                {
                    _playSpeed = EditorGUILayout.FloatField("play_speed", _playSpeed, GUILayout.Width(70 + 25));
                    if (_playSpeed <= 0)
                    {
                        _playSpeed = 1;
                    }
                }

                GUILayout.FlexibleSpace();
            }
        }
        public TileView DrawTilePaletteSetting()
        {
            //选中的是那个tileView
            List <string> tileView_name_list = new List <string>()
            {
                "(All)"
            };

            tileView_name_list.AddRange(tileSet.tileView_list.Select(x => x.name));
            string[] tileView_names  = tileView_name_list.ToArray();
            int[]    tileView_values = Enumerable.Range(-1, tileSet.tileView_list.Count + 1).ToArray();
            using (var check = new EditorGUIBeginChangeCheckScope())
            {
                sharedTileSetData.tileView_reorderableList.index = EditorGUILayout.IntPopup("TileSet View", sharedTileSetData.tileView_reorderableList.index, tileView_names, tileView_values);
                if (check.IsChanged)
                {
                    RemoveTileSelection();
                }
            }


            //column_tile_length
            tileView = sharedTileSetData.tileView_reorderableList != null && sharedTileSetData.tileView_reorderableList.index >= 0 ? tileSet.tileView_list[sharedTileSetData.tileView_reorderableList.index] : null;
            if (tileView == null)
            {
                tileSet.column_tile_count_in_palette = Mathf.Clamp(EditorGUILayout.IntField("column_tile_count_in_palette", tileSet.column_tile_count_in_palette), 1, tileSet.column_tile_count);
            }

            // Draw Background Color Selector
            //    tileSet.background_color = EditorGUILayout.ColorField("Background Color", tileSet.background_color);
            //    if (prev_background_color != tileSet.background_color || GUIStyleConst.Scroll_Style.normal.background == null)
            //    {
            //      prev_background_color = tileSet.background_color;
            //      if (GUIStyleConst.Scroll_Style.normal.background == null)
            //        GUIStyleConst.Scroll_Style.normal.background = new Texture2D(1, 1) { hideFlags = HideFlags.DontSave };
            //      GUIStyleConst.Scroll_Style.normal.background.SetPixel(0, 0, tileSet.background_color);
            //      GUIStyleConst.Scroll_Style.normal.background.Apply();
            //    }


            //tile palette中tile的缩放比例
            using (new EditorGUILayoutBeginHorizontalScope())
            {
                GUILayout.Label(EditorGUIUtility.FindTexture("ViewToolZoom"), GUILayout.Width(35f));
                float visual_tile_zoom = EditorGUILayout.Slider(tileSet.visual_tile_size.x / tileSet.tile_pixel_size.x, 0.25f, 4f);
                tileSet.visual_tile_size = visual_tile_zoom * tileSet.tile_pixel_size;
                if (GUILayout.Button("重置", GUILayout.Width(50f)))
                {
                    tileSet.visual_tile_size = new Vector2(32f * tileSet.tile_pixel_size.x / tileSet.tile_pixel_size.y, 32f);
                }
            }

            return(tileView);
        }
Пример #4
0
        public static int DoTileSetBrushGroupFieldLayout(TileSet tileSet, string label, int group_index)
        {
            string tileSetBrush_group_names = tileSet.tileSetBrush_group_names[group_index];

            string[] tileSetBrushGroup_list = tileSet.tileSetBrush_group_names.Where(x => !string.IsNullOrEmpty(x)).ToArray();
            using (var check = new EditorGUIBeginChangeCheckScope())
            {
                int index = EditorGUILayout.Popup(label,
                                                  ArrayUtility.FindIndex(tileSetBrushGroup_list, x => x == tileSetBrush_group_names), tileSetBrushGroup_list);
                if (check.IsChanged)
                {
                    return(ArrayUtility.FindIndex(tileSet.tileSetBrush_group_names, x => x == tileSetBrushGroup_list[index]));
                }
            }

            return(group_index);
        }
Пример #5
0
        public void DrawTextureSetting()
        {
            using (var change_scope = new EditorGUIBeginChangeCheckScope())
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("atlas_texture"));
                if (change_scope.IsChanged)
                {
                    serializedObject.ApplyModifiedProperties();
                    target.UpdateTileSetConfig();
                }
            }

            if (target.atlas_texture == null)
            {
                EditorGUILayout.HelpBox("为tileset选择一张texture", MessageType.Info);
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("tile_pixel_size"));
                if (GUILayout.Button(new GUIContent("优化TextureImport Settings")))
                {
                    OptimizeTextureImportSettings(target.atlas_texture);
                }
                using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
                {
                    target.tile_pixel_size =
                        _GetPositiveIntVector2(EditorGUILayout.Vector2Field("Tile Size (pixels)", target.tile_pixel_size),
                                               Vector2.one);
                    target.slice_offset =
                        _GetPositiveIntVector2(EditorGUILayout.Vector2Field(new GUIContent("Offset"), target.slice_offset));
                    target.slice_padding =
                        _GetPositiveIntVector2(EditorGUILayout.Vector2Field(new GUIContent("Padding"), target.slice_padding));
                    if (GUILayout.Button("Slice Atlas"))
                    {
                        target.Slice();
                    }
                }
            }
        }
Пример #6
0
        private void OnInspectorGUI_Map()
        {
            EditorGUILayout.Space();

            if (GUILayout.Button("Refresh Map", GUILayout.MaxWidth(125)))
            {
                tileMap.Refresh(true, true, true, true);
            }
            if (GUILayout.Button("Clear Map", GUILayout.MaxWidth(125)))
            {
                if (EditorUtility.DisplayDialog("Warning!", "Are you sure you want to clear the map?\nThis action will remove all children objects under the tilemap", "Yes", "No"))
                {
                    Undo.RegisterFullObjectHierarchyUndo(tileMap.gameObject, "Clear Map " + tileMap.name);
                    tileMap.is_undo_enabled = true;
                    tileMap.ClearMap();
                    tileMap.is_undo_enabled = false;
                }
            }

            using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
            {
                using (new EditorGUILayoutBeginHorizontalScope())
                {
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("cell_size"));
                    if (GUILayout.Button("R", GUILayout.Width(20)))
                    {
                        serializedObject.FindProperty("cell_size").vector2Value = tileMap.tileSet.tile_pixel_size / tileMap.tileSet.pixels_per_unit;
                    }
                }
                EditorGUILayout.PropertyField(serializedObject.FindProperty("is_show_grid"), new GUIContent("is_show_grid", "Show the tilemap grid."));
            }
            EditorGUILayout.Space();

            EditorGUILayout.LabelField(string.Format("Map Size ({0},{1})", tileMap.GridWidth, tileMap.GridHeight));

            //Display Map Bounds
            using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
            {
                EditorGUILayout.LabelField("Map Bounds (in tiles):", EditorStyles.boldLabel);
                is_toggle_map_bounds_edit = EditorGUIUtil.ToggleIconButton("Edit Map Bounds", is_toggle_map_bounds_edit, EditorGUIUtility.IconContent("EditCollider"));

                float saved_label_width = EditorGUIUtility.labelWidth;
                EditorGUIUtility.labelWidth = 80;
                using (new EditorGUIIndentLevelScope(2))
                {
                    using (var check = new EditorGUIBeginChangeCheckScope())
                    {
                        using (new EditorGUILayoutBeginHorizontalScope())
                        {
                            EditorGUILayout.PropertyField(serializedObject.FindProperty("min_grid_x"), new GUIContent("Left"));
                            EditorGUILayout.PropertyField(serializedObject.FindProperty("min_grid_y"), new GUIContent("Bottom"));
                        }
                        using (new EditorGUILayoutBeginHorizontalScope())
                        {
                            EditorGUILayout.PropertyField(serializedObject.FindProperty("max_grid_x"), new GUIContent("Right"));
                            EditorGUILayout.PropertyField(serializedObject.FindProperty("max_grid_y"), new GUIContent("Top"));
                        }
                        if (check.IsChanged)
                        {
                            serializedObject.ApplyModifiedProperties();
                            tileMap.RecalculateMapBounds();
                        }
                    }
                }
                EditorGUIUtility.labelWidth = saved_label_width;
            }

            EditorGUILayout.Space();

            tileMap.is_allow_painting_out_of_bounds = EditorGUILayout.ToggleLeft("is_allow_painting_out_of_bounds", tileMap.is_allow_painting_out_of_bounds);

            EditorGUILayout.Space();

            if (GUILayout.Button("Shrink to Visible Area", GUILayout.MaxWidth(150)))
            {
                tileMap.ShrinkMapBoundsToVisibleArea();
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("is_auto_shrink"));

            EditorGUILayout.Space();
            using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
            {
                EditorGUILayout.LabelField("Advanced Options", EditorStyles.boldLabel);
                using (var check = new EditorGUIBeginChangeCheckScope())
                {
                    bool is_tileMapChunks_visible = IsTileMapChunksVisible();
                    is_tileMapChunks_visible = EditorGUILayout.Toggle(new GUIContent("is_tileMapChunks_visible", "Show tilemap chunk objects for debugging or other purposes. Hiding will be refreshed after collapsing the tilemap."), is_tileMapChunks_visible);
                    if (check.IsChanged)
                    {
                        SetTileMapChunkHideFlag(HideFlags.HideInHierarchy, !is_tileMapChunks_visible);
                    }
                }
                EditorGUILayout.PropertyField(serializedObject.FindProperty("is_enable_undo_while_painting"), new GUIContent("Enable Undo", "Disable Undo when painting on big maps to improve performance."));
            }
        }
Пример #7
0
        private void OnInspectorGUI_Renderer()
        {
            using (var check = new EditorGUIBeginChangeCheckScope())
            {
                Material pre_material = tileMap.material;
                EditorGUILayout.PropertyField(serializedObject.FindProperty("_material"));
                if (check.IsChanged)
                {
                    serializedObject.ApplyModifiedProperties();
                    tileMap.Refresh();
                    if (tileMap.material != pre_material && !AssetDatabase.Contains(pre_material))
                    {
                        DestroyImmediate(pre_material);
                    }
                }
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("_tintColor"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("parallax_factor"));

            //Pixel Snap
            if (tileMap.material.HasProperty("is_pixel_snap"))
            {
                using (var check = new EditorGUIBeginChangeCheckScope())
                {
                    bool is_pixel_snap = EditorGUILayout.Toggle("is_pixel_snap", tileMap.is_pixel_snap);
                    if (check.IsChanged)
                    {
                        tileMap.is_pixel_snap = is_pixel_snap;
                    }
                }
            }

            // Sorting Layer and Order in layer
            using (var check = new EditorGUIBeginChangeCheckScope())
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("sortingLayer"));
                EditorGUILayout.PropertyField(serializedObject.FindProperty("orderInLayer"));
                serializedObject.FindProperty("orderInLayer").intValue = (serializedObject.FindProperty("orderInLayer").intValue << 16) >> 16; // convert from int32 to int16 keeping sign
                if (check.IsChanged)
                {
                    serializedObject.ApplyModifiedProperties();
                    tileMap.RefreshTileMapChunksSortingAttributes();
                    SceneView.RepaintAll();
                }
            }

            EditorGUILayout.PropertyField(serializedObject.FindProperty("inner_padding"), new GUIContent("Inner Padding", "The size, in pixels, the tile UV will be stretched. Use this to fix pixel precision artifacts when tiles have no padding border in the atlas."));

            tileMap.is_visible = EditorGUILayout.Toggle("Is Visible", tileMap.is_visible);
            EditorGUILayout.Space();
            //Render Properties
            using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
            {
                EditorGUILayout.LabelField("Render Properties", EditorStyles.boldLabel);

                SerializedProperty tileMapChunkRendererProperties = serializedObject.FindProperty("tileMapChunkRendererProperties");

                using (var check = new EditorGUIBeginChangeCheckScope())
                {
                    EditorGUILayout.PropertyField(tileMapChunkRendererProperties.FindPropertyRelative("shadowCastingMode"));
                    EditorGUILayout.PropertyField(tileMapChunkRendererProperties.FindPropertyRelative("is_receive_shadows"));
                    EditorGUILayout.PropertyField(tileMapChunkRendererProperties.FindPropertyRelative("lightProbeUsage"));
                    EditorGUILayout.PropertyField(tileMapChunkRendererProperties.FindPropertyRelative("reflectionProbeUsage"));
                    if (tileMap.tileMapChunkRendererProperties.reflectionProbeUsage != UnityEngine.Rendering.ReflectionProbeUsage.Off)
                    {
                        using (new EditorGUIIndentLevelScope(2))
                        {
                            EditorGUILayout.PropertyField(tileMapChunkRendererProperties.FindPropertyRelative("probeAnchor"));
                        }
                    }

                    if (check.IsChanged)
                    {
                        serializedObject.ApplyModifiedProperties();
                        tileMap.UpdateTileMapChunkRenderereProperties();
                    }
                }
            }
        }
Пример #8
0
        private void DisplayCollider()
        {
            Event e = Event.current;

            if (tileSet.selected_tileSetBrushId != TileSetConst.TileSetBrushId_Default)
            {
                EditorGUILayout.LabelField("tileSetBrush不能被编辑", EditorStyles.boldLabel);
                return;
            }

            bool is_multi_selection = tileSet.tileSelection != null;
            bool is_save_changes    = false;
            Tile selected_tile      = is_multi_selection
      ? tileSet.tile_list[TileSetUtil.GetTileIdFromTileData(tileSet.tileSelection.selection_tileData_list[0])]
      : tileSet.selected_tile;

            if (e.type == EventType.MouseDown)
            {
                is_dragging = true;
                if (selected_tile.tileColliderData.vertices != null)
                {
                    saved_vertexs = new Vector2[selected_tile.tileColliderData.vertices.Length];
                    selected_tile.tileColliderData.vertices.CopyTo(saved_vertexs, 0);
                }
            }
            else if (e.type == EventType.MouseUp)
            {
                is_dragging = false;
            }

            //background_color
            tileSet.background_color = EditorGUILayout.ColorField(tileSet.background_color);
            if (pre_background_color != tileSet.background_color || GUIStyleConst.Scroll_Style.normal.background == null)
            {
                pre_background_color = tileSet.background_color;
                if (GUIStyleConst.Scroll_Style.normal.background == null)
                {
                    GUIStyleConst.Scroll_Style.normal.background = new Texture2D(1, 1)
                    {
                        hideFlags = HideFlags.DontSave
                    }
                }
                ;
                GUIStyleConst.Scroll_Style.normal.background.SetPixel(0, 0, tileSet.background_color);
                GUIStyleConst.Scroll_Style.normal.background.Apply();
            }

            float aspect_ratio = tileSet.tile_pixel_size.x / tileSet.tile_pixel_size.y; //比例
            float padding      = 2;                                                     // pixel size of the border around the tile

            //画tile图
            //包围tile_rect的rect
            Rect tile_parent_rect = GUILayoutUtility.GetRect(1, 1, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            using (new GUIBeginGroupScope(tile_parent_rect, GUIStyleConst.Scroll_Style))
            {
                if (e.type == EventType.Repaint)
                {
                    float tile_pixel_size = tile_parent_rect.width / (tileSet.tile_pixel_size.x + 2 * padding);//2*padding是因为padding有左边和右边
                    mouse_pos = e.mousePosition;
                    tile_rect = new Rect(padding * tile_pixel_size, padding * tile_pixel_size,
                                         tile_parent_rect.width - 2 * padding * tile_pixel_size,
                                         (tile_parent_rect.width / aspect_ratio) - 2 * padding * tile_pixel_size);
                    tile_rect.height = Mathf.Min(tile_rect.height, tile_parent_rect.height - 2 * padding * tile_pixel_size);
                    tile_rect.width  = (tile_rect.height * aspect_ratio);
                }
                using (new GUIColorScope(new Color(1f, 1f, 1f, 0.1f)))
                {
                    GUI.DrawTexture(tile_rect, EditorGUIUtility.whiteTexture);
                }
                if (is_multi_selection)
                {
                    foreach (uint tileData in tileSet.tileSelection.selection_tileData_list)
                    {
                        int  tileId = TileSetUtil.GetTileIdFromTileData(tileData);
                        Tile tile   = tileSet.GetTile(tileId);
                        if (tile != null)
                        {
                            GUI.color = new Color(1f, 1f, 1f, 1f / tileSet.tileSelection.selection_tileData_list.Count);
                            GUI.DrawTextureWithTexCoords(tile_rect, tileSet.atlas_texture, tile.uv);
                        }
                    }
                    GUI.color = Color.white;
                }
                else
                {
                    GUI.DrawTextureWithTexCoords(tile_rect, tileSet.atlas_texture, selected_tile.uv);
                }

                /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                Color saved_handle_color = Handles.color;
                if (selected_tile.tileColliderData.type != TileColliderType.None)
                {
                    Vector2[] collider_vertices = selected_tile.tileColliderData.type == TileColliderType.Full ? TileConst.Full_Collider_Tile_Vertices : selected_tile.tileColliderData.vertices;
                    if (collider_vertices == null || collider_vertices.Length == 0)
                    {
                        collider_vertices = selected_tile.tileColliderData.vertices = new Vector2[TileConst.Full_Collider_Tile_Vertices.Length];
                        Array.Copy(TileConst.Full_Collider_Tile_Vertices, collider_vertices, TileConst.Full_Collider_Tile_Vertices.Length);
                        EditorUtility.SetDirty(tileSet);
                    }

                    // 将vertice的xy限制在[0,1]之间
                    for (int i = 0; i < collider_vertices.Length; ++i)
                    {
                        Vector2 collider_vertice = collider_vertices[i];
                        collider_vertice     = collider_vertice.Snap2(tileSet.tile_pixel_size).ConvertElement(v => Mathf.Clamp01(v));
                        collider_vertices[i] = collider_vertice;
                    }

                    // 画边 draw edges
                    Vector3[] poly_edges = new Vector3[collider_vertices.Length + 1];
                    for (int i = 0; i < collider_vertices.Length; ++i)
                    {
                        Vector2 collider_vertice = collider_vertices[i];
                        collider_vertice.x = tile_rect.x + tile_rect.width * collider_vertice.x;
                        collider_vertice.y = tile_rect.yMax - tile_rect.height * collider_vertice.y;

                        Vector2 collider_vertice_next = collider_vertices[(i + 1) % collider_vertices.Length];
                        collider_vertice_next.x = tile_rect.x + tile_rect.width * collider_vertice_next.x;
                        collider_vertice_next.y = tile_rect.yMax - tile_rect.height * collider_vertice_next.y;

                        poly_edges[i]     = collider_vertice;
                        poly_edges[i + 1] = collider_vertice_next;

                        //画边
                        Handles.color = Color.green;
                        Handles.DrawLine(collider_vertice, collider_vertice_next);
                        //画边上的法线 Draw normals
                        Handles.color = Color.white;
                        Vector3 normal_pos = (collider_vertice + collider_vertice_next) / 2f;
                        Handles.DrawLine(normal_pos, normal_pos + Vector3.Cross(collider_vertice_next - collider_vertice, Vector3.forward).normalized *tile_rect.yMin);

                        Handles.color = saved_handle_color;
                    }

                    float pixel_size = tile_rect.width / tileSet.tile_pixel_size.x;
                    if (selected_tile.tileColliderData.type == TileColliderType.Polygon)
                    {
                        bool is_adding_vertex_on   = !is_dragging && e.shift && active_vertex_index == -1;
                        bool is_removing_vertex_on = !is_dragging && ((Application.platform == RuntimePlatform.OSXEditor) ? e.command : e.control) && collider_vertices.Length > 3;
                        //删除顶点
                        if (is_removing_vertex_on && active_vertex_index != -1 && e.type == EventType.MouseUp)
                        {
                            selected_tile.tileColliderData.vertices = new Vector2[collider_vertices.Length - 1];
                            for (int i = 0, j = 0; i < collider_vertices.Length; ++i)
                            {
                                if (i == active_vertex_index)
                                {
                                    continue;
                                }
                                selected_tile.tileColliderData.vertices[j] = collider_vertices[i];
                                ++j;
                            }
                            collider_vertices   = selected_tile.tileColliderData.vertices;
                            active_vertex_index = -1;
                        }

                        float min_distance = float.MaxValue;
                        if (!is_dragging)
                        {
                            active_vertex_index = -1;
                        }
                        for (int i = 0; i < collider_vertices.Length; ++i)
                        {
                            Vector2 collider_vertice = collider_vertices[i];
                            collider_vertice.x = tile_rect.x + tile_rect.width * collider_vertice.x;
                            collider_vertice.y = tile_rect.yMax - tile_rect.height * collider_vertice.y;

                            if (is_dragging)
                            {
                                if (i == active_vertex_index)
                                {
                                    collider_vertice       = mouse_pos;
                                    collider_vertice      -= tile_rect.position;
                                    collider_vertice.x     = Mathf.Clamp(Mathf.Round(collider_vertice.x / pixel_size) * pixel_size, 0, tile_rect.width);
                                    collider_vertice.y     = Mathf.Clamp(Mathf.Round(collider_vertice.y / pixel_size) * pixel_size, 0, tile_rect.height);
                                    collider_vertice      += tile_rect.position;
                                    collider_vertices[i].x = Mathf.Clamp01((collider_vertice.x - tile_rect.x) / tile_rect.width);
                                    collider_vertices[i].y = Mathf.Clamp01((collider_vertice.y - tile_rect.yMax) / -tile_rect.height);
                                }
                            }
                            else
                            {
                                float distance = Vector2.Distance(mouse_pos, collider_vertice);
                                //distance < GUIStyleConst.Collider_Vertex_Handle_Style.normal.background.width是为了限定少于多少距离(该顶点与mouse_pos的距离)的时候才选中该顶点
                                if (distance <= min_distance && distance < GUIStyleConst.Collider_Vertex_Handle_Style.normal.background.width)
                                {
                                    min_distance        = distance;
                                    active_vertex_index = i;
                                }
                            }
                            //画顶点
                            if (e.type == EventType.Repaint)
                            {
                                //画左上角的坐标
                                if (i == active_vertex_index)
                                {
                                    GUIStyleConst.VertexCoord_Style.fontSize = (int)(Mathf.Min(12f, tile_rect.yMin / 2f));
                                    GUI.Label(new Rect(0, 0, tile_rect.width, tile_rect.yMin), Vector2.Scale(collider_vertices[i], tileSet.tile_pixel_size).ToString(), GUIStyleConst.VertexCoord_Style);
                                }
                                GUI.color = active_vertex_index == i ? (is_removing_vertex_on ? Color.red : Color.cyan) : new Color(0.7f, 0.7f, 0.7f, 0.8f);
                                GUIStyleConst.Collider_Vertex_Handle_Style.Draw(new Rect(collider_vertice.x - GUIStyleConst.Collider_Vertex_Handle_Style.normal.background.width / 2, collider_vertice.y - GUIStyleConst.Collider_Vertex_Handle_Style.normal.background.height / 2, 1, 1), i.ToString(), false, false, false, false);
                                GUI.color = Color.white;
                            }
                        }

                        //添加顶点
                        if (is_adding_vertex_on)
                        {
                            //线段index
                            int     segment_index;
                            Vector2 new_vertex_pos = ClosestPointToPolyLine(poly_edges, out segment_index);

                            if (e.type == EventType.MouseUp)
                            {
                                selected_tile.tileColliderData.vertices = new Vector2[collider_vertices.Length + 1];
                                segment_index = (segment_index + 1) % selected_tile.tileColliderData.vertices.Length;
                                for (int i = 0, j = 0; i < selected_tile.tileColliderData.vertices.Length; ++i)
                                {
                                    if (segment_index == i)
                                    {
                                        new_vertex_pos.x = Mathf.Clamp(Mathf.Round(new_vertex_pos.x / pixel_size) * pixel_size, tile_rect.x, tile_rect.xMax);
                                        new_vertex_pos.y = Mathf.Clamp(Mathf.Round(new_vertex_pos.y / pixel_size) * pixel_size, tile_rect.y, tile_rect.yMax);
                                        selected_tile.tileColliderData.vertices[i].x = Mathf.Clamp01((new_vertex_pos.x - tile_rect.x) / tile_rect.width);
                                        selected_tile.tileColliderData.vertices[i].y = Mathf.Clamp01((new_vertex_pos.y - tile_rect.yMax) / -tile_rect.height);
                                    }
                                    else
                                    {
                                        selected_tile.tileColliderData.vertices[i] = collider_vertices[j];
                                        ++j;
                                    }
                                }
                                collider_vertices   = selected_tile.tileColliderData.vertices;
                                active_vertex_index = -1;
                            }
                            else if (e.type == EventType.Repaint)
                            {
                                //添加顶点中移动鼠标,透明动态显示当前将要新增顶点的位置
                                GUI.color = new Color(0.7f, 0.7f, 0.7f, 0.8f);
                                GUIStyleConst.Collider_Vertex_Handle_Style.Draw(new Rect(new_vertex_pos.x - GUIStyleConst.Collider_Vertex_Handle_Style.normal.background.width / 2, new_vertex_pos.y - GUIStyleConst.Collider_Vertex_Handle_Style.normal.background.height / 2, 1, 1), segment_index.ToString(), false, false, false, false);
                                GUI.color = Color.white;
                            }
                        }
                    }

                    if (e.type == EventType.MouseUp)
                    {
                        is_save_changes = true;
                        //remove duplicated vertex
                        selected_tile.tileColliderData.vertices = selected_tile.tileColliderData.vertices.Distinct().ToArray();
                        if (selected_tile.tileColliderData.vertices.Length <= 2)
                        {
                            selected_tile.tileColliderData.vertices = saved_vertexs;
                        }
                        //snap vertex positions
                        selected_tile.tileColliderData.SnapVertices(tileSet);
                    }
                }
            }



            if (GUILayout.Button("反转法线"))
            {
                selected_tile.tileColliderData.vertices.Reverse();
            }

            EditorGUILayout.Space();

            string helpInfo =
                "  - 移动:点击图中的顶点并移动它的位置" + "\n" +
                "  - 添加:按住Shift并点击鼠标进行添加顶点" + "\n" +
                "  - 删除:按住Ctrl并且点击顶点进行删除顶点 (should be more than 3)";

            EditorGUILayout.HelpBox(helpInfo, MessageType.Info);

            // Collider Settings
            float saved_label_width = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = 40;
            if (is_multi_selection)
            {
                EditorGUILayout.LabelField("* Multi-selection Edition", EditorStyles.boldLabel);
            }
            using (new EditorGUILayoutBeginHorizontalScope(GUILayout.MinWidth(140)))
            {
                EditorGUILayout.LabelField("Collider Data", EditorStyles.boldLabel);
                if (GUILayout.Button("Copy", GUILayout.Width(50)))
                {
                    copied_colliderData = selected_tile.tileColliderData.Clone();
                }
                if (GUILayout.Button("Paste", GUILayout.Width(50)))
                {
                    selected_tile.tileColliderData = copied_colliderData.Clone();
                    is_save_changes = true;
                }
            }
            EditorGUILayout.Space();

            using (var check = new EditorGUIBeginChangeCheckScope())
            {
                EditorGUIUtility.labelWidth = 100;
                EditorGUILayout.LabelField("Collider Type:", EditorStyles.boldLabel);
                using (new EditorGUIIndentLevelScope(2))
                {
                    string[] tile_collider_names = Enum.GetNames(typeof(TileColliderType));
                    selected_tile.tileColliderData.type = (TileColliderType)GUILayout.Toolbar((int)selected_tile.tileColliderData.type, tile_collider_names);
                }
                EditorGUIUtility.labelWidth = saved_label_width;
                is_save_changes            |= check.IsChanged;
            }

            //Save changes
            if (is_save_changes)
            {
                if (is_multi_selection)
                {
                    for (int i = 0; i < tileSet.tileSelection.selection_tileData_list.Count; ++i)
                    {
                        tileSet.tile_list[TileSetUtil.GetTileIdFromTileData(tileSet.tileSelection.selection_tileData_list[i])].tileColliderData = selected_tile.tileColliderData.Clone();
                    }
                }
                EditorUtility.SetDirty(tileSet);
                //Refresh selected tileMap
                TileMap selected_tileMap = Selection.activeGameObject ? Selection.activeGameObject.GetComponent <TileMap>() : null;
                if (selected_tileMap)
                {
                    selected_tileMap.Refresh(false, true);
                }
            }
        }

        //获取polyline(vertices组成的线段)线段上的离当前mouse poistion最近的点
        //Get the point on a polyline (in 3D space) which is closest to the current mouse position
        Vector3 ClosestPointToPolyLine(Vector3[] vertices, out int closest_segment_index)
        {
            float min_distance = float.MaxValue;

            closest_segment_index = 0;
            for (int i = 0; i < vertices.Length - 1; ++i)
            {
                float distance = HandleUtility.DistanceToLine(vertices[i], vertices[i + 1]);
                if (distance < min_distance)
                {
                    min_distance          = distance;
                    closest_segment_index = i;
                }
            }
            return(HandleUtility.ClosestPointToPolyLine(vertices));
        }
    }
Пример #9
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
            {
                _target.astarConfigData.textAsset =
                    EditorGUILayout.ObjectField(_target.astarConfigData.textAsset, typeof(TextAsset), false) as
                    TextAsset;
                using (new EditorGUILayoutBeginHorizontalScope())
                {
                    if (GUILayout.Button("Load"))
                    {
                        _target.Load();
                        return;
                    }

                    if (GUILayout.Button("Save"))
                    {
                        _target.Save();
                        return;
                    }
                }
            }


            _target.astarConfigData.cellSize =
                EditorGUILayout.Vector2Field("cell_size", _target.astarConfigData.cellSize);

            using (new EditorGUILayoutBeginVerticalScope(EditorStyles.helpBox))
            {
                EditorGUILayout.LabelField("Bounds:", EditorStyles.boldLabel);
                using (new EditorGUIUtilityLabelWidthScope(80))
                {
                    using (var check = new EditorGUIBeginChangeCheckScope())
                    {
                        _target.astarConfigData.isEnableEditOutsideBounds = EditorGUILayout.Toggle("允许在边界外编辑?",
                                                                                                   _target.astarConfigData.isEnableEditOutsideBounds);
                        using (new EditorGUILayoutBeginHorizontalScope())
                        {
                            _target.astarConfigData.minGridX =
                                EditorGUILayout.IntField("Left", _target.astarConfigData.minGridX);
                            _target.astarConfigData.minGridY =
                                EditorGUILayout.IntField("Bottom", _target.astarConfigData.minGridY);
                        }

                        using (new EditorGUILayoutBeginHorizontalScope())
                        {
                            _target.astarConfigData.maxGridX =
                                EditorGUILayout.IntField("Right", _target.astarConfigData.maxGridX);
                            _target.astarConfigData.maxGridY =
                                EditorGUILayout.IntField("Top", _target.astarConfigData.maxGridY);
                        }

                        if (check.IsChanged)
                        {
                            _target.astarConfigData.Resize();
                        }
                    }
                }
            }


            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(_target);
            }
        }
        private void DisplayPrefab()
        {
            if (tileSet.selected_tileSetBrushId != TileSetConst.TileSetBrushId_Default)
            {
                EditorGUILayout.LabelField("TileSetBrush tile不能被编辑", EditorStyles.boldLabel);
            }
            else
            {
                bool is_multi_selection = tileSet.tileSelection != null;
                Tile selected_tile      = is_multi_selection
        ? tileSet.tile_list[TileSetUtil.GetTileIdFromTileData(tileSet.tileSelection.selection_tileData_list[0])]
        : tileSet.selected_tile;
                GUILayoutUtility.GetRect(1, 1, GUILayout.Width(tileSet.visual_tile_size.x),
                                         GUILayout.Height(tileSet.visual_tile_size.y));//Pos1
                Rect tile_uv = selected_tile.uv;
                GUI.color = tileSet.background_color;
                GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), EditorGUIUtility.whiteTexture, tile_uv, true); //即在Pos1处画图片
                GUI.color = Color.white;
                GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), tileSet.atlas_texture, tile_uv, true);         //即在Pos1处画图片

                if (is_multi_selection)
                {
                    EditorGUILayout.LabelField("* Multi-selection Edition", EditorStyles.boldLabel);
                }

                using (var check1 = new EditorGUIBeginChangeCheckScope())
                {
                    TilePrefabData tilePrefabData    = selected_tile.tilePrefabData;
                    float          saved_label_width = EditorGUIUtility.labelWidth;
                    EditorGUIUtility.labelWidth = 80;
                    //offset
                    tilePrefabData.offset     = EditorGUILayout.Vector3Field("offset", tilePrefabData.offset);
                    tilePrefabData.offsetMode = (TilePrefabDataOffsetMode)EditorGUILayout.EnumPopup("offset mode", tilePrefabData.offsetMode);
                    //prefab
                    bool is_prefab_changed = false;
                    using (var check2 = new EditorGUIBeginChangeCheckScope())
                    {
                        tilePrefabData.prefab =
                            (GameObject)EditorGUILayout.ObjectField("Prefab", tilePrefabData.prefab, typeof(GameObject), false);
                        if (check2.IsChanged)
                        {
                            is_prefab_changed = true;
                        }
                    }

                    //prefab preview
                    Texture2D prefab_preview;
                    using (new GUILayoutBeginHorizontalScope())
                    {
                        prefab_preview = AssetPreview.GetAssetPreview(selected_tile.tilePrefabData.prefab);
                        GUILayout.Box(prefab_preview, prefab_preview != null ? (GUIStyle)"Box" : GUIStyle.none);
                    }

                    if (tilePrefabData.prefab)
                    {
                        EditorGUIUtility.labelWidth = 260;
                        if (prefab_preview)
                        {
                            tilePrefabData.is_show_prefab_preview_in_tile_palette = EditorGUILayout.Toggle("在tile palette中显示预设",
                                                                                                           tilePrefabData.is_show_prefab_preview_in_tile_palette);
                        }
                        EditorGUIUtility.labelWidth             = saved_label_width;
                        tilePrefabData.is_show_tile_with_prefab =
                            EditorGUILayout.Toggle("Show Tile With Prefab", tilePrefabData.is_show_tile_with_prefab);
                    }

                    if (check1.IsChanged)
                    {
                        Undo.RecordObject(tileSet, "Tile Prefab Data Changed");
                        if (is_multi_selection)
                        {
                            for (int i = 0; i < tileSet.tileSelection.selection_tileData_list.Count; ++i)
                            {
                                Tile       tile         = tileSet.tile_list[TileSetUtil.GetTileIdFromTileData(tileSet.tileSelection.selection_tileData_list[i])];
                                GameObject saved_prefab = tile.tilePrefabData.prefab;
                                tile.tilePrefabData = tilePrefabData;
                                if (!is_prefab_changed)
                                {
                                    tile.tilePrefabData.prefab = saved_prefab;
                                }
                            }
                        }
                        else
                        {
                            selected_tile.tilePrefabData = tilePrefabData;
                        }

                        EditorUtility.SetDirty(tileSet);
                    }
                }
            }
        }