예제 #1
0
        /// <summary>
        /// Called when editor is enabled.
        /// </summary>
        private void OnEnable()
        {
            var _atomTypesToGenerate = new List <AtomType>(AtomTypes.ALL_ATOM_TYPES);

#if UNITY_2019_1_OR_NEWER
            var root = this.rootVisualElement;
#elif UNITY_2018_4_OR_NEWER
            var root = this.GetRootVisualContainer();
#endif
            var pathRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            pathRow.Add(new Label()
            {
                text = "Relative Write Path", style = { width = 180, marginRight = 8 }
            });
            var textfield = new TextField()
            {
                value = _baseWritePath, style = { flexGrow = 1 }
            };
            textfield.RegisterCallback <ChangeEvent <string> >(evt => _baseWritePath = evt.newValue);
            pathRow.Add(textfield);
            root.Add(pathRow);

            var typeNameRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            typeNameRow.Add(new Label()
            {
                text = "Type Name", style = { width = 180, marginRight = 8 }
            });
            textfield = new TextField()
            {
                value = _valueType, style = { flexGrow = 1 }
            };
            textfield.RegisterCallback <ChangeEvent <string> >(evt => _valueType = evt.newValue);
            typeNameRow.Add(textfield);
            root.Add(typeNameRow);

            var equatableRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            equatableRow.Add(new Label()
            {
                text = "Is Type Equatable?", style = { width = 180, marginRight = 8 }
            });
            var equatableToggle = new Toggle()
            {
                value = _isValueTypeEquatable, style = { flexGrow = 1 }
            };
            equatableToggle.RegisterCallback <ChangeEvent <bool> >(evt => _isValueTypeEquatable = evt.newValue);
            equatableRow.Add(equatableToggle);
            root.Add(equatableRow);

            var typeNamespaceRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            typeNamespaceRow.Add(new Label()
            {
                text = "Type Namespace", style = { width = 180, marginRight = 8 }
            });
            textfield = new TextField()
            {
                value = _valueTypeNamespace, style = { flexGrow = 1 }
            };
            textfield.RegisterCallback <ChangeEvent <string> >(evt => _valueTypeNamespace = evt.newValue);
            typeNamespaceRow.Add(textfield);
            root.Add(typeNamespaceRow);

            var subUnityAtomsNamespaceRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            subUnityAtomsNamespaceRow.Add(new Label()
            {
                text = "Sub Unity Atoms Namespace", style = { width = 180, marginRight = 8 }
            });
            textfield = new TextField()
            {
                value = _subUnityAtomsNamespace, style = { flexGrow = 1 }
            };
            textfield.RegisterCallback <ChangeEvent <string> >(evt => _subUnityAtomsNamespace = evt.newValue);
            subUnityAtomsNamespaceRow.Add(textfield);
            root.Add(subUnityAtomsNamespaceRow);

            root.Add(CreateDivider());

            var typesToGenerateLabelRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            typesToGenerateLabelRow.Add(new Label()
            {
                text = "Type(s) to Generate"
            });
            root.Add(typesToGenerateLabelRow);

            foreach (var atomType in AtomTypes.ALL_ATOM_TYPES)
            {
                _typeVEDict.Add(atomType, CreateAtomTypeToGenerateToggleRow(atomType));
                root.Add(_typeVEDict[atomType]);
            }

            root.Add(CreateDivider());

            _typesToGenerateInfoRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Column }
            };
            _typesToGenerateInfoRow.Add(new Label()
            {
                style = { color = Color.yellow, height = 12 }
            });
            _typesToGenerateInfoRow.RegisterCallback <MouseUpEvent>((e) => { _typesToGenerateInfoRow.Query <Label>().First().text = ""; });
            root.Add(_typesToGenerateInfoRow);

            root.Add(CreateDivider());

            var buttonRow = new VisualElement()
            {
                style = { flexDirection = FlexDirection.Row }
            };
            var button1 = new Button(Close)
            {
                text  = "Close",
                style = { flexGrow = 1 }
            };
            buttonRow.Add(button1);

            var button2 = new Button(() =>
            {
                var templates            = Generator.GetTemplatePaths();
                var templateConditions   = Generator.CreateTemplateConditions(_isValueTypeEquatable, _valueTypeNamespace, _subUnityAtomsNamespace);
                var templateVariables    = Generator.CreateTemplateVariablesMap(_valueType, _valueTypeNamespace, _subUnityAtomsNamespace);
                var capitalizedValueType = _valueType.Capitalize();

                _atomTypesToGenerate.ForEach((atomType) =>
                {
                    templateVariables["VALUE_TYPE_NAME"] = atomType.IsValuePair ? $"{capitalizedValueType}Pair" : capitalizedValueType;
                    var valueType = atomType.IsValuePair ? $"{capitalizedValueType}Pair" : _valueType;
                    templateVariables["VALUE_TYPE"] = valueType;
                    Generator.Generate(new AtomReceipe(atomType, valueType), _baseWritePath, templates, templateConditions, templateVariables);
                });

                AssetDatabase.Refresh();
            })
            {
                text  = "Generate",
                style = { flexGrow = 1 }
            };
            buttonRow.Add(button2);
            root.Add(buttonRow);
        }
