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);
}
}
}
}
编译器扩展-MergeImage
最新推荐文章于 2024-08-10 07:44:30 发布