Jump to content

Showing a Kerbal portrait in a GUI.Window


Recommended Posts

Hi,

How can I create a kerbal portrait to show in a window ?

(portraits like in tutorials/scenarios or at the beginning of a new game)

I want a Wernher portrait and I want to be able to play emotes.

I've tried using KerbalInstructor but I can't find a way to make it work.

→
Go to Answer

Edited by simon56modder
Link to comment
Share on other sites

This should get you there:

7c58c30317.png

[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class WindowInstructor : MonoBehaviour
{
private const int PortraitWidth = 128;
private const int PortraitHeight = 128;

private KerbalInstructor _instructor;
private RenderTexture _portrait;
private Rect _windowRect = new Rect(250f, 250f, 128f, 128f);
private Dictionary<GUIContent, CharacterAnimationState> _responses;
private int _selectedResponse = 0;

private void Start()
{
Resources.FindObjectsOfTypeAll<KerbalInstructor>()
.ToList()
.ForEach(instructor => print("Instructor: " + instructor.CharacterName + ", prefab: " + instructor.name));

_instructor = Create("Instructor_Gene");

}


private void OnDestroy()
{
if (_portrait != null)
_portrait.Release();

if (_instructor != null)
Destroy(_instructor.gameObject);
}


private void OnGUI()
{
_windowRect = KSPUtil.ClampRectToScreen(GUILayout.Window(GetInstanceID(), _windowRect, DrawWindow, "Window Title",
HighLogic.Skin.window));
}


private void DrawWindow(int winid)
{
GUI.skin = HighLogic.Skin;

GUILayout.BeginHorizontal();
{
GUILayout.FlexibleSpace(); // to center portrait in case window stretches beyond portrait width
GUILayoutUtility.GetRect(PortraitWidth, PortraitHeight);

if (Event.current.type == EventType.Repaint)
Graphics.DrawTexture(GUILayoutUtility.GetLastRect(), _portrait);

GUILayout.FlexibleSpace();
}
GUILayout.EndHorizontal();

_selectedResponse = GUILayout.SelectionGrid(_selectedResponse, _responses.Keys.ToArray(), 4);

if (GUI.changed)
_instructor.PlayEmoteRepeating(_responses[_responses.Keys.ToArray()[_selectedResponse]], 1f);

GUI.DragWindow();
}


private KerbalInstructor Create(string instructorName)
{
var prefab = AssetBase.GetPrefab(instructorName);
if (prefab == null)
throw new ArgumentException("Could not find instructor named '" + instructorName + "'");

var prefabInstance = (GameObject)Instantiate(prefab);
var instructor = prefabInstance.GetComponent<KerbalInstructor>();

_portrait = new RenderTexture(PortraitWidth, PortraitWidth, 8);
instructor.instructorCamera.targetTexture = _portrait;

_responses = instructor.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(fi => fi.FieldType == typeof (CharacterAnimationState))
.Where(fi => fi.GetValue(instructor) != null)
.ToDictionary(fi => new GUIContent(fi.Name), fi => fi.GetValue(instructor) as CharacterAnimationState);

return instructor;
}
}

Link to comment
Share on other sites

This should get you there:

I have issues with System.Reflection :


[ERR 16:10:03.151] AssemblyLoader: Exception loading 'ResearchBodies': System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
at AssemblyLoader.LoadAssemblies () [0x00000] in <filename unknown>:0
Additional information about this exception:
System.TypeLoadException: Could not load type 'ResearchBodies.WindowInstructor' from assembly 'ResearchBodies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
System.TypeLoadException: Could not load type '<>c__DisplayClass8' from assembly 'ResearchBodies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Edited by simon56modder
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...