PreferencesSampleExtension.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using PixiEditor.Extensions.Sdk;
  2. namespace Preferences;
  3. public class PreferencesSampleExtension : WasmExtension
  4. {
  5. /// <summary>
  6. /// This method is called when extension is loaded.
  7. /// All extensions are first loaded and then initialized. This method is called before <see cref="OnInitialized"/>.
  8. /// </summary>
  9. public override void OnLoaded()
  10. {
  11. }
  12. /// <summary>
  13. /// This method is called when extension is initialized. After this method is called, you can use Api property to access PixiEditor API.
  14. /// </summary>
  15. public override void OnInitialized()
  16. {
  17. Api.Preferences.AddCallback<int>("HelloCount", (name, value) => Api.Logger.Log($"Hello count changed to {value}!"));
  18. Api.Preferences.AddCallback<double>("TestDouble", (name, value) => Api.Logger.Log($"Test double changed to {value}!"));
  19. Api.Preferences.AddCallback<string>("TestString", (name, value) => Api.Logger.Log($"Test string changed to {value}!"));
  20. Api.Preferences.AddCallback<bool>("TestBool", (name, value) => Api.Logger.Log($"Test bool changed to {value}!"));
  21. // Internally this preference will have name "yourCompany.Samples.Preferences:HelloCount".
  22. int helloCount = Api.Preferences.GetPreference<int>("HelloCount");
  23. Api.Preferences.UpdatePreference("HelloCount", helloCount + 1);
  24. Api.Preferences.UpdatePreference("TestDouble", 3.14);
  25. Api.Preferences.UpdatePreference("TestString", "Hello, World!");
  26. Api.Preferences.UpdatePreference("TestBool", true);
  27. // This will overwrite built-in PixiEditor preference. Extension must have WriteNonOwnedPreferences permission.
  28. // Prepending "PixiEditor:" to preference name will access built-in PixiEditor preferences. If you set it to other extension unique name,
  29. // it will access extension preferences.
  30. // You can do analogous thing with UpdatePreference.
  31. Api.Preferences.UpdateLocalPreference(
  32. "PixiEditor:OverwrittenPixiEditorPreference",
  33. "This is overwritten value of preference that is built-in in PixiEditor.");
  34. // You don't need any special permission for reading any kind of preference.
  35. Api.Logger.Log(Api.Preferences.GetLocalPreference<string>("PixiEditor:OverwrittenPixiEditorPreference"));
  36. }
  37. }