Unity Editor 编辑器扩展 十三 自定义创建资源

时间:2021-03-27 06:12:24

目录

初级资源创建

Editor文件夹下创建脚本:

using UnityEngine;
using UnityEditor;

public class CreateTest
{
[MenuItem ("Assets/Create ExampleAssets")]
static void CreateExampleAssets ()
{
var material = new Material (Shader.Find ("Standard"));

ProjectWindowUtil.CreateAsset (material, "New Material.mat");
}
}

点击Assets--Create ExampleAssets 就会发现一个材质球被创建

中级资源创建

Editor文件夹下创建脚本:

using UnityEngine;
using UnityEditor;

public class CreateMaterial
{
[MenuItem ("Assets/Create Material")]
static void CreateExampleAssets ()
{
var material = new Material (Shader.Find ("Standard"));
var instanceID = material.GetInstanceID ();

var icon = AssetPreview.GetMiniThumbnail (material);

var endNameEditAction =
ScriptableObject.CreateInstance<DoCreateMaterialAsset> ();

ProjectWindowUtil.StartNameEditingIfProjectWindowExists (instanceID,
endNameEditAction, "New Material.mat", icon, "");
}
}

点击Assets--Create Material 创建一个特定的材质球

更*的自定义资源创建

Editor文件夹下创建脚本:

using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;

public class DoCreateMaterialAsset : EndNameEditAction
{
public override void Action (int instanceId, string pathName, string resourceFile)
{
var mat = (Material)EditorUtility.InstanceIDToObject (instanceId);

mat.color = Color.red;

AssetDatabase.CreateAsset (mat, pathName);
AssetDatabase.ImportAsset (pathName);
ProjectWindowUtil.ShowCreatedAsset (mat);
}
}

Editor文件夹下创建脚本:

using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using System;

public class DoCreateScriptAsset : EndNameEditAction
{
public override void Action (int instanceId, string pathName, string resourceFile)
{
var text = File.ReadAllText (resourceFile);

var className = Path.GetFileNameWithoutExtension (pathName);

//清除空格
className = className.Replace (" ", "");


text ="//Programer: "+"porgramerName"+"\n//code date:"+DateTime.Now.Date.ToShortDateString()+"\n"
+text.Replace ("#SCRIPTNAME#", className);

text += "\n//自己添加Something!"+"\n";

//utf8
var encoding = new UTF8Encoding (true, false);

File.WriteAllText (pathName, text, encoding);

AssetDatabase.ImportAsset (pathName);
var asset = AssetDatabase.LoadAssetAtPath<MonoScript> (pathName);
ProjectWindowUtil.ShowCreatedAsset (asset);
}
}

Editor文件夹下创建脚本:

using UnityEditor;
using UnityEngine;
using System.IO;

public class CreateAssets : EditorWindow
{

[MenuItem("Window/CreatAssets")]
static void Open ()
{
GetWindow<CreateAssets> ();
}

public string scriptName,materialName;
void OnGUI()
{
var options = new []{GUILayout.Width (100), GUILayout.Height (20)};

GUILayout.Label ("CreateScripts");
EditorGUILayout.Space ();
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.LabelField ("ScriptName",options);
scriptName= EditorGUILayout.TextArea (scriptName);
EditorGUILayout.EndHorizontal ();


if (GUILayout.Button ("Create")) {

CreateScript (scriptName);
}

GUILayout.Label ("CreateMaterial");
EditorGUILayout .Space ();
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.LabelField ("materialName",options);
materialName= EditorGUILayout.TextArea (materialName);
EditorGUILayout.EndHorizontal ();


if (GUILayout.Button ("Create")) {
CreateMaterial (materialName);
}

}
// 新建自定义脚本
static void CreateScript (string scriptName)
{
var resourceFile = Path.Combine (EditorApplication.applicationContentsPath,
"Resources/ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt");

Debug.Log (resourceFile);
Texture2D csIcon =
EditorGUIUtility.IconContent ("cs Script Icon").image as Texture2D;

var endNameEditAction =
ScriptableObject.CreateInstance<DoCreateScriptAsset> ();

ProjectWindowUtil.StartNameEditingIfProjectWindowExists (0, endNameEditAction,
scriptName+".cs", csIcon, resourceFile);
}

//新建Material
static void CreateMaterial (string materialName)
{
var material = new Material (Shader.Find ("Standard"));
var instanceID = material.GetInstanceID ();
var icon = AssetPreview.GetMiniThumbnail (material);

var endNameEditAction =
ScriptableObject.CreateInstance<DoCreateMaterialAsset> ();

ProjectWindowUtil.StartNameEditingIfProjectWindowExists (instanceID,
endNameEditAction, materialName+".mat", icon, "");
}

}

Window–Create Assets打开如下窗口

Unity Editor 编辑器扩展 十三 自定义创建资源

这个是自定义生成的脚本

Unity Editor 编辑器扩展 十三 自定义创建资源

关于自定义脚本中间的内容,需要更改它的生成模版,在Unity安装文件内,这里就不啰嗦了,大家可参照这篇文章:http://blog.csdn.net/warrenmondeville/article/details/53577085

本文链接:http://blog.csdn.net/WarrenMondeville/article/details/53807926