Jump to content

How to add a UnityEngine.UI.Text to map view?


Recommended Posts

Currently I'm drawing these in the map view with three LineRenderers and a GUI.Button:

marker.png

I'd like to replace the latter with UnityEngine.UI.Button and UnityEngine.UI.Text, but I'm struggling to get started. Here's what I have so far from studying the Unity documentation, tutorials, Unity forum posts, and code from other mods (most relevantly kOS's VectorRenderer). The first function is called once in my MonoBehaviour's Start(), the second is called later in the camera's OnPreCull:

private void mkButton()
{
	buttonGameObj.layer = 9;
	rect     = buttonGameObj.AddComponent<RectTransform>();
	canvas   = buttonGameObj.AddComponent<Canvas>();
	scaler   = buttonGameObj.AddComponent<CanvasScaler>();
	renderer = buttonGameObj.AddComponent<CanvasRenderer>();
	text     = buttonGameObj.AddComponent<Text>();
	button   = buttonGameObj.AddComponent<Button>();

	text.font = UISkinManager.defaultSkin.label.font;
	text.fontSize = 20;
	text.resizeTextMinSize = 20;
	text.supportRichText = true;
	text.alignment = TextAnchor.MiddleCenter;
	text.text = "TEST";

	button.onClick.AddListener(() => EditMe?.Invoke(myNode));
}

private void UpdateButton(CelestialBody parent, Vector3d direction, float camDist, Color color)
{
	rect.SetParent(parent.bodyTransform, false);
	rect.position = ScaledSpace.LocalToScaledSpace(parent.position + atSOIlimit(parent, direction));
	rect.localRotation = PlanetariumCamera.Camera.transform.rotation;
	rect.localScale = Vector3.one * camDist;

	text.text = myNode.GetCaption(vessel);
	text.color = color;
	text.enabled = true;

	button.enabled = true;
	buttonGameObj.SetActive(true);
}

As far as I have been able to determine, this draws nothing whatsoever; I have several such markers in my save and all of the rings are still visible, but I've panned all over the map view hoping to find some trace of text somewhere at some weird angle and found nothing.

I've tinkered with this a lot; with and without the canvas components, many different values for position and rotation and scale, etc., to no avail. So I don't need guesses or speculation; I suppose what I'm hoping for is either help with catching a bona fide error in setting up these components, or an example of working code that I can learn from.

So, does anyone know how to use UnityEngine.UI.Text in world space?

Edited by HebaruSan
Link to comment
Share on other sites

29 minutes ago, Gotmachine said:

Might not be that, but make sure you set the layer on every gameobject. Also, from memory, you might need layer 5, not 9.

Guess what layer these babies are on? :sticktongue:

marker.png

Thanks for the response, but 

On 3/16/2021 at 12:35 AM, HebaruSan said:

I've tinkered with this a lot; with and without the canvas components, many different values for position and rotation and scale, etc., to no avail. So I don't need guesses or speculation

Link to comment
Share on other sites

  • 4 months later...

Hi HebaruSan,

after I looked through various projects, I have found this in MOARdV's Avionics Systems project and it worked in my case drawing text floating in the cockpit. The InternalText class seems to be the KSP equivalent of the UnityEngine.UI.Text. Does this help?

InternalText t;

...

t = InternalComponents.Instance.CreateText(
        font, // eg. "Arial"
        fontSize, // you need to guess, but in IVA 0.15 gave reasonable size
        transform, // 
        text, // the actual content string
        color, // eg. Color.White
        wrap, // true or false
        align); // eg. "TopLeft"
        

Link to comment
Share on other sites

  • 1 month later...
2 hours ago, HebaruSan said:

If your code also works in the map view, I'd be interested to see how you did it.

I'll give it a shot and let you know, as soon as I find time. But there is this "Internal" prefix of InternalText and InternalComponents that bothers me a bit. I guess "Internal" means "inside the cockpit". I should have noticed this earlier... Maybe there are other components, similar to InternalComponents, that can create text objects as well. Sorry for guessing and speculating.

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...