ReactUnity/core

`<view>` GameObject is not removed after Restart

Muchaszewski opened this issue · 6 comments

NOTE: The error, in this case, does not matter, but I will include it at the end because it might provide a hint for debugging.

Every time an error occurs the <view> GameObject is not removed from the scene view when restarting. This also is happening when auto restart/hot reload mechanism triggers.

On an unrelated note should not it be part of the Canvas view for easier management?

Video.mp4

The view is part of an error
image

[2022-11-29 17:40:28.110] [Error] The above error occurred in the <App> component:

    at App (<input>:16)
    at GlobalsProvider (<input>:54)
    at ErrorBoundary (<input>:47)
    at DefaultView (<input>:12)

React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.
 at ReactUnity.Scripting.DomProxies.ConsoleProxy:GenericLog (object,System.Action`1<string>,object[]) (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scripting/DomProxies/Console.cs:40)
ReactUnity.Scripting.DomProxies.ConsoleProxy:error (object,object[]) (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scripting/DomProxies/Console.cs:65)
System.Reflection.MonoMethod:Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
System.Reflection.MethodBase:Invoke (object,object[])
QuickJS.Binding.DynamicVariadicMethod:Invoke (QuickJS.Native.JSContext,QuickJS.Native.JSValue,int,QuickJS.Native.JSValue[]) (at Library/PackageCache/com.reactunity.quickjs@0.13.0/Runtime/Source/Binding/DynamicMethod.cs:366)
QuickJS.Utils.TypeDB:_DynamicMethodInvoke (QuickJS.Native.JSContext,QuickJS.Native.JSValue,int,QuickJS.Native.JSValue[],int) (at Library/PackageCache/com.reactunity.quickjs@0.13.0/Runtime/Source/Utils/TypeDB.cs:428)
QuickJS.ScriptFunction:_Invoke (QuickJS.Native.JSContext,int,QuickJS.Native.JSValue*) (at Library/PackageCache/com.reactunity.quickjs@0.13.0/Runtime/Source/ScriptFunction.cs:207)
QuickJS.ScriptFunction:Invoke (System.Type,object[]) (at Library/PackageCache/com.reactunity.quickjs@0.13.0/Runtime/Source/ScriptFunction.cs:190)
QuickJS.ScriptFunction:Invoke<object> (object[]) (at Library/PackageCache/com.reactunity.quickjs@0.13.0/Runtime/Source/ScriptFunction.cs:173)
ReactUnity.Helpers.Callback:Call (object[]) (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Helpers/Callback.cs:131)
ReactUnity.Helpers.Callback:Call () (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Helpers/Callback.cs:63)
ReactUnity.Scheduling.DefaultScheduler/<>c__DisplayClass7_0:<setImmediate>b__0 () (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scheduling/DefaultScheduler.cs:41)
ReactUnity.Scheduling.BaseDispatcher`1/<OnUpdateCoroutine>d__23<UnityEngine.Coroutine>:MoveNext () (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scheduling/BaseDispatcher.cs:168)
ReactUnity.Scheduling.BaseDispatcher`1<UnityEngine.Coroutine>:StartAndStopDeferreds (bool) (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scheduling/BaseDispatcher.cs:136)
ReactUnity.Scheduling.BaseDispatcher`1<UnityEngine.Coroutine>:Update () (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scheduling/BaseDispatcher.cs:199)
ReactUnity.Scheduling.RuntimeDispatcherBehavior:Update () (at Library/PackageCache/com.reactunity.core@712c19ae9f/Runtime/Scheduling/RuntimeDispatcher.cs:28)

The same <view> component is part of Canvas
image

Unity doesn't have a way to create objects but not place them in the scene. Ideally, we could place all newly created objects in [Offscreen], but I guess that would affect performance just by changing an object's parent. Alternative way would be to remove every object individually after canvas is destroyed, but that would also cost performance.

In an ideal case, there shouldn't be an error in the React rendering. So I don't want to implement this in a way that would affect performance much.

Unity doesn't have a way to create objects but not place them in the scene.

I believe you are correct in saying this, but can't you disable the objects to "fake" them not being there?

Alternative way would be to remove every object individually after canvas is destroyed, but that would also cost performance.

It might depend on how the canvas is destroyed, but if views are children of a specific canvas and you remove the canvas, I would expect it to remove the children as well, even if it would affect performance or not, because it needs to clean up after itself.

@Thundernerd Thanks for your suggestions.

Normally, when a view is created it is placed under the canvas. But in this case, an error happens before they are placed under the canvas. That is the root cause of this issue. So your second suggestion would not work.

Disabling and enabling objects can affect performance, even if it would be minor. Also objects would still be there, so I don't think that is a solution as well.

I have some plans to fix this, but haven't started working on it yet.

I misunderstood the issue first. I can see the issue after trying it again.

It may be caused by a bug in React. I reported it here: facebook/react#26518

Let's see what React devs say about it. If it doesn't get fixed in React, I will fix it with a workaround in ReactUnity.

Applied the workaround in 0.15.2. This is still a bug in React side. But shouldn't be a problem in ReactUnity anymore.