Skip to content

Commit

Permalink
upload source code for unity create file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-wee committed Jun 27, 2018
1 parent 6af22ea commit bba9ab9
Show file tree
Hide file tree
Showing 191 changed files with 916 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# Unity_CreateScriptExtension
Unity脚本创建扩展,可以模板创建C#脚本一样创建其它扩展名的文件,并可以对每种扩展名文件定义模板,该工具已经完成右键创建Lua,XML,Text文件的创建功能.


## 使用方法
导入unitypackage资源包后,可以像创建C#代码一样,去创建Lua,Xml,txt格式的代码或文件.

![](readme_1.jpg)


## 添加其它格式文件
1. 创建模板文件
在felixwee/templates/目录中创建你的模板文件,例如:XXX_template.md
2. 在felixwee/scripts/DevExtEditor.cs代码中添加快捷菜单项

```cs
/* 创建常用文件类型扩展 */
[MenuItem("Assets/Create/Text", false, 81)]
public static void CreateText()
{
//获取创建代码时的文件路径下的所有对象
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
//根据第一个文件获取不前文件夹路径
string folder = AssetDatabase.GetAssetPath(arr[0]);

//将焦点定位到某个文件,并进行重命名
//id,回调处理,新建文件url,图标,源始文件url
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateFileAction>(), folder + "/XXX.md", null, "Assets/Editor/felixwee/templates/XXX_template.md");
}
```

## 下载插件包
9 changes: 9 additions & 0 deletions Unity/UnityCreateScriptExtension/Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Unity/UnityCreateScriptExtension/Assets/Editor/felixwee.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
完成模板中特定内容的替换并更新件内容
@author: felixwee
@email:[email protected]
@blog: www.felixwee.com
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;

public class CreateFileAction : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
UnityEngine.Object obj = CreateScriptAssetFromTemplate(pathName, resourceFile);
ProjectWindowUtil.ShowCreatedAsset(obj);
}

private UnityEngine.Object CreateScriptAssetFromTemplate(string fullPath, string resourceFile)
{
//读取原文件内容
StreamReader streamReader = new StreamReader(resourceFile);
string text = streamReader.ReadToEnd();
streamReader.Close();

//只获取文件名不包括扩展名
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);

//如果是Lua,对模板中变量进行替换
text =text.Replace("#LuaClass#", fileNameWithoutExtension);

//将替换好的内容写入文件
StreamWriter streamWriter = new StreamWriter(fullPath);
streamWriter.Write(text);
streamWriter.Close();
//导入指定路径下的资源
AssetDatabase.ImportAsset(fullPath);
//返回指定路径下的Object对象
return AssetDatabase.LoadAssetAtPath(fullPath, typeof(UnityEngine.Object));
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
根据模板文件创建各种格式的代码模板
@author: felixwee
@email:[email protected]
@blog: www.felixwee.com
*/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class DevExtEditor {

/* 创建常用文件类型扩展 */
[MenuItem("Assets/Create/Lua Script",false,81)]
public static void CreateLuaScript()
{
//获取创建代码时的文件路径下的所有对象
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
//根据第一个文件获取不前文件夹路径
string folder=AssetDatabase.GetAssetPath(arr[0]);

//将焦点定位到某个文件,并进行重命名
//id,回调处理,新建文件url,图标,源始文件url
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,ScriptableObject.CreateInstance<CreateFileAction>(),folder + "/NewLuaScript.lua", null, "Assets/Editor/felixwee/templates/lua_template.lua");
}
/* 创建常用文件类型扩展 */
[MenuItem("Assets/Create/XML", false, 81)]
public static void CreateXML()
{
//获取创建代码时的文件路径下的所有对象
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
//根据第一个文件获取不前文件夹路径
string folder = AssetDatabase.GetAssetPath(arr[0]);

//将焦点定位到某个文件,并进行重命名
//id,回调处理,新建文件url,图标,源始文件url
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateFileAction>(), folder + "/NewXml.xml", null, "Assets/Editor/felixwee/templates/xml_template.xml");
}

/* 创建常用文件类型扩展 */
[MenuItem("Assets/Create/Text", false, 81)]
public static void CreateText()
{
//获取创建代码时的文件路径下的所有对象
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
//根据第一个文件获取不前文件夹路径
string folder = AssetDatabase.GetAssetPath(arr[0]);

//将焦点定位到某个文件,并进行重命名
//id,回调处理,新建文件url,图标,源始文件url
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateFileAction>(), folder + "/NewText.txt", null, "Assets/Editor/felixwee/templates/txt_template.txt");
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--[ #LuaClass# ]

--[定义lua类]
#LuaClass#={};

--[常用变量定义]
this=#LuaClass#;
local transform;
local gameObject;

function #LuaClass#.Awake()

end


function #LuaClass#.Start()

end


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8" ?>
<root>
</root>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
1 change: 1 addition & 0 deletions Unity/UnityCreateScriptExtension/Library/AssetImportState
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5;0;6;0;0
Binary file not shown.
Empty file.
Binary file not shown.
Loading

0 comments on commit bba9ab9

Please sign in to comment.