using UnityEngine; public class CenteredButton : MonoBehaviour { void OnGUI() { GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Press me")) { Debug.Log("Button pressed!"); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); } }
using UnityEngine; public class ResponsiveUI : MonoBehaviour { float space = 0.0f; void OnGUI() { space = Screen.width / 8.0f; GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Hello World"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.Space(space); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Click me")) { Debug.Log("Button clicked!"); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.EndVertical(); } }This code uses GUILayout FlexibleSpace to create a responsive layout for UI elements. It calculates the available space on the screen and uses that to adjust the amount of flexible space. The code uses the GUILayout package and works with the Unity Editor.