李志的部落格

Site Tagline

Unity

自定义菜单

//https://docs.unity3d.com/ScriptReference/MenuItem.html
public class MyMenu : MonoBehaviour {
	[MenuItem("MyMenu/do something")]
	static void doSomething(){
		Debug.Log("doing something");
	}

	[MenuItem("MyMenu/log selected transform name")]
	static void logSelectedTransformName(){
		if(Selection.activeTransform!=null)
			Debug.Log(Selection.activeTransform.gameObject.name);
		else
			Debug.Log("选择为null");
	}

	[MenuItem("MyMenu/saveTexture")]
	static void saveTexture(){
		Object o = Selection.activeObject;
		Texture2D t = o as Texture2D;
		if (t == null) {
			EditorUtility.DisplayDialog ("title","message","ok");
			return;
		}
		string path = EditorUtility.SaveFilePanel ("保存", "dir", t.name, "jpg");
		if (path.Length > 0) {
			byte[] b = t.EncodeToJPG(90);
			File.WriteAllBytes (path, b);
		}
	}


	//http://www.ceeger.com/Script/TerrainData/TerrainData.html
	[MenuItem("MyMenu/saveTerrainData")]
	static void saveTerrainData(){
		Object o = Selection.activeObject;
		TerrainData t = o as TerrainData;
		if (t == null) {
			EditorUtility.DisplayDialog ("title","message","ok");
			return;
		}
		Debug.Log ("alphamap,"+t.alphamapTextures.Length);
		Debug.Log ("splat,"+t.splatPrototypes.Length);
		SplatPrototype s = t.splatPrototypes [0];
	}

	[MenuItem("MyMenu/saveTerrain")]
	static void saveTerrain(){
		Object o = Selection.activeObject;
		GameObject go = o as GameObject;
		if (go == null) {
			return;
		}
		Terrain t= go.GetComponent <Terrain>() as Terrain;
		if (t == null) {
			EditorUtility.DisplayDialog ("title", "message", "ok");
			return;
		}
		float[,] data= t.terrainData.GetHeights (0, 0, t.terrainData.heightmapWidth, t.terrainData.heightmapHeight);
		Debug.Log (data);
	}

	[MenuItem("MyMenu/findAll")]
	static void saveFindAll(){
		Scene scene = SceneManager.GetActiveScene ();
		GameObject[] gos = scene.GetRootGameObjects ();
		foreach (GameObject go in gos) {
			Debug.Log (go.name);
		}
		//GameObject.f
		//Resources.FindObjectsOfTypeAll(
	}
}

高度图

blog comments powered by Disqus