Пример #1
0
        private void OnEnable()
        {
            UINodeBase uiNode = target as UINodeBase;

            if (uiNode.actionComponents == null)
            {
                uiNode.actionComponents = new ObservableDictionary <string, bool>("");
                uiNode.actionComponents.CollectionChanged += (e) =>
                {
                    //KeyValuePair<string, bool> oldItem = ((KeyValuePair<string, bool>)e.OldItem);
                    //KeyValuePair<string, bool> newItem = ((KeyValuePair<string, bool>)e.NewItem);

                    switch (e.Action)
                    {
                    case CollectionChangedAction.Add:
                        if (((KeyValuePair <string, bool>)e.NewItem).Value == true && uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.NewItem).Key).Count() == 0)
                        {
                            UIBindingComponent attributes = uiNode.gameObject.AddComponent <UIBindingComponent>();
                            attributes.targetComponent = ((KeyValuePair <string, bool>)e.NewItem).Key;
                        }
                        break;

                    case CollectionChangedAction.Remove:
                        foreach (UIBindingComponent attributes in uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.OldItem).Key))
                        {
                            Destroy(attributes);
                        }
                        break;

                    case CollectionChangedAction.Replace:
                        if (((KeyValuePair <string, bool>)e.NewItem).Value == true)
                        {
                            if (uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.NewItem).Key).Count() == 0)
                            {
                                UIBindingComponent attributes = uiNode.gameObject.AddComponent <UIBindingComponent>();
                                attributes.targetComponent = ((KeyValuePair <string, bool>)e.NewItem).Key;
                            }
                        }
                        else
                        {
                            List <UIBindingComponent> attributes = uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.NewItem).Key).ToList();
                            while (attributes.Count > 0)
                            {
                                DestroyImmediate(attributes[0]);
                                attributes.RemoveAt(0);
                            }
                        }
                        break;
                    }
                };
            }
        }
Пример #2
0
        public void Binding(UIBehaviour uiComponent, UIBindingComponent uiToken, IUILogicalNode target)
        {
            text = uiComponent as Text;
            string path = uiComponent.name;

            if (uiToken.AttributeTokens.Where(p => p.attribute == "Text").Count() > 0)
            {
                path = uiToken.AttributeTokens.Where(p => p.attribute == "Text").First().path;
            }
            else if (!string.IsNullOrEmpty(uiToken.componentToken))
            {
                path = uiToken.componentToken;
            }
            else if (!string.IsNullOrEmpty(uiToken.gameObject.GetComponent <IUINode>().NodeToken))
            {
                target.DataContext.BindingComponent(uiToken.gameObject.GetComponent <IUINode>().NodeToken, this);
            }
            target.DataContext.BindingComponent(path, this);
            text.text = target.DataContext.GetData(path) as string;
        }