Esempio n. 1
0
        public async Task <UIElement> GetUIAsync([EngineNS.Editor.Editor_RNameMacrossType(typeof(EngineNS.UISystem.UIElement))] RName name,
                                                 string keyName = "__default", bool forceLoad = false)
        {
            if (name == null || name.IsExtension(CEngineDesc.UIExtension) == false)
            {
                return(null);
            }
            if (name.RNameType != RName.enRNameType.Game)
            {
                throw new InvalidOperationException("暂不支持非游戏UI");
            }
            if (string.IsNullOrEmpty(keyName))
            {
                keyName = "__default";
            }
            var       rc  = CEngine.Instance.RenderContext;
            var       key = new CKeyName(name, keyName);
            UIElement result;

            bool bFound = true;

            lock (UserUIs)
            {
                if (UserUIs.TryGetValue(key, out result) == false)
                {
                    var nameSpace  = name.RelativePath().Replace("/", ".");
                    var typeName   = nameSpace + name.PureName();
                    var resultType = EngineNS.CEngine.Instance.MacrossDataManager.MacrossScripAssembly.GetType(typeName);
                    if (resultType == null)
                    {
                        return(null);
                    }
                    result = System.Activator.CreateInstance(resultType) as UIElement;
                    bFound = false;
                    UserUIs.Add(key, result);
                }
            }

            if (bFound)
            {
                if (forceLoad)
                {
                    var atts     = result.GetType().GetCustomAttributes(typeof(Editor_UIControlInitAttribute), false);
                    var att      = atts[0] as Editor_UIControlInitAttribute;
                    var initType = att.InitializerType;
                    var init     = System.Activator.CreateInstance(initType) as UIElementInitializer;
                    await result.Initialize(rc, init);
                }
            }
            else
            {
                var atts     = result.GetType().GetCustomAttributes(typeof(Editor_UIControlInitAttribute), false);
                var att      = atts[0] as Editor_UIControlInitAttribute;
                var initType = att.InitializerType;
                var init     = System.Activator.CreateInstance(initType) as UIElementInitializer;
                await result.Initialize(rc, init);
            }
            return(result);
        }