예제 #2
0
        static void Regenereate()
        {
            if (!Runtime.IsUnityAtomsRepo)
            {
                Debug.LogWarning("This is currently only available working in the Unity Atoms project...");
            }

            string path = EditorUtility.OpenFolderPanel("Select the 'Packages' folder (containing Core)", ".", "");

            if (string.IsNullOrEmpty(path))
            {
                Debug.LogWarning("Empty path. Abort.");
                return;
            }
            path = new Uri(Application.dataPath).MakeRelativeUri(new Uri(path)).ToString();

            var i = EditorUtility.DisplayDialogComplex("Regenerate Atoms",
                                                       $"Do you want to regenerate all Atoms and write them to '{path}'?\n",
                                                       "Yes, I know what I'm doing!", "Cancel", "");

            if (i == 1)
            {
                Debug.LogWarning("Cancelled generating Atoms.");
                return;
            }

            var itemsToRegenerate = new List <RegenerateItem>()
            {
                // Base Atoms
                new RegenerateItem(
                    valueType: "Void",
                    baseWritePath: Path.Combine(path, "BaseAtoms"),
                    isValueEquatable: false,
                    atomTypesToGenerate: new List <AtomType>()
                {
                    AtomTypes.EVENT, AtomTypes.UNITY_EVENT, AtomTypes.BASE_EVENT_REFERENCE, AtomTypes.EVENT_INSTANCER, AtomTypes.BASE_EVENT_REFERENCE_LISTENER
                },
                    typeNamespace: "",
                    subUnityAtomsNamespace: "BaseAtoms"
                    ),
                new RegenerateItem(valueType: "bool", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Collider2D", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: false, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Collider", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: false, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Collision2D", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: false, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Collision", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: false, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Color", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "float", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "GameObject", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: false, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "int", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "string", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Vector2", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(valueType: "Vector3", baseWritePath: Path.Combine(path, "BaseAtoms"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityEngine", subUnityAtomsNamespace: "BaseAtoms"),
                new RegenerateItem(
                    valueType: "AtomBaseVariable",
                    baseWritePath: Path.Combine(path, "BaseAtoms"),
                    isValueEquatable: false,
                    atomTypesToGenerate: new List <AtomType>()
                {
                    AtomTypes.EVENT, AtomTypes.ACTION, AtomTypes.UNITY_EVENT, AtomTypes.BASE_EVENT_REFERENCE, AtomTypes.EVENT_INSTANCER, AtomTypes.BASE_EVENT_REFERENCE_LISTENER
                },
                    typeNamespace: "",
                    subUnityAtomsNamespace: "BaseAtoms"
                    ),

                new RegenerateItem
                (
                    valueType: "double",
                    baseWritePath: Path.Combine(path, "BaseAtoms"),
                    isValueEquatable: true,
                    atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES,
                    typeNamespace: "",
                    subUnityAtomsNamespace: "BaseAtoms"
                ),

                new RegenerateItem
                (
                    valueType: "Quaternion",
                    baseWritePath: Path.Combine(path, "BaseAtoms"),
                    isValueEquatable: true,
                    atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES,
                    typeNamespace: "UnityEngine",
                    subUnityAtomsNamespace: "BaseAtoms"
                ),

                //MonoHooks
                new RegenerateItem(valueType: "ColliderGameObject", baseWritePath: Path.Combine(path, "MonoHooks"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityAtoms.MonoHooks", subUnityAtomsNamespace: "MonoHooks"),
                new RegenerateItem(valueType: "Collider2DGameObject", baseWritePath: Path.Combine(path, "MonoHooks"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityAtoms.MonoHooks", subUnityAtomsNamespace: "MonoHooks"),
                new RegenerateItem(valueType: "CollisionGameObject", baseWritePath: Path.Combine(path, "MonoHooks"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityAtoms.MonoHooks", subUnityAtomsNamespace: "MonoHooks"),
                new RegenerateItem(valueType: "Collision2DGameObject", baseWritePath: Path.Combine(path, "MonoHooks"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityAtoms.MonoHooks", subUnityAtomsNamespace: "MonoHooks"),

                //Mobile
                new RegenerateItem(valueType: "TouchUserInput", baseWritePath: Path.Combine(path, "Mobile"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityAtoms.Mobile", subUnityAtomsNamespace: "Mobile"),

                //SceneMgmt
                new RegenerateItem(valueType: "SceneField", baseWritePath: Path.Combine(path, "SceneMgmt"), isValueEquatable: true, atomTypesToGenerate: AtomTypes.ALL_ATOM_TYPES, typeNamespace: "UnityAtoms.SceneMgmt", subUnityAtomsNamespace: "SceneMgmt"),

                //FSM
                new RegenerateItem(
                    valueType: "FSMTransitionData",
                    baseWritePath: Path.Combine(path, "FSM"),
                    isValueEquatable: false,
                    atomTypesToGenerate: new List <AtomType>()
                {
                    AtomTypes.EVENT, AtomTypes.ACTION, AtomTypes.UNITY_EVENT, AtomTypes.BASE_EVENT_REFERENCE, AtomTypes.EVENT_INSTANCER, AtomTypes.BASE_EVENT_REFERENCE_LISTENER
                },
                    typeNamespace: "",
                    subUnityAtomsNamespace: "FSM"
                    ),

                //Input System
                new RegenerateItem
                (
                    valueType: "PlayerInput",
                    baseWritePath: Path.Combine(path, "InputSystem"),
                    isValueEquatable: false,
                    atomTypesToGenerate: new List <AtomType>()
                {
                    AtomTypes.EVENT, AtomTypes.ACTION, AtomTypes.UNITY_EVENT, AtomTypes.EVENT_INSTANCER
                },
                    typeNamespace: "UnityEngine.InputSystem",
                    subUnityAtomsNamespace: "InputSystem"
                ),

                new RegenerateItem
                (
                    valueType: "InputAction.CallbackContext",
                    baseWritePath: Path.Combine(path, "InputSystem"),
                    isValueEquatable: false,
                    atomTypesToGenerate: new List <AtomType>()
                {
                    AtomTypes.EVENT, AtomTypes.ACTION, AtomTypes.UNITY_EVENT, AtomTypes.EVENT_INSTANCER
                },
                    typeNamespace: "UnityEngine.InputSystem",
                    subUnityAtomsNamespace: "InputSystem"
                ),
            };

            foreach (var item in itemsToRegenerate)
            {
                var templates            = Generator.GetTemplatePaths();
                var templateConditions   = Generator.CreateTemplateConditions(item.IsValueEquatable, item.ValueTypeNamespace, item.SubUnityAtomsNamespace, item.ValueType);
                var templateVariables    = Generator.CreateTemplateVariablesMap(item.ValueType, item.ValueTypeNamespace, item.SubUnityAtomsNamespace);
                var capitalizedValueType = item.ValueType.Replace('.', '_').Capitalize();

                foreach (var atomType in item.AtomTypesToGenerate)
                {
                    templateVariables["VALUE_TYPE_NAME"] = atomType.IsValuePair ? $"{capitalizedValueType}Pair" : capitalizedValueType;
                    var valueType = atomType.IsValuePair ? $"{capitalizedValueType}Pair" : item.ValueType;
                    templateVariables["VALUE_TYPE"] = valueType;
                    Generator.Generate(new AtomReceipe(atomType, valueType), item.BaseWritePath, templates, templateConditions, templateVariables);
                }
            }
            AssetDatabase.Refresh();
        }
예제 #3
0
        public void Generate()
        {
            var type = Type.GetType($"{FullQualifiedName}");

            if (type == null)
            {
                throw new TypeLoadException($"Type could not be found ({FullQualifiedName})");
            }
            var isValueTypeEquatable = type.GetInterfaces().Contains(typeof(IEquatable <>));

            var templateVariables    = Generator.CreateTemplateVariablesMap(BaseType, Namespace, "BaseAtoms");
            var capitalizedValueType = BaseType.Capitalize();
            var templates            = Generator.GetTemplatePaths();
            var templateConditions   =
                Generator.CreateTemplateConditions(isValueTypeEquatable, Namespace, "BaseAtoms", BaseType);
            var baseWritePath =
                Path.Combine((Path.GetDirectoryName(AssetDatabase.GetAssetPath(this.GetInstanceID()))) ?? "Assets/",
                             "Generated");

            Directory.CreateDirectory(baseWritePath);

            Scripts.Clear();
            var t   = GenerationOptions;
            var idx = 0;

            while (t > 0)
            {
                if (t % 2 == 1)
                {
                    var atomType = AtomTypes.ALL_ATOM_TYPES[idx];

                    templateVariables["VALUE_TYPE_NAME"] =
                        atomType.IsValuePair ? $"{capitalizedValueType}Pair" : capitalizedValueType;
                    var valueType = atomType.IsValuePair ? $"{capitalizedValueType}Pair" : BaseType;
                    templateVariables["VALUE_TYPE"] = valueType;

                    var resolvedRelativeFilePath = Templating.ResolveVariables(templateVariables: templateVariables,
                                                                               toResolve: atomType.RelativeFileNameAndPath);
                    var targetPath = Path.Combine(baseWritePath, resolvedRelativeFilePath);

                    var newCreated = !File.Exists(targetPath);

                    Generator.Generate(new AtomReceipe(atomType, valueType), baseWritePath, templates,
                                       templateConditions, templateVariables);

                    if (newCreated)
                    {
                        AssetDatabase.ImportAsset(targetPath);
                    }
                    var ms = AssetDatabase.LoadAssetAtPath <MonoScript>(targetPath);
                    Scripts.Add(ms);
                }
                else
                {
                    Scripts.Add(null);
                }

                idx++;
                t >>= 1;
            }
        }