Script(C#):
using UnityEngine;
using System.Collections;
using UnityEditor;
public class Material : EditorWindow{
GUIStyle style = new GUIStyle();
SystemWindowsForm windowsForm = new SystemWindowsForm();
Vector2 scrollPositionBackGround;
[MenuItem("遊戲控制器視窗/2D/素材")]
static void materialWindowFunction() {
Material materialWindow = new Material();
materialWindow = (Material)EditorWindow.GetWindow(typeof(Material));
materialWindow.Show();
}
void OnGUI() {
// GUI 風格
style.fontSize = 18;
style.normal.textColor = Color.white;
// GUILayout 自訂位置
GUILayout.BeginArea(new Rect(0, 0, 170, 30));
GUILayout.Label("圖片", style);
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(0, 30, 170, 30));
GUILayout.Label("圖片路徑位置:", style);
GUILayout.EndArea();
// 背景圖片的路徑名稱
GUILayout.BeginArea(new Rect(0, 60, 200, 60));
scrollPositionBackGround = GUILayout.BeginScrollView(scrollPositionBackGround, GUILayout.Width(200),GUILayout.Height(60));
GUILayout.Box(windowsForm.pathTpye, style);
GUILayout.EndScrollView();
GUILayout.EndArea();
// 背景圖片讀取事件
EditorGUILayout.BeginHorizontal();
if (GUI.Button(new Rect(0, 130, 170, 30), GUIContent.none))
windowsForm.openFile();
GUILayout.BeginArea(new Rect(0, 130, 170, 30));
GUILayout.Label("背景圖片匯入", style);
GUILayout.EndArea();
EditorGUILayout.EndHorizontal();
// 背景圖片匯入事件
EditorGUILayout.BeginHorizontal();
if (GUI.Button(new Rect(180, 130, 50, 30), GUIContent.none))
{
if (windowsForm.pathTpye.Equals("No Image"))
windowsForm.messag("讀不到路徑");
else {
windowsForm.CodyImage();
}
}
GUILayout.BeginArea(new Rect(180, 130, 50, 30));
GUILayout.Label("確定", style);
GUILayout.EndArea();
EditorGUILayout.EndHorizontal();
}
}
-------------------------
Script (C#):
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using UnityEngine;
public class SystemWindowsForm{
OpenFileDialog openFileDialog = new OpenFileDialog();
// 檔案路徑
private string path = "No Image";
public string pathTpye {
get { return path; }
}
private string fileName;
/// <summary>
/// messageBox 訊息列
/// </summary>
/// <param string="訊息"></param>
public void messag(string message) {
MessageBox.Show(message);
}
/// <summary>
/// 開啟檔案
/// </summary>
public void openFile() {
openFileDialog.Title = "開啟檔案";
openFileDialog.Filter = "PNG Image|*.png|JPG Image|*.jpg";
openFileDialog.ShowDialog();
switch (openFileDialog.FilterIndex) {
case 1:
if (File.Exists(openFileDialog.FileName))
path = openFileDialog.FileName;
fileName = openFileDialog.SafeFileName;
break;
case 2:
if (File.Exists(openFileDialog.FileName))
path = openFileDialog.FileName;
fileName = openFileDialog.SafeFileName;
break;
}
}
public void CodyImage() {
string localPaht = UnityEngine.Application.dataPath + "/GameController/Textrue2D/Background/";
Bitmap image = new Bitmap(path);
// 翻轉
image.RotateFlip(RotateFlipType.Rotate180FlipX);
Texture2D readImage = new Texture2D(image.Width, image.Height);
for (int x = 0; x < image.Width; x++) {
for (int y = 0; y < image.Height; y++) {
readImage.SetPixel(x, y, new Color32(image.GetPixel(x, y).R,
image.GetPixel(x,y).G,
image.GetPixel(x,y).B,
image.GetPixel(x,y).A));
}
}
readImage.Apply();
byte[] imageDate = readImage.EncodeToPNG();
File.WriteAllBytes(localPaht + fileName, imageDate);
messag("匯入完成");
}
}
------------------
結果圖:
沒有留言:
張貼留言