Page 2 of 2

Re: Unity: Do DependencyProperties / Control.SetBinding work

Posted: 01 Mar 2015, 15:52
by ai_enabled
Just catch the same issue. Commented out dependency property in code and it works, so I'm sure the issue is same.
The workaround is seems to work, but in my case, controls must be instantiated via .NET Activator (I have a dozens for controls defined in Unity prefabs as full type name and instantiate them via .NET Activator).
If you have any ideas how to fix it - please tell me!

UPD. Calling Noesis.Extend.RegisterNativeType(type) where type is type of Control to instantiate helps to resolve this issue. But there are some other cases where I instantiate controls via direct constructor call...

UPD 2. Added logging to AddNativeType() in Noesis.Extend: Debug.LogWarning("NosisGUI: Register type " + info.Type.FullName);
It seems that some (maybe all) UserControls inheritors is not registered. If I register them manually, they work as expected.

UPD 3. Registering manually all UserControl inheritors helps, but the error still appears for some other classes - inheritors of DependencyObject. Registering manually them still not helps, as the new issues appears...
For example, calling VisualTreeHelper.GetChild() throws exception "Native type is not registered".
If I register manually all (absolutely all) inheritors from BaseComponent, I get this error:
"Exception System.ApplicationException: Can't add a 'Assets.UI.Menus.MainMenu' to a 'UIElementCollection'" which is absolutely nonsense...

UPD 4. Registered manually all used by the game UserControls, DependencyObjects and BaseComponents inheritors, it seems the game is running now. So the issue is that NoesisGUI 1.2.0 doesn't registering custom classes (which had [Extend] attribute on them in 1.0.x/1.1.x) automatically and workaround is to do it manually.

Re: Unity: Do DependencyProperties / Control.SetBinding work

Posted: 02 Mar 2015, 08:00
by ai_enabled
The game seems to run stable now, but there are still some non-registered controls - when I do recursive loop from LayoutRoot to all its childrens NoesisGUI throws "Native type is not registered" for some childs (method VisualTreeHelper.GetChild()).

Re: Unity: Do DependencyProperties / Control.SetBinding work

Posted: 02 Mar 2015, 20:34
by sfernandez
I think I have a clue on what is happening thanks to all your comments.

Please try the following and let me know if solves your problems:

- Modify Assets/Plugins/NoesisGUI/Scripts/Core/NoesisExtend.cs line 985 by adding a new version of the RegisterNativeType function:
////////////////////////////////////////////////////////////////////////////////////////////////
public static IntPtr RegisterNativeType(Type type)
{
    return RegisterNativeType(type, true);
}

////////////////////////////////////////////////////////////////////////////////////////////////
public static IntPtr RegisterNativeType(Type type, bool registerDP)
{
  // ...

  if (type.GetTypeInfo().IsSubclassOf(typeof(Noesis.DependencyObject)))
  {
    if (registerDP)
    {
        RegisterDependencyProperties(type);
    }

    // ...
}
- Modify Assets/Plugins/NoesisGUI/Scripts/Proxies/DependencyPropertyExtend.cs line 12:
public static DependencyProperty Register(string propertyName, System.Type propertyType,
  System.Type ownerType, PropertyMetadata propertyMetadata)
{
  IntPtr ownerNativeType = Noesis.Extend.TryGetNativeType(ownerType);
  if (ownerNativeType == IntPtr.Zero)
  {
    // Force native type registration, but skip DP registration because we are inside
    // static constructor and DP are already being registered
    Noesis.Extend.RegisterNativeType(ownerType, false);
  }

  // ...
}
Hope this helps ;)


EDIT: There was a an error in the DependencyProperty.Register() code I posted, the the comparison should check == IntPtr.Zero instead. I fixed the code.

Re: Unity: Do DependencyProperties / Control.SetBinding work

Posted: 03 Mar 2015, 04:34
by ai_enabled
Thanks for suggestion, I've done it, but unfortunately it throws a new exception now:
Exception: System.TypeInitializationException: An exception was thrown by the type initializer for Assets.UI.Common.Controls.ScreenViewbox ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ApplicationException: Type 'Assets.UI.Common.Controls.ScreenViewbox' already registered
  at Noesis.ContentControl.Extend (System.Type type) [0x0001b] in c:\Projects\Games\AtomicTorch.GameEngine\NoesisGUI\Noesis\NoesisGUI\Scripts\Proxies\ContentControl.cs:152 

