Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Kailang
Created October 9, 2018 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kailang/e158fcde57d0d75b4435a2eaa75d4353 to your computer and use it in GitHub Desktop.
Save Kailang/e158fcde57d0d75b4435a2eaa75d4353 to your computer and use it in GitHub Desktop.
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