tattyd
June 26, 2022, 1:55am
1
Hey all,
Like many others, I was in need of a soft shadow for some of my elements in UI Toolkit. I decided this was a good time to learn about OnGenerateVisualContent
Here’s my Soft Shadows component:
Shadow.cs
/* MIT License
Copyright (c) 2022 David Tattersall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
This file has been truncated. show original
Usage:
Wrap whatever VisualElement should be shadowed with a component.
Control shadow colour using the USS color style property (shows as ‘text color’ in the editor). This allows you to do fun mouseover effects, as well as transitions:
Shadow {
transition: 250ms;
}
Shadow:hover {
color: white;
}
Attributes:
shadow-corner-radius controls the corner radius, although in practice it feels more like controlling the blurryness.
shadow-scale allows you to scale the shadow up/down to alter how far it renders from the contents.
shadow-offset-x and shadow-offset-y allow you to move the shadow around, e.g. to simulate light coming from a specific direction.
Hope it’s useful to somebody!
Sample:
34 Likes
Neiyra
October 16, 2022, 11:19pm
3
Thanks, that great! I´m gonna test it immediately.
This does work well, though for anyone looking at this, it does not conform to the shape of the texture, so if you just want a circular or squared background effect, this can work very well!
That is really nice amaizing
but i am not able to controll attributes in uss but i can in the inspector
i tryied using class id and dirrect element but it didn’t work in uss
I am new to unity and ui toolkit is like an alien to me. what does this mean? “wrap around” do i have to go in uxml file or some other thing
1 Like
Deeje
February 7, 2024, 12:14am
8
Yes, open your .USS file with the button at the bottom of the UI Builder window. Find the VisualElement you want to have the shadow, and just add before and after. Then you can adjust the shadow parameters inside the UI builder.
1 Like
@tattyd Thanks for sharing this custom component! Do you think it would be easy to create a shadow for a circular button?
Hi, I made some modifications for this script and want to share it.
I made better transition and inner shadow.
I made a comment for the original post with more details -
Shadow.cs
/* MIT License
Copyright (c) 2022 David Tattersall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
This file has been truncated. show original
My changes -
/* MIT License
Copyright (c) 2022 David Tattersall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
This file has been truncated. show original
2 Likes
Will it work for rounded objects
Yes, you can set radius and radius transition. Just use same radius for inner shadow. In example, it has wide radius for inner shadow than for outer. I’ve made new version what uses USS instead. In original, you need to provide it in constructor.
If you’re asking if it will look good on other visual elements, I don’t tried. But techily, you can use it on any visual element.
/* MIT License
Copyright (c) 2022 David Tattersall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
This file has been truncated. show original
1 Like
Incase anyone wants this version, I’ve uploaded a gist. This is supposed to also allow it to act as a “Glow”. I made some changes including:
No background. I wanted to use this on semitransparent stuff.
Constant scaling. Not relative. I wanted to use this on long objects.
.glow {
color: var(--color-light-blue-primary);
--shadow-transition: 1;
--shadow-corner-radius: 20;
--shadow-offset-x: 0;
--shadow-offset-y: 0;
--shadow-spread: 10;
--outer-opacity: 0;
--inner-opacity: .05;
}
ShadowVisualElement.cs
/* MIT License
Copyright (c) 2022 David Tattersall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
This file has been truncated. show original
2 Likes