Skip to content

Instantly share code, notes, and snippets.

@Kailang
Created Oct 9, 2018
Embed
What would you like to do?
using UnityEngine;
public class Test : MonoBehaviour {
public TpAtlas atlas;
public TpSprite sprite;
void OnEnable() {
var text = Resources.Load<TextAsset>("ui").text;
atlas = JsonUtility.FromJson<TpAtlas>(text);
var size = atlas.size;
foreach (var s in atlas.sprites) {
if (s.name == "block_fade") {
sprite = s;
break;
}
}
var material = new Material(Shader.Find("Sprites/Default"));
var texture = new Texture2D(0, 0);
texture.LoadImage(Resources.Load<TextAsset>(atlas.name).bytes);
texture.Apply();
material.mainTexture = texture;
GetComponent<MeshRenderer>().sharedMaterial = material;
var mesh = GetComponent<MeshFilter>().mesh;
mesh.MarkDynamic();
// v0 - v1
// | \ |
// v3 - v2
var pivot = sprite.pivot;
var quad = sprite.quad;
var uv = sprite.uv;
var vertices = new Vector3[4];
var uvs = new Vector2[4];
vertices[0].Set(-pivot.x + quad.x, pivot.y - quad.y, 0);
vertices[1].Set(-pivot.x + quad.x + quad.w, pivot.y - quad.y, 0);
vertices[2].Set(-pivot.x + quad.x + quad.w, pivot.y - quad.y - quad.h, 0);
vertices[3].Set(-pivot.x + quad.x, pivot.y - quad.y - quad.h, 0);
if (!sprite.rotated) {
uvs[0].Set(uv.x / size.x, -uv.y / size.y);
uvs[1].Set((uv.x + uv.w) / size.x, -uv.y / size.y);
uvs[2].Set((uv.x + uv.w) / size.x, (-uv.y - uv.h) / size.y);
uvs[3].Set(uv.x / size.x, (-uv.y - uv.h) / size.y);
} else {
uvs[0].Set((uv.x + uv.w) / size.x, -uv.y / size.y);
uvs[1].Set((uv.x + uv.w) / size.x, (-uv.y - uv.h) / size.y);
uvs[2].Set(uv.x / size.x, (-uv.y - uv.h) / size.y);
uvs[3].Set(uv.x / size.x, -uv.y / size.y);
}
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = new [] { 0, 1, 2, 0, 2, 3 };
Debug.Log(Time.time);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment