HebaruSan Posted March 16, 2021 Share Posted March 16, 2021 (edited) Currently I'm drawing these in the map view with three LineRenderers and a GUI.Button: 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 September 17, 2021 by HebaruSan Link to comment Share on other sites More sharing options...
Gotmachine Posted March 23, 2021 Share Posted March 23, 2021 Might not be that, but make sure you set the layer on every gameobject. Also, from memory, you might need layer 5, not 9. Link to comment Share on other sites More sharing options...
HebaruSan Posted March 23, 2021 Author Share Posted March 23, 2021 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? 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 More sharing options...
mahanako Posted August 4, 2021 Share Posted August 4, 2021 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 More sharing options...
zer0Kerbal Posted September 16, 2021 Share Posted September 16, 2021 I want to do something similar on the flight scene - just one word.... Link to comment Share on other sites More sharing options...
mahanako Posted September 19, 2021 Share Posted September 19, 2021 I actually did this for the flight scene, more specifically for IVA. Link to comment Share on other sites More sharing options...
HebaruSan Posted September 19, 2021 Author Share Posted September 19, 2021 8 hours ago, mahanako said: I actually did this for the flight scene, more specifically for IVA. If your code also works in the map view, I'd be interested to see how you did it. Link to comment Share on other sites More sharing options...
mahanako Posted September 19, 2021 Share Posted September 19, 2021 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 More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now