- Home /
Question by
chrisall76 · Jul 29, 2013 at 02:43 AM ·
meshtexture2drectatlas
Help with atlas.packtextures
Hello, today I'm trying to create a sprite solution and it seems I get the concept. But no matter what I try I can't get the sprite to be applied to the object's material.
SpriteAtlasHandler:
using UnityEngine;
using System.Collections;
public class SpriteAtlasHandler : MonoBehaviour {
public Texture2D[] atlasTextures;
public Rect[] rects;
// Use this for initialization
void Start () {
Texture2D atlas = new Texture2D(1024, 1024);
rects = atlas.PackTextures(atlasTextures, 2, 1024);
}
// Update is called once per frame
void Update () {
}
public void SetUV(int rectNum, bool Mirroed, Mesh mesh, int Direct, Texture texture){
Vector2[] newUV = new Vector2[mesh.uv.Length];
Vector2 start = new Vector2(rects[rectNum].x, rects[rectNum].y);
Vector2 size = new Vector2(rects[rectNum].width, rects[rectNum].height);
newUV[0] = start + size;
newUV[1] = start + Vector2.right * size.x;
newUV[2] = start + Vector2.up * size.y;
newUV[3] = start;
mesh.uv = newUV;
texture = atlasTextures[0];
}
}
SpriteHandler:
using UnityEngine;
using System.Collections;
public class SpriteHandler : MonoBehaviour {
public SpriteAtlasHandler atlasHandler;
public Mesh thisMesh;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
atlasHandler.SetUV(0, false, thisMesh, 0, renderer.material.mainTexture);
}
}
Comment