private void ActionPaint(int x, int y, int n) { Text label = transform.GetChild(n).GetChild(0).GetComponent <Text>(); label.text = label.text != "0" && paintMask == 0 ? "" : paintMask.ToString(); SetTileColor(n, paintMask == 0 ? Color.black : Color.white); pathfinder.SetType(x, y, paintMask); cache.SetType(x, y, paintMask); }
private void OnEnable() { cache = JsonUtility.FromJson <TilemapCache>(cacheAsset.text); if (cache == null) { cache = new TilemapCache(gridColumns, gridRows, FLOOR); } if (cache.width != gridColumns || cache.height != gridRows || cache.tilemap.Length != gridColumns * gridRows) { cache = new TilemapCache(gridColumns, gridRows, FLOOR); } pathfinder = new TwoDeePathfinder(); pathfinder.Create(gridColumns, gridRows, FLOOR); stateLabels = new Dictionary <int, string> { { 0, "Paint" }, { 1, "Path" }, }; if (Application.isPlaying) { for (int c = transform.childCount - 1; c >= 0; --c) { Destroy(transform.GetChild(c).gameObject); } } vWorldUnits = Camera.main.orthographicSize * 2; hWorldUnits = vWorldUnits * Camera.main.aspect; tileWidth = hWorldUnits / gridColumns; tileHeight = vWorldUnits / gridRows; int tot = gridColumns * gridRows; for (int i = 0; i < tot; ++i) { GameObject tile = new GameObject("Tile_" + i.ToString("000")); SpriteRenderer sr = tile.AddComponent <SpriteRenderer>(); sr.sprite = tileSprite; GameObject label = new GameObject("Label"); label.AddComponent <Canvas>(); label.AddComponent <CanvasRenderer>(); CanvasScaler cs = label.AddComponent <CanvasScaler>(); cs.dynamicPixelsPerUnit = 100f; Text lText = label.AddComponent <Text>(); lText.fontSize = 0; lText.supportRichText = false; lText.alignment = TextAnchor.MiddleCenter; lText.color = new Color(105f / 255f, 105f / 255f, 105f / 255f); lText.raycastTarget = false; lText.resizeTextForBestFit = true; lText.resizeTextMinSize = 0; lText.font = labelFont; RectTransform rt = label.GetComponent <RectTransform>(); rt.sizeDelta = Vector2.one * 0.5f; rt.position = new Vector3(0.5f, 0.5f, 0f); label.transform.SetParent(tile.transform); tile.transform.SetParent(transform); tile.transform.localScale = new Vector3(tileWidth, tileHeight, 1f); int x = -1; int y = -1; Grid.GetPoint(gridColumns, i, out x, out y); tile.transform.localPosition = new Vector3( x * tileWidth - hWorldUnits * 0.5f, y * tileHeight - vWorldUnits * 0.5f, 0); int type = cache.tilemap[i]; pathfinder.SetType(x, y, type); if (type > 0) { lText.text = type.ToString(); } SetTileColor(sr, type == 0 ? Color.black : Color.white); } }