Last active
November 8, 2015 12:46
-
-
Save Evsenev/e7a5ef707d4b0b3f5460 to your computer and use it in GitHub Desktop.
Создание текстуры из 4 кусоков
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
using System.IO; | |
public class TextureCreator : Editor { | |
[MenuItem("TEXTURE/Create Texture")] | |
static void CreateTexture(){ | |
Texture2D texture = new Texture2D(1024,1024); | |
Texture2D tex1 = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/1.jpg", typeof(Texture2D)); | |
Texture2D tex2 = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/2.jpg", typeof(Texture2D)); | |
Texture2D tex3 = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/3.jpg", typeof(Texture2D)); | |
Texture2D tex4 = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/4.jpg", typeof(Texture2D)); | |
DrawPixels(tex1, Postition.TopLeft, ref texture); | |
DrawPixels(tex2, Postition.TopRight, ref texture); | |
DrawPixels(tex3, Postition.BottomLeft, ref texture); | |
DrawPixels(tex4, Postition.BottomRight, ref texture); | |
byte[] pngData = texture.EncodeToPNG(); | |
if(pngData != null){ | |
File.WriteAllBytes("Assets/texture.png", pngData); | |
Debug.Log ("TEXTURE CREATED"); | |
} | |
AssetDatabase.Refresh(); | |
pngData = null; | |
} | |
enum Postition{ | |
TopLeft, TopRight, BottomLeft, BottomRight | |
} | |
static void DrawPixels (Texture2D tex, Postition pos, ref Texture2D result){ | |
int width = tex.width; | |
int height = tex.height; | |
int startX = 0; | |
int startY = 0; | |
switch (pos) | |
{ | |
case Postition.TopLeft: | |
height *= 2; | |
startY = height/2; | |
break; | |
case Postition.TopRight: | |
width *= 2; | |
height *= 2; | |
startX = width/2; | |
startY = height/2; | |
break; | |
case Postition.BottomLeft: | |
break; | |
case Postition.BottomRight: | |
width *= 2; | |
startX = width/2; | |
break; | |
} | |
for (int y = startY, GY = 0 ; y < height; ++y, ++GY) | |
for (int x = startX, GX = 0; x < width; ++x, ++GX) | |
result.SetPixel(x, y, tex.GetPixel(GX, GY)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment