Jump to content

[Resolved (more or less)] How to resize the KSPedia ?


Recommended Posts

Hi

I play on a triple monitor setup and a part of the KSPedia get hidden behind the screen's bezel, so I'm trying to write a plugin for moving and resizing it.

Moving KSPedia around is easy :

KSPAssets.KSPedia.KSPedia.Instance.GetComponent<RectTransform> ().position = new Vector3 (x, y, z);

But I can't figure out how to resize it, I don't want to scale down the whole KSPedia window, just the "content", not the hierarchy menu neither the buttons on top. My best attempt was : 

KSPAssets.KSPedia.KSPedia.Instance.GetComponent<KSPAssets.KSPedia.KSPediaAspectController> ().desiredContentAspect = // float value

It works, the content is scaled, the window gets its width updated to fit the content but the window's height remains the same, see screenshot below (desiredContentAspect = .5f) :

Spoiler

ZgMNpZK.png

I'm pretty sure what I need is in the RectTransform class, I've read the docu on Unity's website, search google etc. All answers are about modifying RectTransform.sizeDelta but when I do it on KSPedia's RectTransform nothing changed. I'd really appreciate some help with that.

Ping @DMagic Lord of the new UI system and Protector of the KSPedia.

Edited by Li0n
Link to comment
Share on other sites

If you just want to change the size of the window you can try changing its scale. 

Just use Debug Stuff to figure out the hierarchy of KSPedia and change the localscale of the transform.

Changing the width or height might not work correctly since this is probably using the layout system, so the content of the window forces it into a certain height and width. Again, Debug Stuff would help here, as it would let you know about the presence of any Content Size Fitters or Layout Elements. If they are there then it may be possible to edit some of those layout elements to get the entire window to shift to a different size.

Link to comment
Share on other sites

  • 2 weeks later...

@DMagic I've tried 5 times to answer your comment, each time I've re-read it and thought about something I haven't try so thanks for the hints :wink:

As you suggest I used "Debug Stuff" to figure out what was the kspedia's hierarchy and what layout component was there, here it is :

Quote

- DialogCanvas
  |- KSPedia(Clone)     (VerticalLayoutGroup) (KSPediaAspectController)
     |- TopBars     (LayoutElement) (HorizontalLayoutGroup)
     |  |- ContentsTopBar     (LayoutElement)
     |  |  |- Button_back
     |  |  |- Button_forward
     |  |  |- PageTitleText
     |  |     |- Image
     |  |
     |  |- ContentsTopBar     (LayoutElement)
     |     |- PageTitleText
     |     |- Button_exit
     |     |- Button_pause
     |
     |- Panels     (LayoutElement) (HorizontalLayoutGroup)
        |- Contents Scroll View     (LayoutElement)
        |  |- Viewport
        |     |- Content     (VerticalLayoutGroup) (ContentSizeFitter)
        |
        |- Scrollbar
        |  |- Sliding Area
        |     |- Handle
        |
        |- Panel Scroll View     (LayoutElement)
           |- Viewport
           |  |- Content
           |
           |- Scrollbar
              |- Sliding Area
                 |- Handle

 

Tweaking those don't get me anywhere though. So I've tried to add a ContentSizeFitter to KSPedia(Clone) in order to make it resize to whatever I wanted, that kinda work but the bluish background won't resize horizontally and don't re-center (the left border of the kspedia stays at the same x position). I couldn't find out how to make it resize so I add a GameObject with a RectMask2D component to mask this ugly remains of the background.

Last I struggle a little to find the correct math to move the resized KSPedia under the mask, for whatever reason the "real" size of the KSPedia isn't available, I think it is in KSPAsset.KSPedia.KSPediaAspectController.rt.sizeDelta but it's a private member. Thanks to an on-screen ruler I've figured out what was the math to get the width of the kspedia from the height of the screen.

Next step will be to add buttons to move and resize it dynamically. :)

Edited by Li0n
Link to comment
Share on other sites

It looks like the KSPediaAspectController is doing something similar to the ContentSizeFitter, it implements the same interfaces.

You could try playing around with some of the values for that, the desiredContentAspect, the AspectMode, or maybe the borders. I think the public SetLayout methods should recalculate the layout (it probably isn't constantly updated, this is probably why you are able to change the layout by adding a ContentSizeFitter after the KSPediaAspectController has done its thing).

One of the nice things about the new UI system is that there aren't really any private fields when it comes to Unity objects. If a given field or reference is private you can always just access it the object with GetComponent or GetComponentInChildren (or something silly like FindObjectsOfType when it's really hard to get to). In the case of the RectTransform you don't even need that. You can just take the standard transform reference from any Unity GameObject and convert it to a RectTransform. Anything with a UI component attached to it requires a RectTransform, which takes the place of the standard Transform.

Link to comment
Share on other sites

10 hours ago, DMagic said:

You could try playing around with some of the values for that, the desiredContentAspect, the AspectMode, or maybe the borders. I think the public SetLayout methods should recalculate the layout (it probably isn't constantly updated, this is probably why you are able to change the layout by adding a ContentSizeFitter after the KSPediaAspectController has done its thing).

I did play with those :

  • desiredContentAspect : it's a ratio (width/height) for the kspedia window, changing it do the trick but I couldn't figure out the math to make it behave how I wanted. Actually I may have the math right now, see below
  • AspectMode : no idea how it is supposed to be used, I try every settings, multiple time. It either does nothing or the kspedia window collapse, leaving only the top bar. Don't remember which settings does what though.
  • borders : they are the size of each of the border window, not sure what I can do with them
  • SetLayout() : I've tried several time to call those, after changing kspedia.RectTransform.sizeDelta, or the AspectMode (and probably others I forgot about) but it has never done anything
10 hours ago, DMagic said:

One of the nice things about the new UI system is that there aren't really any private fields when it comes to Unity objects. If a given field or reference is private you can always just access it the object with GetComponent or GetComponentInChildren (or something silly like FindObjectsOfType when it's really hard to get to). In the case of the RectTransform you don't even need that. You can just take the standard transform reference from any Unity GameObject and convert it to a RectTransform. Anything with a UI component attached to it requires a RectTransform, which takes the place of the standard Transform.

That's what I was thinking but I couldn't find any RectTransform with the "real" size of the kspedia window. The parent of KSPedia(Clone), DialogCanvas, has its sizeDelta equal to my screen's resolution. KSPedia(Clone)'s RectTransform.sizeDelta is equal to 880*528, no matter what the resolution of the screen is (and therefor the size of the "visible" kspedia).

In order to find the "real/visible" size of the kspedia window I came up with this formula :

float visibleHeight = Screen.height - KSPediaAspectController.borderTop - KSPediaAspectController.screenBorder;

float visibleWidth = visibleHeight * KSPediaAspectController.desiredContentAspect + KSPediaAspectController.borderLeft + KSPediaAspectController.borderRight;

For info here is how I checked the size of the different RectTransform :

foreach (RectTransform rt in KSPAssets.KSPedia.KSPedia.Instance.GetComponentsInChildren<RectTransform> ()) {
				Debug.Log ("SizeDelta of " + rt.name + " is : " + rt.sizeDelta.ToString ());
			}

None of which was the size of the "visible" kspedia window.

 

So, what I could do differently is reversing the formula above in order to input the height/width I want and calculate the resulting desiredContentAspect. That may work.

Sorry if I missed something in my explanation, I really tried a lot of things and don't remember all of them (my source file has something like 1000 lines of "try and missed code").

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