public unsafe T GetInterface <T>() where T : class, IInterface { T result = this as T; if (result != null) { return(result); } if (injectedInterfaces == null) { // If the injected interfaces haven't been set up set them up now if (objRef == null) { return(null); } UClass unrealClass = GetClass(); if (unrealClass as USharpClass != null) { // This is a C# defined type. We know if it implements the target interface or not due to the // above "this as T". There isn't any need to inject interfaces into the UObject. return(null); } FScriptArray *interfacesPtr = (FScriptArray *)Native_UClass.Get_InterfacesRef(unrealClass.Address); if (interfacesPtr->ArrayNum != 0) { injectedInterfaces = new Dictionary <Type, IInterface>(); foreach (FImplementedInterface implementedInterface in unrealClass.Interfaces) { if (implementedInterface.InterfaceClassAddress != IntPtr.Zero) { Type type = UClass.GetTypeFromClassAddress(implementedInterface.InterfaceClassAddress); if (type != null) { IInterface instance = UnrealInterfacePool.New(type, objRef); if (instance != null) { injectedInterfaces[type] = instance; if (type == typeof(T)) { result = instance as T; } } } } } } } else { // Try and get the interface from the injected interfaces. IInterface instance; injectedInterfaces.TryGetValue(typeof(T), out instance); result = instance as T; } return(result); }