编译器扩展-MergeImage

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class ImageMergeWindow :EditorWindow  {

	//注意图片需要放到Resources目录下
	[MenuItem("Window/S_ImageMerge")]
	static void S_ImageMerge()
	{
		ImageMergeWindow window = EditorWindow.GetWindow<ImageMergeWindow> ();
		window.Show ();
	}


	private int list;
	private float width;
	private float height;
	private GameObject parentObj;

	void OnGUI()
	{
		LayoutWindow ();
		OnCreateBtnClick ();
	}

	void LayoutWindow()
	{
		EditorGUILayout.BeginHorizontal ();
		EditorGUILayout.LabelField ("名称:",GUILayout.Width(30));
		name = EditorGUILayout.TextField (name,GUILayout.Width(160));
		EditorGUILayout.EndHorizontal ();

		EditorGUILayout.BeginHorizontal ();
		EditorGUILayout.LabelField ("宽:",GUILayout.Width(30));
		width = EditorGUILayout.FloatField (width,GUILayout.Width(60));
		EditorGUILayout.LabelField ("高:",GUILayout.Width(30));
		height = EditorGUILayout.FloatField ( height,GUILayout.Width(60));
		EditorGUILayout.EndHorizontal ();

		EditorGUILayout.BeginHorizontal ();
		EditorGUILayout.LabelField ("列:",GUILayout.Width(30));
		list = EditorGUILayout.IntField ( list,GUILayout.Width(60));
		EditorGUILayout.EndHorizontal ();
	}

	void OnCreateBtnClick()
	{

		if (name == null) {
			return;
		}

		if (list == 0) {
			return;
		}

		if (width == 0 || height == 0) {
			return;
		}

		if (GUILayout.Button ("创建")) {
			string fullPath = "Assets/Resources/"+ name;
			//获取指定路径下面的所有资源文件
			if (Directory.Exists (fullPath)) {
				DirectoryInfo direction = new DirectoryInfo (fullPath);
				FileInfo[] files = direction.GetFiles ("*png", SearchOption.AllDirectories);
				FileInfo[] newFiles = new FileInfo[files.Length];
				//排序
				for (int i = 0; i < files.Length; i++) {
				//	Debug.Log ("排序前" +files [i].Name);
					int index;
					string nameNum = files [i].Name.Replace ((name + "_"), "");
					nameNum = nameNum.Replace (".png", "");
				//	Debug.Log ("nameNum:    " + nameNum);
					int.TryParse(nameNum,out index);
					//Debug.Log ("index:"+index);
					newFiles[index-1] = files[i];
				
				}


			

				if (newFiles.Length > 0) {
					parentObj = new GameObject ();
					parentObj.name = name;
				}

				for (int r = 0; r < newFiles.Length/ list; r++)//这是行
				{
					for (int l = 0; l < list; l++) {
						string path = (name + "/" + newFiles [l + r * list].Name).Replace(".png","");
						Debug.Log (path);
						Texture2D texture = (Texture2D)Resources.Load (path);
						texture.name = newFiles [l + r * list].Name;
						Debug.Log ("textureName" + texture.name);
						GameObject subObj = GameObject.CreatePrimitive (PrimitiveType.Quad);
						subObj.transform.localScale = new Vector3 (width, height, 1);
						subObj.transform.localPosition = new Vector3  (width *(l%list), -height * r, 1);

						Material mat = new Material (Shader.Find("Transparent/Diffuse"));  
						mat.mainTexture = texture;
						AssetDatabase.CreateAsset (mat, ("Assets/Resources/"+ name+"/"+ newFiles [l + r * list].Name + ".mat"));
						subObj.GetComponent<Renderer> ().material = mat;
						subObj.transform.SetParent (parentObj.transform);
					}
				}
				parentObj.transform.Rotate (0, 180, 0);
				PrefabUtility.CreatePrefab ("Assets/Prefabs/" + parentObj.name + ".prefab", parentObj);
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值