Exemplo n.º 1
0
        public static UnityEngine.Object CreateScript(ScriptContent data, string path, string template)
        {
            var scriptContent = GenerateScriptTextContent(data, template);
            var fullPath      = Path.GetFullPath(path);

            File.WriteAllText(fullPath, scriptContent, new System.Text.UTF8Encoding());

            // Import the asset
            AssetDatabase.ImportAsset(path);

            return(AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)));
        }
Exemplo n.º 2
0
        public static string CreateScriptText(ScriptContent content, string template)
        {
            template = AddNamespaces(content, template);

            var @namespace = content.@namespace;

            template = template.Replace("#NAMESPACE#", @namespace);

            template = template.Replace("#SCRIPTNAME#", content.typeName);
            template = AddFields(content, template);

            //template = template.Replace("#METHODS#", content.methods);

            template = template.Replace("#NOTRIM#", "");

            return(template);
        }
Exemplo n.º 3
0
        private static string AddFields(ScriptContent content, string template)
        {
            var fieldsIndex = template.IndexOf("#FIELDS#");

            var endOfLineIndex = template.LastIndexOf('\n', fieldsIndex);
            var indent         = new string(' ', fieldsIndex - endOfLineIndex - 1);

            var sb = new StringBuilder();

            foreach (var field in content.fields)
            {
                sb.Append($"\n{field};");
            }
            //template = template.

            //var indentedFields = content.fields.Replace("\n", Environment.NewLine + indent);
            //template = template.Replace("#FIELDS#", indentedFields);
            return(template);
        }
Exemplo n.º 4
0
        private void CreateScript()
        {
            var scriptData = new ScriptContent
            {
                @namespace      = _settings.DefaultNamespace,
                typeName        = Path.GetFileNameWithoutExtension(_fileLocationPanel.FileName).Replace(" ", ""),
                fields          = GetScaffoldingText(false),
                usingNamespaces = _fieldsList.Fields.Select(f => f.reference.GetType().Namespace).Distinct().ToArray()
            };

            var template = _settings.ScriptTemplate.text;
            var path     = Path.ChangeExtension(_fileLocationPanel.FullPath, ".cs");

            ScaffoldingUtility.CreateScript(scriptData, path, template);
#if ASSET_FACTORY
            GetWindow <AssetFactoryWindow>().Close();
#endif
            Close();
        }
Exemplo n.º 5
0
        private static string AddNamespaces(ScriptContent content, string template)
        {
            var usingsEndIndex = template.IndexOf("#USINGSEND#");

            foreach (var namescp in content.usingNamespaces)
            {
                if (string.IsNullOrEmpty(namescp))
                {
                    continue;
                }
                if (template.LastIndexOf(namescp, usingsEndIndex) == -1)
                {
                    var newLine = $"\nusing {namescp};";
                    template        = template.Insert(usingsEndIndex, newLine);
                    usingsEndIndex += newLine.Length;
                }
            }
            template = template.Replace("#USINGSEND#", string.Empty);
            return(template);
        }
Exemplo n.º 6
0
        public static string GenerateScriptTextContent(ScriptContent data, string template)
        {
            var usingsEndIndex = template.IndexOf("#USINGSEND#");

            foreach (var namescp in data.usingNamespaces)
            {
                if (string.IsNullOrEmpty(namescp))
                {
                    continue;
                }
                if (template.LastIndexOf(namescp, usingsEndIndex) == -1)
                {
                    var newLine = $"\nusing {namescp};";
                    template        = template.Insert(usingsEndIndex, newLine);
                    usingsEndIndex += newLine.Length;
                }
            }
            template = template.Replace("#USINGSEND#", string.Empty);

            var @namespace = data.@namespace;

            template = template.Replace("#NAMESPACE#", @namespace);

            template = template.Replace("#SCRIPTNAME#", data.typeName);

            var fieldsIndex    = template.IndexOf("#FIELDS#");
            var endOfLineIndex = template.LastIndexOf('\n', fieldsIndex);
            var indent         = new string(' ', fieldsIndex - endOfLineIndex - 1);
            var indentedFields = data.fields.Replace("\n", Environment.NewLine + indent);

            template = template.Replace("#FIELDS#", indentedFields);


            template = template.Replace("#METHODS#", data.methods);

            template = template.Replace("#NOTRIM#", "");

            return(template);
        }