Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?
@tetr4lab

UnityでAndroidのKeystoreパスワードを自動セーブ・ロードする

前提

  • unity 2018.4.7f1
  • Android

できること

  • 一度、KeystoreとAliasのパスワードを設定してビルドすると、以降、エディター起動時に自動的に設定されるようになります。

やりかた

  • 以下のコードを、適当なAssets/~/Editor/~へ入れてください。
SaveKeystore.cs
using UnityEditor;
using UnityEditor.Callbacks;

public class SaveKeystore {

    private static string keystorePrefsName => PlayerSettings.Android.keystoreName;
    private static string keyaliasPrefsName => $"{PlayerSettings.Android.keystoreName}/{PlayerSettings.Android.keyaliasName}";

    [InitializeOnLoadMethod]
    private static void OnLoad () {
        if (string.IsNullOrEmpty (PlayerSettings.Android.keystorePass) && !string.IsNullOrEmpty (PlayerSettings.Android.keystoreName)) {
            PlayerSettings.Android.keystorePass = EditorPrefs.GetString (keystorePrefsName);
        }
        if (string.IsNullOrEmpty (PlayerSettings.Android.keyaliasPass)) {
            PlayerSettings.Android.keyaliasPass = EditorPrefs.GetString (keyaliasPrefsName);
        }
    }

    [PostProcessBuild]
    private static void OnBuilded (BuildTarget target, string path) {
        if (target == BuildTarget.Android) {
            if (!string.IsNullOrEmpty (PlayerSettings.Android.keystoreName) && !string.IsNullOrEmpty (PlayerSettings.Android.keystorePass)) {
                EditorPrefs.SetString (keystorePrefsName, PlayerSettings.Android.keystorePass);
            }
            if (!string.IsNullOrEmpty (PlayerSettings.Android.keyaliasName) && !string.IsNullOrEmpty (PlayerSettings.Android.keyaliasPass)) {
                EditorPrefs.SetString (keyaliasPrefsName, PlayerSettings.Android.keyaliasPass);
            }
        }
    }

}

留意事項

  • KeystoreおよびAliasのパスワードが、平文でエディターの設定内(EditorPrefs)に保存されます。
  • Keystoreファイルの絶対パスに依存します。プロジェクトフォルダを移動するとオートロードできなくなります。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?