I want to give a border to all the buttons of settings that are enabled in my options menu.

Let's say that the demo mode is enabled, then the according "on" button should have an e.g. red border, while the "off" button hasn't got one. I don't care if the border is visible when you hover over the button (= the "highlighted" color is used) as long as the actual color change for highlighting/pressing the button still works.

So far I've only found the "Outline" effect but it only seems to work on the text component of a button, not the actual button itself. How do you do this in Unity (2017.3.1f1, preferably without using additional assets)?

  • \$\begingroup\$ Conventionally, you'd use a sprite that has a border drawn into it. Is there a particular reason you don't want to create assets for this? \$\endgroup\$ – DMGregory Apr 18 '18 at 14:28
  • \$\begingroup\$ I'm using the standard UISprite and the settings in the inspector to change the color. There are about 10 different colors, so I'd have to create a new sprite for each one to not have the color change in the inspector also mess up the border. Plus, to be honest, I'm pretty bad at the graphical stuff. \$\endgroup\$ – Neph Apr 18 '18 at 14:48
  • \$\begingroup\$ What I tend to do, and I think what DMGregory was suggesting, is to create a separate 'border' sprite that can match the button shape. Then you place that on a separate GameObject under the button in the scene hierarchy, and make it active/inactive as needed. If the border is white (or greyscale), you can use the color parameter to tint it as you need. Thus, you only need one additional asset (per button shape, generally), and if you can create the button, the border should be just as easy (or hard). \$\endgroup\$ – Chris Mills-Price Apr 18 '18 at 20:25
  • \$\begingroup\$ @ChrisMills-Price Ah, I see. I'm pretty bad at creating graphical stuff though and I'm not sure if you can actually get the png file of the default UISprite to invert it, so I'd really prefer adding a built in border programmatically. Is it not possible to use the "Outline" effect for buttons somehow, not just their text? \$\endgroup\$ – Neph Apr 19 '18 at 8:25
  • 1
    \$\begingroup\$ I just found (half of) a way: "myGameObject.AddComponent<Outline>()" or "Component - UI - Effect" directly in Unity. "effectDistance" sets the width (Vector2D), "effectColor" sets the color and you can enable/disable it with "enabled". Unfortunately it uses some kind of multiplied color, so while a red border on a white button is actually red, a red border on a black button is simply black. \$\endgroup\$ – Neph Apr 19 '18 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.