[Unity] Bug: Fields with internal type exposed via explicit interface is always undefined
CreepGin opened this issue · comments
David commented
Pre-reading
Puer Version
2.0.5
Unity Version
2023.2
Platform
All
Error Message
No response
Bug reproduce
This pattern is used by Unity's UI Toolkit. VisualElement.style
and VisualElement.resolvedStyle
are both backed by internal types InlineStyleAccess
and ResolvedStyleAccess
. When accessing any related fields (i.e. VisualElement.style.width
or VisualElement.width
) from JS, it'll always return undefined.
Here's a minimal test case.
// C#
namespace MyNamespace {
public class FooVE {
public IFoo foo;
public FooVE() {
foo = new FooAccess();
}
}
public interface IFoo {
float width { get; }
}
internal class FooAccess : IFoo {
public float width => 1.23f;
}
}
// JS
var fooVE = new CS.MyNamespace.FooVE()
console.log(fooVE) // MyNamespace.FooVE
console.log(fooVE.width) // undefined
chexiongsheng commented
your test code is wrong.
The correct version is:
var fooVE = new CS.MyNamespace.FooVE()
console.log(fooVE) // MyNamespace.FooVE
console.log(fooVE.foo.width)
You are not using TypeScript? If you using TypeScript, compiler will point out your mistake.
David commented
Ah yes sorry, I did the minimal test case too fast (and didn't use TS). Let's fix that.
// CS
public class FooVE {
public IFoo foo;
public FooVE() {
foo = new FooAccess();
}
public float GetWidth() {
return foo.width;
}
}
public interface IFoo {
float width { get; }
}
internal class FooAccess : IFoo {
float IFoo.width => 125f; // Note the explicit interface `IFoo.`
}
public class FooTest { // Testing access from another class
public static void Test() {
var fooVE = new FooVE();
Debug.Log(fooVE.foo.width);
}
}
// JS
var fooVE = new CS.FooVE()
console.log(fooVE) // FooVE
console.log(fooVE.foo.width) // undefined
console.log(fooVE.GetWidth()) // 125
CS.FooTest.Test() // 125
David commented
Ty for the quick fix!🎉