I see there is already some questions on this matter, but they are not using unity3d or are using the old UI method of unity.
I have a toggle button with a specific background and I want that background to change when the toggle is off. I tried to do this using the inspector and setting a function to apply a different texture to the button when it was off, but did not work.
Do you have any idea on how to accomplish this? I am attaching what I tried.
The background of a toggle is just an Image
so it's this simple, in a script have
public Image theBackground;
you can then change that in any way you wish!
if you wish, have a function
public Image theBackground;
public Toggle theToggle;
public void ChangeBackground()
{
if (theToggle.isOn)
... set theBackground as you wish ..
else
... set theBackground as you wish ..
}
which sets it appropriately. Look in the toggle and drag that function to the OnValueChanged.
Enjoy!
I just ran into the same problem. What I did to solve it was the following:
When you place the Toggle on the Hierarchy, you get
Toggle
Background
Checkmark
Label
What I believe the toggle script does is modify the checkmark object (to make it appear or not).
So I replaced the Checkmark components (Rect Transform and Image (Script)) with the Background components.
Basically, just Copy component > Paste component values from Background to Checkmark.
And then, switch the Image Colors for both to the desired colors. Now when you Toggle in-game, you'll see it changes colors.
I know it's been a really long time, but it might help others :)
Related
I am trying to do something like X and O game, and I want the image of button be showed if it was pressed but it's not working, I made sure that button is working by reversing the command it worked fine! Here is screenshot
I tried to reverse the command and it worked so the button is working fine
Have in mind that a UI.Button requires at least one active and enabled visual component (Image/Text/etc) with RaycastTarget enabled ... I suspect since you disabled the image you simply disabled the interactions with this button
=> solution could be to simply have two Images like e.g.
- Button (with Button + Image component)
|-- Icon (with Image component)
The Button object is your main button with a background image - can be completely transparent, but can also react to interaction and have color for hover, press etc. This you always leave enabled and here you enable the RaycastTarget.
Then in Icon you assign the according circle or cross sprite and can enable / disable it accordingly. Here you can also deactivate the RaycastTarget to save some performance. You could even have two different child icons according to the sprites then you don't need to assign the sprite on runtime either but just activate according child object
I am creating a custom editor window in Unity. I want to have a label change color when the mouse hovers over it. To accomplish this, it would seem that this should work:
GUI.skin.label.hover.textColor = Color.red;
GUILayout.Label("My Label");
But the label still displays as normal with no effect when I hover over it with the mouse.
I've also tried manually creating GUIStyles and passing them as an argument to GUILayout.Label with the same result. If I change the normal state, however, I see the color change.
Is the hover state supported for labels, and if not, what controls is it supported for and how would I find out this information? It seems absent from Unity's docs.
You should be able to use GUI.skin.button if you are only interested in changing the text color.
var myStyle = new GUIStyle(GUI.skin.button);
myStyle.hover.textColor = Color.red;
GUILayout.Label("My Label", myStyle);
However, this has the side effect of taking all of the other qualities of the button style. Your label will look like a button. In theory you could change the background image for all of the style states and make it look not like a button, but doing so for the normal state reverts the hover behavior to that of a label style. This answer in the unity Q&A seems to be the most insightful explanation for why hover does not work (at least, not usually) but normal and active do.
In short, unity has special code for built in GUI styles that have hover over effects that force a redraw when the mouse passes over them. This seems to somehow be tied up in the normal style state for certain special styles, such as GUI.skin.button. The result, there is no option to have custom hover backgrounds, and anything that does need to have a custom hover text color must have the same background image as one of the built-in button styles.
I have 2 Canvas: one for Login, one for Register.
This is Canvas of Login:
When register button is clicked, i change and call Register Canvas:
But it gets blurry. I use same Unity config in both Canvas.
I disable and enable Canvas via C# script:
void TaskOnClick()
{
CanvasLogin.enabled = false;
LoginValue.text = "";
PasswordValue.text = "";
CanvasRegister.enabled = true;
}
The problem only occurs when I enable Canvas via command/script, if Canvas start enabled, the blurry dont occur.
Scale Config:
And it's funny that during the execution if I change the aspect of the screen and return, the text backs to normal...
The problem is not with second canvas, the problem is when i enable it
via command/script
I just came across this same problem and as this was the first result when I searched I'll add my answer. I had a GameObject with Canvas and CanvasScaler components.
I found that you if you disable / enable a Canvas at runtime you need to update the CanvasScaler too. In my case I disabled / enabled the CanvasScaler also. Setting any value on the CanvasScaler would probably set it to dirty and cause update.
As your problem is only with one of the canvases, you might want to check the attached Canvas Scaler components. It seems like the scaling on the second canvas is not correctly configured.
You can just copy all of the component's values from your first to the second canvas, with the dropdown menu from the little gear icon in the upper right corner of the component in the inspector window.
The easiest way:
use gameobject.SetActive() instead of canvas.enabled
I'm trying to change the Realtime Shadow Colour, which can be seen in the Lighting window under the Scene tab.
Now, I can change it in the editor, but I seemingly can't for the life of me figure out how to change it through a script during runtime.
I have tried changing RenderSettings.subtractiveShadowColor, but turned out it wasn't the one.
Suggestions/solutions are appreciated.
You can alter that setting by using RenderSettings.subtractiveShadowColor, you simply set that value to a Color like so:
RenderSettings.subtractiveShadowColor = colourValue;
Additionally the RenderSettings class is where you'll find most of the variables that are displayed in that window as well.
in my WP7 app using silverlight controls,
I have two buttons,
I want to make when I press a button, the other button seems like it's also being pressed (change of color to invert...etc)
something like the native keyboard on wp7 phones when the larger button appear above your thumb
I tried to give both of them the focus, but it's not possible
-Detailed Explanation-
usually, when you press a button, the button style changes according to the theme,
ex: if theme is dark, the button becomes white and the text becomes black. but if the theme is white, the button becomes black when clicked.
My aim is to mimic that behavior to a button when another button is pressed.
In the click event you could use
VisualStateManager.GoToState(Button1, "Pressed", true);
however to go back to the Normal state you would have to know when the click is over. LeftMouseButtonUp does not work because there is no mouse.
So you could set a timer and revert the state. Ugly but it works.
On a side note: I suspect the design you might want to use a different way of getting the result you need.
I haven't done much wp7 development but I believe the normal, active and pressed appearances are implemented as different states of the button and you can programatically set the state. This article should explain it in a bit more detail but the advantage of this approach over manually setting background colours is that it should automatically take advantages of any transitions configured between those states, as well as the black => white, or white => black theme related changes will be done for you too.
http://www.timmykokke.com/2010/11/using-visual-states-in-custom-controls-in-silverlight/
Update:
In the click handler of one set the background property of the other.
In pseudo-code:
Button button1
ToggleButton button2
Button1.MouseDown:
Set button2 pressed state to true
Button2.MouseUp:
Set button2 pressed state to false
try using MS Expression Blend to customize the behavior, details