Beispiel #1
0
        public static void GeneratePropertiesFromModel(Model model)
        {
            string safeModelName = model.name.Replace(" ", string.Empty);
            string className     = safeModelName + "Properties";
            string basePath      = ProjectUtilities.GetRootPathOf(model);
            string writePath     = basePath + className + ".cs";

            List <string> fields = new List <string>();

            foreach (Property property in model.properties)
            {
                string fieldText = "\tpublic static string ";

                string safeName = property.name.Replace(" ", string.Empty);

                fieldText += safeName + " = \"" + property.name + "\"";

                fieldText += ";\r\n";

                fields.Add(fieldText);
            }

            TextAsset interfaceAsset = Resources.Load <TextAsset>("Templates/ModelPropertiesTemplate");

            string interfaceTemplate = interfaceAsset.text;

            interfaceTemplate = interfaceTemplate.Replace("{CLASS_NAME}", className);
            interfaceTemplate = interfaceTemplate.Replace("{CLASS_BODY}", string.Join(string.Empty, fields.ToArray()));

            if (!string.IsNullOrEmpty(model.propertiesAssetGUID))
            {
                string assetPath = AssetDatabase.GUIDToAssetPath(model.propertiesAssetGUID);

                UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(TextAsset));

                if (obj != null && obj.name != className)
                {
                    basePath  = ProjectUtilities.GetRootPathOf(obj);
                    writePath = basePath + className + ".cs";

                    AssetDatabase.DeleteAsset(assetPath);
                }
                else
                {
                    writePath = assetPath;
                }
            }

            File.WriteAllText(writePath, interfaceTemplate);

            AssetDatabase.ImportAsset(writePath);
            AssetDatabase.Refresh();

            model.propertiesAssetGUID = AssetDatabase.AssetPathToGUID(writePath);
        }
Beispiel #2
0
        public static void GenerateInterfaceFromModel(Model model)
        {
            foreach (State state in model.states)
            {
                List <string> stateNames = new List <string>();

                string safeStateName = state.name.Replace(" ", string.Empty);
                string className     = "I" + model.name + safeStateName + "Handler";

                if (state.generateEnter)
                {
                    stateNames.Add("\tvoid OnEnter" + safeStateName + "();\r\n");
                }

                if (state.generateUpdate)
                {
                    stateNames.Add("\tvoid On" + safeStateName + "();\r\n");
                }

                if (state.generateExit)
                {
                    stateNames.Add("\tvoid OnExit" + safeStateName + "();\r\n");
                }

                if (stateNames.Count == 0)
                {
                    if (!string.IsNullOrEmpty(state.interfaceAssetGUID))
                    {
                        AssetDatabase.DeleteAsset(AssetDatabase.GUIDToAssetPath(state.interfaceAssetGUID));
                    }

                    continue;
                }

                TextAsset interfaceAsset = Resources.Load <TextAsset>("Templates/InterfaceTemplate");

                string interfaceTemplate = interfaceAsset.text;
                interfaceTemplate = interfaceTemplate.Replace("{INTERFACE_NAME}", className);
                interfaceTemplate = interfaceTemplate.Replace("{INTERFACE_BODY}", string.Join(string.Empty, stateNames.ToArray()));

                string basePath      = ProjectUtilities.GetRootPathOf(model);
                string interfacePath = basePath + className + ".cs";

                state.interfaceName = className;
                state.safeStateName = safeStateName;

                bool skipImport = false;

                if (!string.IsNullOrEmpty(state.interfaceAssetGUID))
                {
                    string             tempPath = AssetDatabase.GUIDToAssetPath(state.interfaceAssetGUID);
                    UnityEngine.Object obj      = AssetDatabase.LoadAssetAtPath(tempPath, typeof(TextAsset));

                    if (obj != null)
                    {
                        if (obj.name != className)
                        {
                            basePath = ProjectUtilities.GetRootPathOf(obj);
                            string newPath = basePath + className + ".cs";
                            interfacePath = newPath;

                            AssetDatabase.DeleteAsset(tempPath);

                            File.WriteAllText(newPath, interfaceTemplate);
                        }
                        else
                        {
                            interfacePath = tempPath;
                            skipImport    = true;

                            File.WriteAllText(tempPath, interfaceTemplate);
                        }
                    }
                    else
                    {
                        File.WriteAllText(interfacePath, interfaceTemplate);
                    }
                }
                else
                {
                    File.WriteAllText(interfacePath, interfaceTemplate);
                }

                if (!skipImport)
                {
                    AssetDatabase.ImportAsset(interfacePath);

                    state.interfaceAssetGUID = AssetDatabase.AssetPathToGUID(interfacePath);
                }
            }

            AssetDatabase.Refresh();
        }
Beispiel #3
0
        public static Model GenerateModel(string name)
        {
            Model model = ProjectUtilities.CreateAsset <Model>(name + "Model");

            return(model);
        }