public void SetSpriteWithOffset() { SpriteWithOffset[] os = SpriteWithOffset.CreateFromJson(jsonFile.text); Dictionary <string, SpriteWithOffset> d = new Dictionary <string, SpriteWithOffset>(); foreach (var o in os) { d.Add(o.name, o); } foreach (var sr in spriteRenderers) { SpriteWithOffset o = null; if (!d.TryGetValue(sr.sprite.name, out o)) { Debug.LogWarning(sr.sprite.name + " cant found in json"); continue; } float pixelsPerUnit = o.width / sr.bounds.size.x; //Debug.Log(pixelsPerUnit); Vector2 leftTop = new Vector2(sr.bounds.min.x, sr.bounds.max.y); var go = sr.gameObject; go.transform.position += VectorUtils.V23(VectorUtils.V32(transform.position) - leftTop); var offset = new Vector2(o.x, -o.y) / (pixelsPerUnit); go.transform.position += VectorUtils.V23(offset); } }
private static IEnumerable <SpriteWithOffset> fromJson(string json) { Regex r = new Regex(pat, RegexOptions.None); Match m = r.Match(json); while (m.Success) { var s = new SpriteWithOffset(); s.name = m.Groups[1].Value; s.x = float.Parse(m.Groups[2].Value); s.y = float.Parse(m.Groups[3].Value); s.width = float.Parse(m.Groups[4].Value); s.height = float.Parse(m.Groups[5].Value); yield return(s); m = m.NextMatch(); } }