本文整理汇总了C#中UnityEngine.Texture2D.PackTextures方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.PackTextures方法的具体用法?C# Texture2D.PackTextures怎么用?C# Texture2D.PackTextures使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Texture2D
的用法示例。
在下文中一共展示了Texture2D.PackTextures方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateMyAtlas
UITextureAtlas CreateMyAtlas(string AtlasName, Material BaseMat, string[] sPritesNames)
{
var size = 1024;
Texture2D atlasTex = new Texture2D(size, size, TextureFormat.ARGB32, false);
Texture2D[] textures = new Texture2D[sPritesNames.Length];
Rect[] rects = new Rect[sPritesNames.Length];
for(int i = 0; i < sPritesNames.Length; i++)
{
textures[i] = ResourceLoader.loadTexture(0, 0, sPritesNames[i] + ".png");
}
rects = atlasTex.PackTextures(textures, 2, size);
UITextureAtlas atlas = ScriptableObject.CreateInstance<UITextureAtlas>();
Material material = Material.Instantiate(BaseMat);
material.mainTexture = atlasTex;
atlas.material = material;
atlas.name = AtlasName;
for (int i = 0; i < sPritesNames.Length; i++)
{
var spriteInfo = new UITextureAtlas.SpriteInfo()
{
name = sPritesNames[i],
texture = atlasTex,
region = rects[i]
};
atlas.AddSprite(spriteInfo);
}
return atlas;
}
示例2: CreateAtlas
/// <summary>
/// Create a UIAtlas on runtime from a list of Texture2Ds
/// </summary>
public static UIAtlas CreateAtlas(string atlasName, GameObject parent, List<Texture2D> textures, List<string> names)
{
Logger.Debug(TAG, "Generating UIAtlas: {0}", atlasName);
// Pack textures
int maxSize = SystemInfo.maxTextureSize;
Texture2D atlasTexture = new Texture2D(maxSize, maxSize);
Rect[] rects = atlasTexture.PackTextures(textures.ToArray(), 0, maxSize);
// Create new empty GameObject with UIAtlas component
UIAtlas atlas = NGUITools.AddChild<UIAtlas>(parent);
atlas.name = atlasName;
// Set material
atlas.coordinates = UIAtlas.Coordinates.TexCoords;
atlas.spriteMaterial = new Material(Shader.Find("Unlit/Transparent Colored"));
atlas.spriteMaterial.mainTexture = atlasTexture;
// Add sprites
for (int i = 0; i < rects.Length; i++) {
UIAtlas.Sprite sprite = new UIAtlas.Sprite();
sprite.inner = rects[i];
sprite.outer = rects[i];
sprite.name = names[i];
atlas.spriteList.Add(sprite);
}
// Return reference to the UIAtlas script
return atlas;
}
示例3: makeAtlas
public static void makeAtlas(Texture2D[] textures)
{
if (textures != null && textures.Length > 1)
{
// Make our atlas gameobject with a blank atlas script on it.
GameObject go = new GameObject("TEST_ATLAS");
go.AddComponent<Atlas>();
// The blank texture is replaced with the packed texture.
Texture2D tex = new Texture2D(2, 2);
// Now pack the textures
Rect[] UVs = tex.PackTextures(textures, 2, 4096);
// Create out spriteAnimation components onto the atlas with height/width equal to the source.
for (int i = 0; i < UVs.Length; i++)
{
SpriteAnimation anim = go.AddComponent<SpriteAnimation>();
anim.setData(textures[i].name, UVs[i]);
}
FileStream fs = new FileStream(Application.dataPath + "/Resources/Sprites/Atlas_Sprite.png", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(tex.EncodeToPNG());
bw.Close();
fs.Close();
//PrefabUtility.CreatePrefab(Application.dataPath + "/Resources/Atlases/" + go.name + ".prefab", go);
}
else
{
Debug.LogWarning("Given Textures are null or singular.");
}
}
示例4: GeneratePackage
public void GeneratePackage()
{
GameObject pPackage = new GameObject ("obj_" + PackageName);
PackageData pData = pPackage.AddComponent<PackageData> ();
pData.TextureNames = new string[Textures.Length];
for (int i = 0; i < Textures.Length; i++)
{
string pTexpath = AssetDatabase.GetAssetPath(Textures[i]);
ConfigureForPacker(pTexpath);
pData.TextureNames[i] = pTexpath;
}
//total Texture
Texture2D pTex = new Texture2D (1, 1, TextureFormat.ARGB32, false);
pData.TextureUVs = pTex.PackTextures (Textures,padding,4096);
string pTexAssetPath = AssetDatabase.GenerateUniqueAssetPath("Assets/" + PackageName + ".png");
byte[] bytes = pTex.EncodeToPNG ();
System.IO.File.WriteAllBytes (pTexAssetPath,bytes);
bytes = null;
UnityEngine.Object.DestroyImmediate (pTex);
AssetDatabase.ImportAsset (pTexAssetPath);
pData.TotalTexture = AssetDatabase.LoadAssetAtPath (pTexAssetPath, typeof(Texture2D)) as Texture2D;
ConfigureForPacker (AssetDatabase.GetAssetPath (pData.TotalTexture));
pTexAssetPath = AssetDatabase.GenerateUniqueAssetPath("Assets/data_" + PackageName + ".prefab");
Object prefab = PrefabUtility.CreateEmptyPrefab (pTexAssetPath);
PrefabUtility.ReplacePrefab (pPackage, prefab, ReplacePrefabOptions.ConnectToPrefab);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
DestroyImmediate (pPackage);
}
示例5: BuildTextureAtlas
/// <summary>
/// Builds the texture atlas.
/// </summary>
public static void BuildTextureAtlas()
{
// Build texture array
Texture2D[] atlasTextures = new Texture2D[textures.Count];
foreach (KeyValuePair<int, Texture2D> texture in textures)
{
atlasTextures[texture.Key] = texture.Value;
}
// Build atlas
Texture2D _textureAtlas = new Texture2D (4096, 4096, TextureFormat.RGBA32, false);
Rect[] uvRects = _textureAtlas.PackTextures (atlasTextures, 0);
textureAtlas = new Texture2D(_textureAtlas.width, _textureAtlas.height, TextureFormat.RGBA32, false);
textureAtlas.SetPixels (0,0,_textureAtlas.width, _textureAtlas.height, _textureAtlas.GetPixels (0,0,_textureAtlas.width, _textureAtlas.height));
// Set texture atlas properties
textureAtlas.anisoLevel = 9;
textureAtlas.filterMode = FilterMode.Point;
textureAtlas.Apply ();
// Save uvs
textureCoordinates = new Dictionary<int, Vector2[]> ();
foreach (KeyValuePair<int, Texture2D> texture in textures)
{
textureCoordinates.Add (texture.Key,new Vector2[]
{
new Vector2(uvRects[texture.Key].x, uvRects[texture.Key].y),
new Vector2(uvRects[texture.Key].x+uvRects[texture.Key].width, uvRects[texture.Key].y),
new Vector2(uvRects[texture.Key].x+uvRects[texture.Key].width, uvRects[texture.Key].y+uvRects[texture.Key].height),
new Vector2(uvRects[texture.Key].x, uvRects[texture.Key].y+uvRects[texture.Key].height)
});
}
}
示例6: OnGUI
void OnGUI()
{
GUILayout.Label("HairyPlotter Atlas Creator", EditorStyles.boldLabel);
if (textures == null)
textures = new Texture2D[0];
assetName = EditorGUILayout.TextField("Asset Name", assetName);
textureSize = EditorGUILayout.IntField("Max Size", textureSize);
padding = EditorGUILayout.IntField("Padding", padding);
System.Array.Resize(ref textures, EditorGUILayout.IntField("Textures", textures.Length));
for (int i = 0; i < textures.Length; ++i)
{
textures[i] = (Texture2D)EditorGUILayout.ObjectField(textures[i], typeof(Texture2D), false);
}
if (assetName != "" && textures.Where(x => x != null).Count() > 0)
{
if (GUILayout.Button("Create Atlas", EditorStyles.miniButton))
{
Texture2D atlas = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, true);
atlas.PackTextures(textures.Where(x => x != null).ToArray(), padding, textureSize);
char dirSep = System.IO.Path.DirectorySeparatorChar;
string fileName = "Assets" + dirSep + assetName + ".png";
System.IO.File.WriteAllBytes(fileName, atlas.EncodeToPNG());
AssetDatabase.ImportAsset(fileName);
}
}
}
示例7: CreateTextureAtlas
public static UITextureAtlas CreateTextureAtlas(string atlasName, string[] spriteNames, string assemblyPath)
{
int maxSize = 1024;
Texture2D texture2D = new Texture2D(maxSize, maxSize, TextureFormat.ARGB32, false);
Texture2D[] textures = new Texture2D[spriteNames.Length];
Rect[] regions = new Rect[spriteNames.Length];
for (int i = 0; i < spriteNames.Length; i++)
textures[i] = TextureLoader.LoadTextureFromAssembly(assemblyPath + spriteNames[i] + ".png");
regions = texture2D.PackTextures(textures, 2, maxSize);
UITextureAtlas textureAtlas = ScriptableObject.CreateInstance<UITextureAtlas>();
Material material = UnityEngine.Object.Instantiate<Material>(UIView.GetAView().defaultAtlas.material);
material.mainTexture = texture2D;
textureAtlas.material = material;
textureAtlas.name = atlasName;
for (int i = 0; i < spriteNames.Length; i++) {
UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo {
name = spriteNames[i],
texture = textures[i],
region = regions[i],
};
textureAtlas.AddSprite(item);
}
return textureAtlas;
}
示例8: initializeAtlas
private void initializeAtlas()
{
Texture2D[] cubeTextures = new Texture2D[9];
/*
cubeTextures[1] = Resources.Load("Textures/hiDefGrass") as Texture2D;
cubeTextures[2] = Resources.Load("Textures/sidegrass") as Texture2D;
cubeTextures[3] = Resources.Load("Textures/hiDefDirt") as Texture2D;
cubeTextures[4] = Resources.Load("Textures/hiDefDirt") as Texture2D;
cubeTextures[5] = Resources.Load("Textures/hiDefDirt") as Texture2D;
cubeTextures[6] = Resources.Load("Textures/hiDefStone") as Texture2D;
cubeTextures[7] = Resources.Load("Textures/hiDefStone") as Texture2D;
cubeTextures[8] = Resources.Load("Textures/hiDefStone") as Texture2D;
*/
cubeTextures[1] = Resources.Load("Textures/dokuGrass") as Texture2D;
cubeTextures[2] = Resources.Load("Textures/dokuGrassSide") as Texture2D;
cubeTextures[3] = Resources.Load("Textures/dokuDirt") as Texture2D;
cubeTextures[4] = Resources.Load("Textures/dokuDirt") as Texture2D;
cubeTextures[5] = Resources.Load("Textures/dokuDirt") as Texture2D;
cubeTextures[6] = Resources.Load("Textures/dokuStone") as Texture2D;
cubeTextures[7] = Resources.Load("Textures/dokuStone") as Texture2D;
cubeTextures[8] = Resources.Load("Textures/dokuStone") as Texture2D;
textureAtlas = new Texture2D(4096, 4096);
atlasUVs = textureAtlas.PackTextures(cubeTextures, 0);
textureAtlas.Apply();
}
示例9: TextureIndex
public TextureIndex()
{
List<Texture2D> atlasTextures = new List<Texture2D>();
// Load all files in Textures folder
atlasTextures.AddRange(Resources.LoadAll<Texture2D>(Config.Directories.TextureFolder));
//Load and generate all the connected textures
List<Texture2D> connectedTextures = new List<Texture2D>();
connectedTextures.AddRange(Resources.LoadAll<Texture2D>(Config.Directories.ConnectedTextureFolder));
if (connectedTextures.Count!=0)
atlasTextures.AddRange(ConnectedTextures.GenerateConnectedTextures(connectedTextures));
// Create new atlas
atlas = new Texture2D(8192, 8192);
atlas.filterMode = FilterMode.Point;
// Generate atlas
Rect[] rects = atlas.PackTextures(atlasTextures.ToArray(), 0, 8192, false);
// Add each atlas entry into texture dictionary
for (int i = 0; i < atlasTextures.Count; i++)
{
if (!atlasTextures[i])
continue;
string[] fileName = atlasTextures[i].name.ToString().Split('-');
Rect texture = rects[i];
string textureName = fileName[0];
int connectedTextureType = -1;
//Get the properties associated with this texture
for (int n = 1; n < fileName.Length; n++)
{
switch (fileName[n][0])
{
case 'c':
int.TryParse(fileName[n].Substring(1), out connectedTextureType);
break;
default:
break;
}
}
//Create a texture collection with this name if it doesn't exist already
TextureCollection collection;
if (!textures.TryGetValue(textureName, out collection))
{
collection = new TextureCollection(textureName);
textures.Add(textureName, collection);
}
collection.AddTexture(texture, connectedTextureType);
}
}
示例10: CreateSpriteSheetAtlasSliced
private static void CreateSpriteSheetAtlasSliced() {
List<Texture2D> textures = new List<Texture2D> ();
Object[] selectedObjects = Selection.objects;
foreach (Object obj in selectedObjects) {
if(obj is Texture2D){
TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(obj));
if (importer != null) {
importer.textureType= TextureImporterType.Advanced;
importer.npotScale= TextureImporterNPOTScale.None;
importer.mipmapEnabled=false;
importer.isReadable=true;
AssetDatabase.ImportAsset (AssetDatabase.GetAssetPath(obj));
AssetDatabase.Refresh();
}
textures.Add(obj as Texture2D);
}
}
textures.Sort ((x, y) => string.Compare(x.name, y.name));
string path=AssetDatabase.GetAssetPath(textures[0]);
path=path.Remove(path.LastIndexOf('/'));
Texture2D atlas = new Texture2D(4096, 4096, TextureFormat.ARGB32, false);
Color[] colors=new Color[4096*4096];
for(int i=0; i< colors.Length;i++){
colors[i]=Color.clear;
}
atlas.SetPixels(colors);
atlas.Apply();
Rect[] rects=atlas.PackTextures(textures.ToArray(), 0,false);
byte[] bytes = atlas.EncodeToPNG();
System.IO.File.WriteAllBytes(path+ "/spritesheet.png", bytes);
AssetDatabase.Refresh();
TextureImporter tempImporter = (TextureImporter)AssetImporter.GetAtPath(path+"/spritesheet.png");
if (tempImporter != null) {
tempImporter.textureType = TextureImporterType.Sprite;
tempImporter.spriteImportMode=SpriteImportMode.Multiple;
tempImporter.maxTextureSize=4096;
int count=textures.Count;
SpriteMetaData[] meta= new SpriteMetaData[count];
for(int i=0; i< count;i++){
meta[i].name=i.ToString();
meta[i].alignment = (int)SpriteAlignment.Center;
meta[i].pivot=Vector2.zero;
meta[i].rect = rects[i];
}
tempImporter.spritesheet=meta;
AssetDatabase.ImportAsset (path+"/spritesheet.png");
AssetDatabase.Refresh();
}
}
示例11: AtlasTextures
/// <summary>
/// Creates a single texture atlas from the provided source textures.
/// </summary>
/// <param name="textures">Textures to combine into one</param>
/// <param name="textureSize">Size of the texture atlas to create</param>
/// <param name="rectAreas">Rect locations of each texture</param>
/// <returns>The created atlased texture</returns>
/// <example>
/// This will atlas the two textures, TextureA and TextureB, into one efficient texture map.
/// <code>
/// using System;
/// using UnityEngine;
/// using CivGrid;
///
/// class TextureTest : MonoBehaviour
/// {
/// Texture2D[] texturesToCombine;
/// public Texture2D texture1;
/// public Texture2D texture2;
///
/// Texture2D atlasedTexture;
/// Rect[] rectLocations;
///
/// void Start()
/// {
/// texturesToCombine = new Texture2D[2];
/// texturesToCombine[0] = texture1;
/// texturesToCombine[1] = texture2;
///
/// atlasedTexture = TexturePacker.AtlasTextures(texturesToCombine, 2048, out rectLocations);
/// }
/// }
/// </code>
/// </example>
public static Texture2D AtlasTextures(Texture2D[] textures, int textureSize, out Rect[] rectAreas)
{
//creates return texture atlas
Texture2D packedTexture = new Texture2D(textureSize, textureSize);
//packs all source textures into one
rectAreas = packedTexture.PackTextures(textures, 0, textureSize);
packedTexture.Apply();
//returns texture atlas
return packedTexture;
}
示例12: Awake
void Awake()
{
Material newMaterial = new Material(Shader.Find("Sprites/ClipArea"));
Texture2D[] textures = new Texture2D[textNames.Length];
int width = 0;
int height = 0;
float deep = 0.01f;
for(int i = 0; i < textNames.Length; ++i)
{
textures[i] = Resources.Load<Texture2D>(textNames[i]);
width += textures[i].width;
height += textures[i].height;
}
packedTexture = new Texture2D(1024,1024);
Rect[] uvs = packedTexture.PackTextures(textures,0,1024);
for(int i = 0; i < uvs.Length; ++i)
{
GameObject gameObject = new GameObject();
gameObject.name = "item_" + i;
gameObject.transform.position = new Vector3(-300 + 150 * i ,0);
Mesh mesh = CreateMesh(45, 45);
MeshRenderer renderer = gameObject.AddComponent<MeshRenderer>();
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
mesh.uv = RectToUV(uvs[i]);
meshFilter.mesh = mesh;
newMaterial.mainTexture = packedTexture;
renderer.material = newMaterial;
}
//renderer.material = newMaterial;
/*
packedTexture = new Texture2D(1024,1024);
Rect[] uvs = packedTexture.PackTextures(textures,0,1024);
newMaterial.mainTexture = packedTexture;
Vector2[] uva,uvb;
for (int j=0;j < filters.Length;j++) {
filters[j].gameObject.renderer.material=newMaterial;
uva = (Vector2[])(((MeshFilter)filters[j]).mesh.uv);
uvb = new Vector2[uva.Length];
for (int k=0;k < uva.Length;k++){
uvb[k]=new Vector2((uva[k].x*uvs[j].width)+uvs[j].x, (uva[k].y*uvs[j].height)+uvs[j].y);
}
((MeshFilter)filters[j]).mesh.uv=uvb;
}
*/
// Test();
}
示例13: BuildAtlas
static void BuildAtlas( Texture2D[] textures )
{
Debug.Log( textures.Length );
Texture2D output = new Texture2D( 1024, 1024 );
output.PackTextures( textures, 0, 1024 );
using ( var fs = new FileStream( "Assets/texMap.png", FileMode.Create ) )
{
byte[] pngBytes = output.EncodeToPNG();
fs.Write( pngBytes, 0, pngBytes.Length );
fs.Close();
}
}
示例14: BakeEmojiAtlas
private static void BakeEmojiAtlas()
{
string pathToEmojis = Application.dataPath + "/Textures/Emojis";
string pathToEmojiInfo = Application.dataPath + "/EmojiInfo/info.txt";
string pathToBakedAtlas = Application.dataPath + "/Textures/Baked/BakedEmojis.png";
string[] emojiFileNames = Directory.GetFiles(pathToEmojis, "*.png");
// Load each PNG file as separate Texture2D
Texture2D[] emojiTextures = new Texture2D[emojiFileNames.Length];
for (int i = 0; i < emojiFileNames.Length; i++)
{
// Create a texture. Texture size does not matter, since
// LoadImage will replace with with incoming image size.
Texture2D tex = new Texture2D(2, 2);
if (!tex.LoadImage(File.ReadAllBytes(emojiFileNames[i])))
{
Debug.LogError("Cannot load file " + emojiFileNames[i] + " via tex.LoadImage!!!");
return;
}
emojiTextures[i] = tex;
}
// Create atlas
Texture2D atlas = new Texture2D(2048, 2048);
Rect[] rects = atlas.PackTextures(emojiTextures, 1, 2048);
// Save atlas
byte[] atlasBytes = atlas.EncodeToPNG();
File.WriteAllBytes(pathToBakedAtlas, atlasBytes);
// Save EmojiInfo
StringBuilder sb = new StringBuilder();
for (int i = 0; i < emojiFileNames.Length; i++)
{
sb.AppendLine(Path.GetFileNameWithoutExtension(emojiFileNames[i]) + " " + rects[i].x + " " + rects[i].y + " " + rects[i].width + " " + rects[i].height );
}
File.WriteAllText(pathToEmojiInfo, sb.ToString());
Debug.Log("Baked " + emojiFileNames.Length + " emojis into " + pathToBakedAtlas);
}
示例15: GenerateAtlas
void GenerateAtlas()
{
foreach (Texture2D t in textures)
ConfigureForAtlas(AssetDatabase.GetAssetPath(t));
Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
Rect[] uvs = texture.PackTextures(textures, padding, 4096);
string assetPath = AssetDatabase.GenerateUniqueAssetPath("Assets/" + AtlasName + ".png");
byte[] bytes = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(assetPath, bytes);
bytes = null;
UnityEngine.Object.DestroyImmediate(texture);
AssetDatabase.ImportAsset(assetPath);
texture = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)) as Texture2D;
ConfigureSpriteAtlas(texture, uvs);
}