Noesis.Error.Check () (at c:/Projects/Games/AtomicTorch.GameEngine/NoesisGUI/Noesis/NoesisGUI/Scripts/Core/NoesisError.cs:21)
Noesis.UIRenderer.Noesis_CreateRenderer (IntPtr root) (at c:/Projects/Games/AtomicTorch.GameEngine/NoesisGUI/Noesis/NoesisGUI/Scripts/Core/NoesisUIRendererImports.cs:87)
Noesis.UIRenderer..ctor (Noesis.FrameworkElement content, Vector2 offscreenSize, UnityEngine.GameObject target) (at c:/Projects/Games/AtomicTorch.GameEngine/NoesisGUI/Noesis/NoesisGUI/Scripts/Core/NoesisUIRenderer.cs:61)
NoesisGUIPanel.OnEnable () (at c:/Projects/Games/AtomicTorch.GameEngine/NoesisGUI/Noesis/NoesisGUI/Scripts/NoesisGUIPanel.cs:172)
UnityEngine.Object:Instantiate(Object)
Loader:OnLevelWasLoaded() (at Assets/Scripts/Loader.cs:86)
Loader:Awake() (at Assets/Scripts/Loader.cs:46)
UPD. I've added check in RegisterNativeType() (at the beginning of method):
       {
                        // check if Native Type is already registered
		        IntPtr nativeType;
		        if (Noesis.Extend._managedTypes.TryGetValue(type, out nativeType))
		        {
			        return nativeType;
		        }
	        }
So now it throws exception:
Exception System.Exception: Unknown Unity extended type

   -------------------------stack-------------------------

  at Noesis.Error.Check () [0x00026] in c:\Projects\Games\AtomicTorch.GameEngine\NoesisGUI\Noesis\NoesisGUI\Scripts\Core\NoesisError.cs:21 

  at Noesis.DependencyProperty.Noesis_RegisterDependencyProperty_BaseComponent_ (IntPtr classType, IntPtr propertyName, IntPtr propertyType, IntPtr propertyMetadata) [0x0000b] in c:\Projects\Games\AtomicTorch.GameEngine\NoesisGUI\Noesis\NoesisGUI\Scripts\Proxies\DependencyPropertyExtend.cs:347 

  at Noesis.DependencyProperty.Register (System.String propertyName, System.Type propertyType, System.Type ownerType, Noesis.PropertyMetadata propertyMetadata) [0x00788] in c:\Projects\Games\AtomicTorch.GameEngine\NoesisGUI\Noesis\NoesisGUI\Scripts\Proxies\DependencyPropertyExtend.cs:188 

  at Assets.UI.Menus.MainMenu..cctor () [0x00000] in <filename unknown>:0 
UPD 2. It works now with modified code for DependencyPropertyExtend.Register method:
  public static DependencyProperty Register(string propertyName, System.Type propertyType,
            System.Type ownerType, PropertyMetadata propertyMetadata)
        {
			//IntPtr ownerNativeType = Noesis.Extend.TryGetNativeType(ownerType);
			//if (ownerNativeType != IntPtr.Zero)
			//{
				// Force native type registration, but skip DP registration because we are inside
				// static constructor and DP are already being registered
				Noesis.Extend.RegisterNativeType(ownerType, false);
			//}
              ...
        }
Thanks! I've removed workaround used for manual registration and use this fix.

Re: Unity: Do DependencyProperties / Control.SetBinding work

Posted: 03 Mar 2015, 10:07
by sfernandez
I feel a bit silly looking now at the code of DependencyProperty.Register I posted :shock:

The correct code is this (I already fixed it in my previous post):
public static DependencyProperty Register(string propertyName, System.Type propertyType,
  System.Type ownerType, PropertyMetadata propertyMetadata)
{
  IntPtr ownerNativeType = Noesis.Extend.TryGetNativeType(ownerType);
  if (ownerNativeType == IntPtr.Zero)
  {
    // Force native type registration, but skip DP registration because we are inside
    // static constructor and DP are already being registered
    Noesis.Extend.RegisterNativeType(ownerType, false);
  }

  // ...
} 

Re: Unity: Do DependencyProperties / Control.SetBinding work

Posted: 04 Mar 2015, 06:13
by ai_enabled
Thanks for the fixed code, now it's working properly!
Regards!