Animating scroll position in OnGUI

Hi,

I’m trying to build a mini inventory item selection box popup.

I created a C# script and attached to player object. In OnGUI() function I calculated the position and then;

GUI.DrawTexture (contentSection, contentBG);

GUILayout.BeginArea (contentSection);
scrollPos = GUILayout.BeginScrollView(scrollPos, true, false, GUILayout.Width (contentSection.width), GUILayout.Height (contentSection.height));

GUILayout.BeginHorizontal ();

for (int i = 0; i < items.Count; i++) {
  GUILayout.Box (items[i].Texture);
}

GUILayout.EndHorizontal ();

GUILayout.EndScrollView ();
GUILayout.EndArea ();

And I’m handling the Input’s in Update() function;

void Update() {
  if (Input.GetKeyDown (toggleKey)) {
    ToggleInventory ();
  }

  if (Input.GetKeyDown (previousKey)) {
    Debug.Log ("left item");
    scrollPos.x = scrollPos.x - 50;
  } else if (Input.GetKeyDown (nextKey)) {
    Debug.Log ("right item");
    scrollPos.x = scrollPos.x + 50;
  }
}

I have some questions;

  1. How can I lerp or damp scrollposition? I tried Mathf.Lerp but didn’t work. (Maybe scrollpos is overridden in ongui?)
  2. And I want a nice opening & closing animation but I don’t know how to achieve that in OnGUI function.

And any suggestions are welcome, I’m a newbie, thanks.

Hi,

you are using the legacy GUI framework. Why don’t you use the new UI system (BTW: this forum is not for the legacy GUI system).
The old GUI System has a lot of problems and limitations. There is no reason why somebody would ever use the old system in my mind.

Except for the Editor obviously. But I agree for the player use the new UI.

Thanks @Hosnkobf & @_Daniel

I switched to new UI.

1 Like