Hi,
I’m wondering if its possible to pass parameters to an existing object, rather than when the object is instantiated?
For instance, if I want to instantiate a Toggle with the GUILayout style parameter set to “Button”, I can do this:
using UnityEngine;
public class ExampleClass : MonoBehaviour {
private bool pressed = true;
OnGUI() {
pressed = GUILayout.Toggle(pressed, “Toggle me !”, “Button”);
}
}
Is it possible instead to just set the “Button” style parameter for an existing button? So I have an existing button, then I attach a script to it that goes something like this?
gameObject.style = “Button”;
Cheers
Expose a GUIStyle member
public class Example : MonoBehaviour
{
[SerializeField]
GUIStyle style;
void OnGUI()
{
GUILayout.Toggle(pressed, "Toggle", style);
}
}
Or use a GUISkin and skin.GetStyle()
Thanks KelsoMRK.
This is strange. If I use the code I pasted I can set the style to “Button” on creation.
However, if I use your code, I can see it makes all the “style” parameters available in the inspector, but the option for “Button” does not exist??
I also tried setting it to “Button” like this:
But also this is not working…
Very strange. Is the “Button” style parameter missing or hidden, or something??
Also, strangely, when I use my original code now the “Toggle” just show some red text - whereas before it was showing a “Button” (as a toggle)???
It’s exposing a single style, not the entire skin.
If you want to use the Button style for whatever skin you’re using then you can do this
GUILayout.Toggle(pressed, "Toggle", GUI.skin.GetStyle("Button"));
Thanks for this. I’m still getting the “red” “Toggle” text instead of using the “Button” style from the default GUISkin. Has the default skin changed somehow? Also, this red “Toggle” text seems to be appearing above the existing Toggle object - which seems to show that this script is now creating a new Toggle object, instead of editing the existing one?
Sounds like you’ve got two of the same script in your scene.
Ive checked, the script is only included once. I started a new project and now I dont get the red text. However, as shown in the image link, there are now 2 toggles. The one in the left hand corner seems like it is just creating a button, with the word “Toggle” in the text. When I click this, it doesnt stay clicked (like a toggle does).
Basically what Im trying to do, is edit the value of an existing “Toggle” to make it’s style look just like a button, but it still behaves like a Toggle (e.g. On/Off function).
It seems like your code is just creating a new “Button” object with the word “Toggle” in the text. If I remove the “Toggle” object and attach the script to the “Panel” object, I can see that a new “Button” object is created when I run the game…
You’re mixing old UI and new UI. That’s why you’re seeing 2 elements.
Hi KelsoMRK, any idea what’s the difference between old and new UI??
Also, any idea why Toggle is acting like a button?
The difference is…everything. OnGUI isn’t used by the new UI system at all. Your toggle is acting like a button because you gave it the style of a button.
You’ve basically used OnGUI to create a legacy UI toggle that looks like a button and then separately created a new UI toggle.
I’d suggest checking out the Learn section. There’s a series of tutorial videos there about the new UI.
Is this not possible within the new UI system? To change the properties of a toggle so it looks like a button?
You can build a button that acts like a toggle. Just have the click event flip some internal boolean field.
Yes, I see that I can determine on/off for the clicks that are happening from the button. My main aim though is to get the button to visually behave like it is being toggled. ie for the “On” state of the button to prevail until the button is clicked off. Any idea how to do this?
Make an “on” sprite and an “off” sprite. Swap out the sprites based on the boolean field.
Or make them child Image elements that you turn off and on.