-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove background from Unity Player #190
Comments
I don't know to make a transparent background in unity but I'm wondering if as you said,"you just want to render an object for your main-flutter application". Would that be possible? |
@TarekMedhat I already did that, but I got a complex application, where I got a Gradient background and I want to use the 3D-Model in many pages and/or Hero-Widgets. So it doesn't solve the cause problem :/. But maybe I am just a unity newbie and am missing something here? |
Hi @Ahmadre, Unity is not really the best tool for the job for you. It seems like you're looking for an 3D object viewer instead of a full embedded 3D game engine. |
@thomas-stockx I already tried every package you've linked ^^. My Model can't be rendered in these packages, because of the complex materials & shaders. Edit: None of the above packages let me transform my model partly (like opening the drivers door), but with Unity I am able to send command to my model |
@Ahmadre Actually the project that lead me to write this library had a similar request and here is how I solved this problem. I had to pass the color of my current screen as a prop to the unity game engine and changing the entire unity background dynamically and it worked like charm. No one could tell the difference between unity and flutter. Hope this helps |
If this solves your issue please close it :) |
@juicycleff sounds great! Do you have any code example or reference where i can look that up? What color do I have to change and what settings do I have to set to dynamically change the color? You can also dm me if you like: [email protected] or on twitter: @4hm4dr3 Thx :) |
Ok, solved by dynamically setting the color: public Camera cam;
void Start()
{
cam = GetComponent<Camera>();
cam.clearFlags = CameraClearFlags.SolidColor;
}
void Update()
{
}
public void SetBackgroundColor(string rgbString)
{
string[] part = rgbString.Split(',');
Color color = new Color(float.Parse(part[0]), float.Parse(part[1]), float.Parse(part[2]));
cam.backgroundColor = color;
} |
This is possible: (AvatarWidget contains the UnityWidget). Updated instructions (Android/iOS)On 6e55f08:
(Android only) 3 Add
to add this:
here
here. Modified file here. IMPORTANT - this is a temporary fix only. The flutter_unity_widget plugin itself should implement this workaround, or you should implement it by subclassing your own OverrideUnityActivity and moving the logic there. However, given #286, running from a separate activity doesn't actually seem to work. I'll wait for #286 to be resolved before providing a more permanent solution. [0] unfortunately there's a separate bug with loading native libraries in these versions of Unity, so until that's fixed, you'll need to follow the workaround @ #329 (comment)
This worked as-is for me on iOS. EDIT 1: Actually I just realized that even though the transparency works on Android, the Unity view is rendered above the entire Flutter layer (so widgets logically higher in the Z-index are rendered behind the Unity layer). This isn't a problem on iOS. So far it's either render with no transparency (and have the layers correct), or render with transparency (and have the layers incorrect). Investigating further. EDIT 2: (Partial) success! After 12 hours of butting my head against the wall, this is possible on Android too: However I only managed to hack this in by patching the Unity bytecode. Now I need to figure out a more elegant solution. EDIT 3: Ok, fixed. We need to grab the SurfaceView created by UnityPlayer and call setZOrderOnTop(false). Updated instructions above. EDIT 4: Some further weirdness on Android - when the UnityWidget is in the foreground, any changes to the background (colour/animation/opacity/etc) won't be visible until the UnityWidget is removed and re-inserted. IMG_3392.mp4and this is what it looks like on Android: device-2021-03-20-000213.mp4Given it's fine on iOS, I'm assuming it's not a Flutter compositor issue and it's a quirk of working with Android SurfaceView. I know others have mentioned rendering to a TextureView instead, though that comes with performance issues. EDIT 5: OK, so I've managed to fix this on Android too - but at a cost: device-2021-03-20-152057.mp4So this is now working, but with a considerable drop in framerate. I did this by forcing the UnityPlayer to render to a TextureView rather than a SurfaceView: if (unityPlayer != null && !reInitialize) {
callback?.onReady()
return
}
try {
Handler(Looper.getMainLooper()).post {
if (!reInitialize) {
activity.window.setFormat(PixelFormat.RGBA_8888)
unityPlayer = UnityPlayer(activity, ule)
val view = TextureView(activity);
view.setOpaque(false)
view.setSurfaceTextureListener(object : TextureView.SurfaceTextureListener{
override fun onSurfaceTextureAvailable(surface:SurfaceTexture, width:Int, height:Int) {
unityPlayer!!.displayChanged(0, Surface(surface))
}
override fun onSurfaceTextureDestroyed(surface:SurfaceTexture ) : Boolean {
return true
}
override fun onSurfaceTextureSizeChanged(surface:SurfaceTexture , width:Int, height:Int) {
}
override fun onSurfaceTextureUpdated(surface:SurfaceTexture) {
}
});
unityPlayer!!.addViewToPlayer(view, true)
... I'm not really familiar with Android rendering internals, but I'm guessing that the original problem is something to do with the Flutter compositor not playing nicely with the additional Window created with a SurfaceView (which I believe uses the system compositor), meaning that the layer is only ever rendered with the first background (i.e. "Flutter") frame from when the SurfaceView was attached. A TextureView does not create an additional Window, meaning it is composited directly by Flutter (but with the noticeable drop in performance). This seems to be an inherent limitation of TextureView, so I'm not going to play around any further. For now, I'm going to stick with the workaround - use the SurfaceView method (above), don't use an animated backgrounds, and if I need to change the background, remove the UnityWidget from the Flutter render tree first, change the background, then reinsert. I will open an issue with the Flutter engine team, though, to see if there's any way of properly rendering the background (Flutter) layers with a SurfaceView. |
The solution @nmfisher provided looks obsolete. On Unity 2020.3.32f1 even with Render over native UI checked, my background is still not transparent. Do you have any update ? |
😭We cant set background to transparent since this lib set the container background color to white by this line Line 60 in 7995993
|
I just tested @nmfisher ’s transparency instructions on a more recent setup, and it seems to be a little easier now. DemoIn this video the CircularProgressIndicator is behind Unity, and the speed slider is above Unity. There is a black flash while Unity is launching, but I assume this is a Flutter bug. (I get the same in google maps). Screen_Recording_20221030-234819.mp4Versions
Instructions(iOS only needs step 1 & 2)
(Android only from here on)
<!--
...
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- <item name="android:windowBackground">@android:color/white</item> -->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
OtherI did not have to adjust anything in unityLibrary/src/main/AndroidManifest.xml. |
@timbotimbo |
I just tried exporting with 2022.1.23f1, and got it to work following the steps above. Don't know about possible bugs in earlier versions, but it looks like it is still comaptible. |
As with the other issue we covered, I am reproducing the problem on the unity-2022.2.0b16[silicon beta] of the m1 chip device. |
Hey! Wasn't able to make it work at first with @timbotimbo instructions at first. It is important to look at your styles.xml file. As it seems, in newer Flutter versions, when creating new project, there is not just one styles.xml. One is located in the 'values' directory and other one in 'values-night'. To make this work correctly I had to delete the whole 'values-night' folder. Not only that but also watch out for the NormalTheme. If you deleted the 'values-night' folder before, now in your 'values' directory - inside style.xml file should now see: Rename the In my case I also renamed the LaunchTheme from Light to Black as well. Happy coding! (and transparency 😁) |
Based on a recent test: If you want to do this with URP, you also need to disable both |
I found that if I just applied the following to
Applying the above to the Note that i'm exporting from Unity 2022.3.22f1 with the cameras background set to black transparent. The Unity project was created using the URP demo project with the Flutter Unity package added. |
At first: thank you sooo much for all the work and super good documentation ❤️ ❤️ ❤️ :)
Second: Let's be honest, if you're a flutter developer and want to just render an object for your main-flutter application, you'll come to the problem, where you got the ugly background in your unity-player.
Example:
![Bildschirmfoto 2020-08-13 um 13 57 44](https://user-images.githubusercontent.com/18512224/90131768-f97ac300-dd6c-11ea-8036-0aa7765cb737.png)
I am googling and researching over weeks to get my background transparent, so I can use my shown car without the background.
Settings I've tried:
I also just tried to set Clear Flags to Depth only, but that leads to the ugly brown background.
So: what is the sense of Unity's Export Flutter function, when we cannot render our objects without a unity background?
I would really appreciate any help :)
The text was updated successfully, but these errors were encountered: