public void UpdateTile(bool beingUpdated = false, bool isUpdater = false) { GetNeighbours(out neighbours); mask = TileBitMask.None; //This is for updating neighbours. for (int i = 0; i < neighbours.Length; i++) { //Stops stackoverflow. if (beingUpdated) { break; } if (neighbours[i] != null) { if (neighbours[i].obj != null) { neighbours[i].obj.GetComponent <Tile>().UpdateTile(true); } } } //Don't continue if we're an updater tile(a tile that updates others if (isUpdater) { return; } if (neighbours[0] != null) { mask |= TileBitMask.Top; } if (neighbours[1] != null) { mask |= TileBitMask.Right; } if (neighbours[2] != null) { mask |= TileBitMask.Bottom; } if (neighbours[3] != null) { mask |= TileBitMask.Left; } //Ugly hack. Since selecting everything via the unity flag drawer turns mask into -1, I need to make sure when updating, we use -1 instead of 15 to get the correct key inside the dictionary if ((int)mask == 15) { mask |= (TileBitMask)(-1); } GameObject newTile = rules.autoTileRulesDictionary.ContainsKey(mask) ? rules.autoTileRulesDictionary[mask].tile : rules.defaultTile.tile; float rotationDiff = rules.autoTileRulesDictionary.ContainsKey(mask) ? rules.autoTileRulesDictionary[mask].rotationDiff : rules.defaultTile.rotationDiff; bool zRotation = rules.autoTileRulesDictionary.ContainsKey(mask) ? rules.autoTileRulesDictionary[mask].ZRotation : rules.defaultTile.ZRotation; //Get the actual prefab from the assets (otherwise we'll be trying to instantiate the wrong thing) GameObject basePrefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource(newTile); GameObject tile = PrefabUtility.InstantiatePrefab(basePrefab) as GameObject; if (tile) { //Although we can just use tilemap.PlaceTile, this gives us much finer control. (and at the current state, using placetile here will cause an infinite loop of updating, crashing unity) Tile t = tile.GetComponent <Tile>(); tile.transform.position = transform.position; tile.transform.rotation *= !zRotation?Quaternion.Euler(0, rotationDiff, 0) : Quaternion.Euler(0, 0, rotationDiff); tile.transform.parent = tilemap.transform; if (tile.GetComponent <Renderer>()) { tile.GetComponent <Renderer>().sharedMaterial = GetComponent <Renderer>().sharedMaterial; } t.gridPosition = gridPosition; t.tilemap = tilemap; t.tilemap.tiles[t.gridPosition].obj = tile; t.neighbours = neighbours; t.mask = mask; t.rules = rules; tile.hideFlags = hideFlags; DestroyImmediate(gameObject); } }
/// <summary> /// Create a new autotile /// </summary> /// <param name="t">GameObject tile</param> /// <param name="r">Rotation of the tile</param> /// <param name="z">Should we rotate by Z instead of Y? (for when you f**k up blender exporting, like I did)</param> /// <param name="tM">Should we rotate by Z instead of Y? (for when you f**k up blender exporting, like I did)</param> public AutoTileData(GameObject t, float r, bool z, TileBitMask tM = TileBitMask.None) { tile = t; rotationDiff = r; ZRotation = z; }