• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aniki219 · May 15, 2020 at 10:08 PM · editor-scripting

Add custom hotkeys to internal Unity Editor window

I'm using the Tile Palette and I find it so clunky. I want to add custom hotkeys to speed up my work flow, maybe change the tilemap I'm painting with ctrl+up/down- that kind of thing.
I started looking into Editor Scripting but it doesn't seem like I'm actually able to do this. I just want to read/write values to/from existing editor windows.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by exploringunity · May 17, 2020 at 05:43 AM

Hey @Aniki219,

The UnityEditor.Tilemaps.GridPaintingState class has the following public properties you can use:

  • validTargets, which is all available tilemaps

  • scenePaintTarget, which is the current active tilemap

If the properties you wanted to change happened to be private instead of public, you could still use Reflection to set them. Here is an example of using the properties above to change the active tilemap, and Reflection to change the active palette.

Editor/TilemapHelper.cs

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using System.Linq;
 using UnityEditor.Tilemaps;
 
 public class TilemapHelper
 {
     [MenuItem("Custom Tools/Next Tilemap %&q")]
     static void SelectNextTilemap()
     {
         // Current/available tilemaps can be found in GridPaintingState
         var tilemaps = GridPaintingState.validTargets;
         var numTilemaps = tilemaps.Length;
         var currentTilemap = GridPaintingState.scenePaintTarget;
         // Handle trivial cases
         if (numTilemaps == 0) { return; }
         if (numTilemaps == 1) { GridPaintingState.scenePaintTarget = tilemaps[0]; return; }
         // Find current tilemap index
         var idx = 0;
         for (; idx < numTilemaps; idx++)
         {
             var tilemap = tilemaps[idx];
             if (tilemap == currentTilemap) { break; }
         }
         // Move to next tilemap, with wraparound
         idx = (idx + 1) % numTilemaps;
         // Set new tilemap
         GridPaintingState.scenePaintTarget = tilemaps[idx];
     }
     
     [MenuItem("Custom Tools/Next Palette %&e")]
     static void SelectNextPalette()
     {
         // Find current/all palettes using reflection
         var knownType = typeof(GridPaintingState);
         var assembly = knownType.Assembly;
         var assmeblyTypes = assembly.GetTypes();
         var wndType = assmeblyTypes.Single(x => x.Name == "GridPaintPaletteWindow");
         var wnd = EditorWindow.GetWindow(wndType);
         var currentPaletteProp = wndType.GetProperty("palette");
         var currentPalette = (GameObject)currentPaletteProp.GetValue(wnd);
         var gridPalettesType = assmeblyTypes.Single(x => x.Name == "GridPalettes");
         var palettesProp = gridPalettesType.GetProperty("palettes");
         var palettes = (List<GameObject>)palettesProp.GetValue(gridPalettesType);
         var numPalettes = palettes.Count;
         // Handle trivial cases
         if (numPalettes == 0) { return; }
         if (numPalettes == 1) { currentPaletteProp.SetValue(wnd, palettes[0]); return; }
         // Find current palette and choose next
         GameObject newPalette = null;
         for (var i = 0; i < numPalettes; i++) 
         {
             if (palettes[i] == currentPalette) 
             {
                 var nextIdx = (i + 1) % numPalettes;
                 newPalette = palettes[nextIdx];
                 break;
             }
         }
         // Set palette using reflection
         currentPaletteProp.SetValue(wnd, newPalette);
     }
 }

Hope this helps!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aniki219 · May 17, 2020 at 07:54 PM 0
Share

Thank you very much, this is exactly what I wanted!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I reenable selection tools after disabling them? 0 Answers

Reorderable list of UnityEvents 2 Answers

C#: Include Custom Editor features for a custom Classes while avoiding dependencies? 0 Answers

Hotkeys that don't interfere with renaming files 0 Answers

Function to move object to scene view 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges