コード例 #1
0
        private void CreateSharedVariableUI(Type type, object obj, VisualElement container)
        {
            var fields = NodeSerializer.GetSerializedFields(type);

            foreach (var fInfo in fields)
            {
                var foldout = new Foldout
                {
                    text = ObjectNames.NicifyVariableName(fInfo.Name)
                };

                if (fInfo.FieldType != typeof(SharedVariable))
                {
                    if (((!fInfo.FieldType.IsAbstract && fInfo.FieldType.IsSerializable) || NodeSerializer.IsTaskReference(fInfo.FieldType)) && !fInfo.FieldType.Namespace.StartsWith("System"))
                    {
                        var target = fInfo.GetValue(obj) ?? NodeSerializer.CreateTypeInstance(fInfo.FieldType);
                        fInfo.SetValue(obj, target);

                        var name     = $"unity-foldout-Selected.Data.{fInfo.Name}";
                        var existing = container.Q(name);

                        if (existing != null)
                        {
                            if (NodeSerializer.IsTaskReference(fInfo.FieldType))
                            {
                                CreateTaskReferenceUI(fInfo.FieldType, fInfo, obj, existing);
                            }
                            else if (fInfo.FieldType.IsSerializable)
                            {
                                CreateSharedVariableUI(fInfo.FieldType, target, existing);
                            }
                        }
                        else
                        {
                            existing = new Foldout
                            {
                                text = ObjectNames.NicifyVariableName(fInfo.Name)
                            };

                            if (NodeSerializer.IsTaskReference(fInfo.FieldType))
                            {
                                CreateTaskReferenceUI(fInfo.FieldType, fInfo, obj, existing);
                            }
                            else if (fInfo.FieldType.IsSerializable)
                            {
                                CreateSharedVariableUI(fInfo.FieldType, target, existing);
                            }

                            if (existing.childCount > 0)
                            {
                                container.Add(existing);
                            }
                        }

                        existing.MarkDirtyRepaint();
                    }
                    continue;
                }

                var choices = _inspectorObject?.View?.Properties?.Select(x => x.Name).ToList() ?? new List <string>();
                choices.Insert(0, "None");

                var value = fInfo.GetValue(obj) as SharedVariable;

                var index = choices.IndexOf(value?.Name);
                if (index < 0)
                {
                    index = 0;
                }

                var popup   = new PopupField <string>("Variable", choices, index);
                var tooltip = fInfo.GetCustomAttribute <TooltipAttribute>();
                if (tooltip != null)
                {
                    popup.tooltip = tooltip.Tooltip;
                }

                popup.RegisterValueChangedCallback(ev =>
                {
                    if (value == null)
                    {
                        var propType = _inspectorObject.View.Properties.FirstOrDefault(x => x.Name == ev.newValue)?.Type;
                        if (propType != null)
                        {
                            value = Activator.CreateInstance(propType) as SharedVariable;
                        }
                    }

                    value.Name     = ev.newValue;
                    value.IsShared = value.Name != "None";
                    fInfo.SetValue(obj, value);
                    serializedObject.ApplyModifiedProperties();
                });
                foldout.Add(popup);
                container.Add(foldout);
            }
        }