Saturday 12 June 2021

Unity editor scripting (part 1) - setting the Android keystore and password automatically

One of the interesting features of Unity game enigne is that it's quite easy to extend its capabilities via editor scripts.

Let's take a look at the first example: setting the Android keystore and passwords automatically. The code below has been used with Unity 2019.4.24f1 on Windows and MacOS. I cannot guarantee it works out-of-the-box on other versions, but the differences shouldn't be huge.

A commonly encountered problem when working with other people on an Android Unity game, is managing the signing credentials. 

As the keystore file and credentials are often outside the main repo, the signing setup is lost between pushed commits and developers' machines. Moreover: while the keystore file location and keyalias name are stored in Unity's famous ProjectSettings file (a.k.a. the Blob, really, there's everything there), and therefore on the repo; the passwords are not (which makes sense, as passwords should be secure and not stored in some random blob-like messy configuration files... Generally - they shouldn't be keeped on the repository at all).

Some solution to that could be an editor script, setting the credentials to ones given in a secure.properties file, which includes store file path, key alias path, store password and key password. Of course, if you want to keep the credentials outside the repo, remember to add both secure.properties and .keystore file to your .gitignore.


So, let's take a look:



And then we need to set up the secure.properties, as follows:
... and set up the folder structure, as follows:
 
YourProjectFolder/
├─ Assets/
│  ├─ Scripts/
│  │  ├─ Editor/
│  │  │  ├─ SetKeystore.cs
secure.properties
YourKeystore.keystore

Now how it works:
First of all, we need to find a way to run some code when the Unity is loaded. For that, we need the [InitializeOnLoad] directive added above the class name, and then the code we want to run in the static constructor. This way the code should be run when the Unity editor is open. The rest of the code (the DoSetKeystore() method) is pretty self-explanatory: it uses the PlayerSettings class to set up the appropriate credentials. From my experience, I'd say almost every thing you can do with Unity's GUI, you can also do with an editor script. And you should do it with a script.

The menuItem("Tools/Set keystore.")] attribute before a static method causes it to appear as a menu item ("Set keystore" command in the "Tools" menu) - such method can be called manually by choosing the option in the Unity's menu.
In our case: if you want to change the credentials and then restore the default ones (or if you run the editor without .properties and .keystore files set up properly), you can always set the credentials later using the menu item - without needing to restart the editor. If you don't want to have your Tools menu crowded, feel free to remome the attribute. But it's good to know such attribute exists (it's a handy way of calling various other editor scripts, and we all love editor scripts).

Please note the folder structure - the secure.properties file placed next to your project folder (the one containing Assets folder). As we can see, when opening the file in the Unity editor sciript, we refer to it's location relative to the Assets folder (not the script itself). The same applies to the .keystore file location in secure.properties - that's why it also has ".." there.

Some meta-information for myself (beginner in blogging):
  • For creating a Blogger-embeddable code snippet, it's easy to use Github's gistshttps://gist.github.com/. It should be embeded using Blogger's HTML edit mode.
  • The folder structure ASCII graph can be easily generated with https://ascii-tree-generator.com/ tool, which is awesome. Such structure can be embedded using the Blogger's create mode. It'll provide useful when presenting more Unity editor scripts in the future...
  • As for now, I'm using Helvetica as the main blog font and Recursive as the font for inline code. I'm not sure if it's the most readable option. I'll experiment with this.
For part 2 of editor scripting tutorial, click here.

No comments:

Post a Comment

Python crash course part 10: inheritance and polymorphism

In the last part we've shown how to create and use a class in Python. Today we're going to talk about inheritance: wchich means cre...