读一读

AssetPostProcessor继承自UnityEditor,是一个编辑器类,主要在资源导入的时候可以自己定义一些默认的格式,当然但如完毕也有通知的接口。使用方法,新建脚本继承AssetPostProcessor,重写你需要的方法就行了。

导入资源,不同的类型用不同的方法。Model用OnPreprocessModel(导入前调用,使用ModelImporter修改属性),OnPostprocessMode(导入后调用)。图片用OnPreprocessTexture(使用TextureImporter修改属)和OnPostprocessTexture/Sprites()等。还有Audio等。

对一个MeshRenderer类似的赋材质的时候调用OnAssignMaterialModel(Material m,Renderer r)

static OnPostprocessAllAssets(string[] importAssets,string[] deleteAssets,string[] movedAssets,string[] movedFromPaths),所有资源的导入删除和移动都会调用它,可以用它监听资源的动向


using UnityEditor;
public class AutoSetTextureUISprite : AssetPostprocessor
{

    void OnPreprocessTexture()
    {
        //自动设置类型;  
        TextureImporter textureImporter = (TextureImporter)assetImporter;
        textureImporter.textureType = TextureImporterType.Sprite;//设置图片类型

        /*TextureImporterPlatformSettings setting = new TextureImporterPlatformSettings();
        setting.format = TextureImporterFormat.RGBA32;
        setting.maxTextureSize = 2048;
        setting.compressionQuality = 80;//[0,100]
        setting.textureCompression = TextureImporterCompression.Compressed;
        这个是真的不知道怎么用,设置了难道就可以了?

        textureImporter.SetPlatformTextureSettings(setting);*/
        
        textureImporter.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.RGBA32, true);//过时但是能用
        textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.RGBA32, true);
    }
}


脚本可以放在工程Assets的任意地方,然后导入图片就会自动设置了。

默认设置为Sprite是因为在NGUI中使用texture打包出来的图集图片也是texture中,会在某些平台上产生模糊,所以默认设置成Sprite且为RGBA32不压缩格式,以免忘记

当导入的图片是在粒子等上使用时,也会因为是Sprite而使用不了从而去主动设置格式,避免了n多图集没有修改格式,而造成的模糊


CreateAssetMenu特性可以创建一个按钮,将声明的类生成一个可编辑的资源文件

[CreateAssetMenu(fileName = "List",menuName = "Create List",order = 1)]
public class GameObjectPoolList : ScriptableObject {
    public List<GameObjectPool> pools = new List<GameObjectPool>();
}

这样子就不用使用ScriptableObject去GetInstance,再用AssetDatabase去创建资源文件了。


一、要编辑的单元

using UnityEngine;
using System;

[Serializable]
public class GameObjectPool {

    [SerializeField]
    private string name;
    [SerializeField]
    private int maxNum;
}

二、可编辑的类型

using System.Collections.Generic;
using UnityEngine;

public class GameObjectPoolList : ScriptableObject {

    public List<GameObjectPool> pools = new List<GameObjectPool>();
}

三、创建这个可编辑的文件

using UnityEngine;
using UnityEditor;

public class PoolManagerEditor {

    [MenuItem("Manager/Create Pool")]
    public static void CreateEditorPools() {
        GameObjectPoolList list = ScriptableObject.CreateInstance<GameObjectPoolList>();
        string path = "Assets/26/gameobjectpool.asset";
        AssetDatabase.CreateAsset(list, path);
        AssetDatabase.SaveAssets();
    }
}

四、结果

blob.png


using UnityEditor;
public class MyWindow : EditorWindow {

	[MenuItem("Item/MyWindow")]
	static void ShowMyWindow(){
		MyWindow window =  EditorWindow.GetWindow<MyWindow> ();
		window.Show ();//获得实例,显示窗口
	}

	string inputStr = "";
	void OnGUI(){//绘制窗口内容
		EditorGUILayout.LabelField ("实例化输入名字的物体");
		inputStr =  EditorGUILayout.TextField (inputStr);
		if (GUILayout.Button ("确定")) {
			GameObject go = new GameObject (inputStr);
			Undo.RegisterCreatedObjectUndo (go,"create"+inputStr);
		}
	}
}


创建类继承自EditorWindow,存放在Editor文件夹下,表示是一个窗口类

这里创建了一个菜单来打开这个窗口

OnGUI是窗口内容的绘制,可以用EdittorGUILayoutGUILayout等类来绘制ui内容

UndoRegisterCreateObjectUndo用来记录新创建的物体,用于后面可以撤销新创建的


void OnWizardOtherButton(){
	GameObject[] objects = Selection.gameObjects;
	int count = 0;
	foreach (GameObject go in objects) {
		Undo.RecordObject (go, "change name");
		go.name += changeName;
		count++;
		EditorUtility.DisplayProgressBar ("进度", count + "/" + objects.Length, (float)count / objects.Length);
	}
	EditorUtility.ClearProgressBar ();//关闭进度条
	ShowNotification (new GUIContent (Selection.gameObjects.Length + "个物体被修改了"));
}


使用EditorUtility的静态方法DisplayProgressBar(标题,提示,进度float)


public string changeName = "你的名字";
const string NameField = "changeName";

void OnEnable(){
	this.changeName = EditorPrefs.GetString (NameField,this.changeName);//第二个参数为默认值
}

void OnWizardUpdate(){
	errorString = null;
	helpString = null;
	if (Selection.gameObjects.Length > 0) {
		helpString = "你选择的物体数量为:" + Selection.gameObjects.Length;
	} else {
		errorString = "最少需要选择一个物体";
	}

	EditorPrefs.SetString (NameField, this.changeName);
}


使用EditorPrefs类的静态方法(对应数据类型),跟PlayerPrefs差不多滴


void OnWizardOtherButton(){
	GameObject[] objects = Selection.gameObjects;
	foreach (GameObject go in objects) {
		Undo.RecordObject (go, "change name");
		go.name += changeName;
	}
	//在这里
	ShowNotification (new GUIContent (Selection.gameObjects.Length + "个物体被修改了"));
}


ShowNotification是EditorWindow的一个成员方法,实例化了的窗口才可以使用

会直接在窗口的空白位置显示你要的提醒信息,参数是GUIContent类型的,封装了一层


using UnityEngine;
using UnityEditor;

public class Migrazar : ScriptableWizard {

	[MenuItem("Item/CreateWizard")]
	public static void CreateWizard(){
		ScriptableWizard.DisplayWizard<Migrazar> ("I am a boy","Change And Close","Change");
	}


	public string changeName = "你的名字";

	void OnWizardOtherButton(){
		GameObject[] objects = Selection.gameObjects;
		foreach (GameObject go in objects) {
			Undo.RecordObject (go, "change name");
			go.name += changeName;
		}
	}
}


ScriptableWizard的方法DisplayWizard的第三个参数就是OtherButton的名字,点击调用OnWizardOtherButton()


using UnityEngine;
using UnityEditor;

public class Migrazar : ScriptableWizard {
    //导向框中字段值发生改变时调用
    void OnWizardUpdate(){
    	errorString = null;//错误的提示信息
    	helpString = null;//帮助提示信息
    	if (Selection.gameObjects.Length > 0) {
    		helpString = "你选择的物体数量为:" + Selection.gameObjects.Length;
    	} else {
    		errorString = "最少需要选择一个物体";
    	}
    }
    
    //监听选择的改变也要调用OnWizardUpdate
    void OnSelectionChange(){
    	OnWizardUpdate ();
    }
}