Beispiel #1
0
 public SharedVariable GetVariable(string name)
 {
     if (!behaviour)
     {
         return(null);
     }
     return(behaviour.GetVariable(name));
 }
Beispiel #2
0
        private void ShouldHide(FieldInfo fieldInfo, out bool should, out bool readOnly)
        {
            HideIf_BTAttribute hideAttr = fieldInfo.GetCustomAttribute <HideIf_BTAttribute>();
            ReadOnlyAttribute  roAttr   = fieldInfo.GetCustomAttribute <ReadOnlyAttribute>();

            should   = false;
            readOnly = false;
            if (hideAttr != null)
            {
                readOnly = hideAttr.readOnly;
                if (ZetanUtility.TryGetMemberValue(hideAttr.path, nodeEditor.node, out var fv, out var mi) && mi != null)
                {
                    if (typeof(SharedVariable).IsAssignableFrom(fv.GetType()))
                    {
                        if (fv is SharedVariable sv)
                        {
                            if ((sv.isGlobal || sv.isShared) && !string.IsNullOrEmpty(sv.name))
                            {
                                if (sv.isGlobal)
                                {
                                    sv = global.GetVariable(sv.name);
                                }
                                else if (sv.isShared)
                                {
                                    sv = tree.GetVariable(sv.name);
                                }
                                if (sv != null && Equals(sv.GetValue(), hideAttr.value))
                                {
                                    should = true;
                                }
                            }
                            else if (!sv.isGlobal && !sv.isShared && Equals(sv.GetValue(), hideAttr.value))
                            {
                                should = true;
                            }
                        }
                    }
                    else if (Equals(fv, hideAttr.value))
                    {
                        should = true;
                    }
                }
            }
            else if (roAttr != null)
            {
                readOnly = !roAttr.onlyRuntime || roAttr.onlyRuntime && Application.isPlaying;
            }
        }
Beispiel #3
0
        public void Init(BehaviourTree tree)
        {
            if (!IsInstance)
            {
                Debug.LogError("尝试初始化未实例化的结点: " + name);
                return;
            }
            Tree = tree;
            foreach (var field in GetType().GetFields(ZetanUtility.CommonBindingFlags).Where(field => field.FieldType.IsSubclassOf(typeof(SharedVariable))))
            {
                var hideAttr = field.GetCustomAttribute <HideIf_BTAttribute>();
                if (hideAttr != null && ZetanUtility.TryGetMemberValue(hideAttr.path, this, out var value, out _) && Equals(value, hideAttr.value))
                {
                    continue;
                }
                SharedVariable variable = field.GetValue(this) as SharedVariable;
                //if (variable.isShared) field.SetValue(this, this.owner.GetVariable(variable.name));
                //else if (variable.isGlobal) field.SetValue(this, BehaviourManager.Instance.GetVariable(variable.name));
                if (variable.isShared)
                {
                    variable.Link(Tree.GetVariable(variable.name));
                }
                else if (variable.isGlobal)
                {
                    variable.Link(BehaviourManager.Instance.GetVariable(variable.name));
                }
            }
            Shortcut = new NodeShortcut(Tree.Executor);
            OnAwake();
#if false
            void TryLinkSharedVariable(object onwer, FieldInfo field)
            {
                if (field.FieldType.IsSubclassOf(typeof(SharedVariable)))
                {
                    SharedVariable variable = field.GetValue(onwer) as SharedVariable;
                    field.SetValue(this, this.owner.GetVariable(variable.name));
                }
                else if (field.FieldType.IsArray)//因为Unity只能显示数组和列表,只对这两种特殊情况特殊处理
                {
                    var array = field.GetValue(owner) as SharedVariable[];
                    if (array.Length < 1)
                    {
                        return;
                    }
                    bool typeMatch  = false;
                    var  eType      = array[0].GetType();
                    var  fieldInfos = eType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    if (eType.IsSubclassOf(typeof(SharedVariable)))
                    {
                        typeMatch = true;
                    }
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (typeMatch)
                        {
                            array[i] = this.owner.GetVariable(array[i].name);
                        }
                        else
                        {
                            foreach (var _field in fieldInfos)
                            {
                                TryLinkSharedVariable(array[i], _field);
                            }
                        }
                    }
                }
                else if (typeof(IList).IsAssignableFrom(field.FieldType))
                {
                    var list = field.GetValue(owner) as IList;
                    if (list.Count < 1)
                    {
                        return;
                    }
                    bool typeMatch  = false;
                    var  eType      = list[0].GetType();
                    var  fieldInfos = eType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    if (eType.IsSubclassOf(typeof(SharedVariable)))
                    {
                        typeMatch = true;
                    }
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (typeMatch)
                        {
                            list[i] = this.owner.GetVariable((list[i] as SharedVariable).name);
                        }
                        else
                        {
                            foreach (var _field in fieldInfos)
                            {
                                TryLinkSharedVariable(list[i], _field);
                            }
                        }
                    }
                }
            }
#endif
        }