Skip to content

Instantly share code, notes, and snippets.

@Sujimichi
Created October 5, 2016 16:12
Show Gist options
  • Save Sujimichi/23cf077d89bc15acdc3c889cd39ac638 to your computer and use it in GitHub Desktop.
Save Sujimichi/23cf077d89bc15acdc3c889cd39ac638 to your computer and use it in GitHub Desktop.
KSP toolbar setup not working as expected
using System;
using UnityEngine;
using KSP.UI.Screens;
namespace ToolBarTest
{
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class ToolBarTest : MonoBehaviour
{
public static ApplicationLauncherButton button_1;
public static ApplicationLauncherButton button_2;
Texture2D test_texture_1 = new Texture2D(1, 1, TextureFormat.RGBA32, false);
Texture2D test_texture_2 = new Texture2D(1, 1, TextureFormat.RGBA32, false);
private void Awake(){
test_texture_1.SetPixel(0, 0, Color.blue);
test_texture_1.Apply();
test_texture_2.SetPixel(0, 0, Color.red);
test_texture_2.Apply();
Debug.Log ("Adding App launcher event callbacks");
GameEvents.onGUIApplicationLauncherReady.Add (this.app_launcher_ready);
GameEvents.onGUIApplicationLauncherDestroyed.Add (this.app_launcher_destroyed);
}
public void app_launcher_ready(){
GameEvents.onGUIApplicationLauncherReady.Remove (this.app_launcher_ready);
Debug.Log ("app launcher is ready");
add_to_toolbar ();
}
public void app_launcher_destroyed(){
Debug.Log ("app launcher destroyed");
remove_from_toolbar();
}
public void add_to_toolbar(){
Debug.Log ("Adding buttons to toolbar");
ToolBarTest.button_1 = ApplicationLauncher.Instance.AddModApplication (
button_1_action, button_1_action,
null, null,
null, null,
ApplicationLauncher.AppScenes.VAB | ApplicationLauncher.AppScenes.SPH,
test_texture_1
);
ToolBarTest.button_2 = ApplicationLauncher.Instance.AddModApplication (
button_2_action, button_2_action,
null, null,
null, null,
ApplicationLauncher.AppScenes.SPACECENTER,
test_texture_2
);
}
public void remove_from_toolbar(){
Debug.Log ("removing button from toolbar");
ApplicationLauncher.Instance.RemoveModApplication (button_1);
ApplicationLauncher.Instance.RemoveModApplication (button_2);
}
public void button_1_action(){
Debug.Log ("button 1: Oi! someone clicked me.");
}
public void button_2_action(){
Debug.Log ("button 2: quit poking me.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment