Configuring Unity Preferences from C#

Hello, I was wondering if it was possible to configure Unity Preferences (Unity → Preferences) from C#?

More specifically, I would like to enable cache server and set the IP address to use from a script that would run at startup so it doesn’t require the user to manually (aka remember) to do this.

What you’re looking for is EditorPrefs.

The problem there is that the keys are not publicly available. I know that eg. auto-refresh is set through:

EditorPrefs.SetBool("kAutoRefresh", true|false);

Because @karl_jones told me. Maybe he’s got some hot info on what cache server options and the IP adress are?

Of course, these values are written to the registry, so you could go hunting for a key in the registry that’s got the same value as the IP address you’ve set. I belive the EditorPrefs names and the registry keys are exactly the same. It’s probably under Unity somewhere?

1 Like

Looking at where EditorPrefs is stored, it uses the following keys:

EditorPrefs.SetString(“CacheServerIPAddress”,“your-cache-server.com”);
EditorPrefs.SetBool(“CacheServerEnabled”,true);

Apparently there is an internal class in UnityEditor called CacheServerPreferences but Type.GetType(“UnityEditor.CacheServerPreferences”) failed to return a reference to it. There is a function in there called ReadPreferences() that loads values from EditorPrefs. It may not be necessary to call manually after setting the above anyway as my initial tests proved just setting the prefs using EditorPrefs did what I want.

It would be nice if there was a more official way of doing this, but I will take this over nothing.

You can use the trick i suggested in this thread: EditorPrefs pre-defined magic strings - Web Resources - Unity Discussions

This will allow you to see all the different reads/writes the editor is invoking using EditorPrefs.

Scripts below are tested under Unity 2017.4.4f1:

            EditorPrefs.SetInt ("CacheServerMode", 1);
            EditorPrefs.SetString ("CacheServerIPAddress", "192.168.10.30" );
            Debug.Log("CacheServer set to 192.168.10.30");
            var readMethod = typeof(Editor).Assembly.GetType("UnityEditor.CacheServerPreferences")
                .GetMethod("ReadPreferences", BindingFlags.Static | BindingFlags.Public);
            readMethod.Invoke(null, null);