public override void OnInspectorGUI() { StructureContainer container = (StructureContainer)target; DrawDefaultInspector(); if (GUILayout.Button("Generate ghost version")) { if (container.structure == null) { Debug.LogError("Could not generate ghost variant! PlacedStructure is null!"); return; } if (container.gridSize == null) { Debug.LogError("Could not generate ghost variant! Size is null!"); } if (container.ghostMaterial == null) { Debug.LogError("Could not generate ghost variant! Ghost material is null!"); return; } if (container.ghostStructure != null) { Debug.LogError("Could not generate ghost variant! Variant already exists!"); return; } GameObject ghostVariant = (GameObject)PrefabUtility.InstantiatePrefab(container.structure); MeshRenderer[] mrs = ghostVariant.GetComponentsInChildren <MeshRenderer>(); foreach (var mr in mrs) { mr.shadowCastingMode = ShadowCastingMode.Off; mr.material = container.ghostMaterial; } ghostVariant.GetComponent <PlacedStructure>().enabled = false; string path = AssetDatabase.GetAssetPath(container.structure); path = path.Substring(0, path.Length - 7); path += " Ghost.prefab"; container.ghostStructure = PrefabUtility.SaveAsPrefabAsset(ghostVariant, path); DestroyImmediate(ghostVariant); Debug.Log("Ghost variant generated!"); } }
public bool PlaceOn(Coords pos, StructureContainer structure, Direction rotation) { GameObject obj = structure.structure; PlacedStructure placedStructure = obj.GetComponent <PlacedStructure>(); Box orientedBox = structure.getBoundingBox().Offset(pos).WithOrientation(new Vector3(pos.X, 0, pos.Y), rotation); if (placedStructure == null) { return(false); } if (!IsEmptySpace(orientedBox)) { return(false); } GameObject copy = Instantiate(obj); copy.transform.position = pos.ToGlobalPosition(); Vector3 rot = copy.transform.eulerAngles; copy.transform.eulerAngles = new Vector3(rot.x, rotation.EulerRotation, rot.z); PlacedStructure structureComponent = copy.GetComponent <PlacedStructure>(); structureComponent.rotation = rotation; var maxX = Mathf.CeilToInt(orientedBox.GetMaxX()); var maxY = Mathf.CeilToInt(orientedBox.GetMaxY()); for (var x = Mathf.FloorToInt(orientedBox.GetMinX()); x < maxX; x++) { for (var y = Mathf.FloorToInt(orientedBox.GetMinY()); y < maxY; y++) { Coords tilePos = new Coords(x, y); Debug.DrawLine(tilePos.ToGlobalPosition(), tilePos.ToGlobalPosition() + Vector3.up * 7, Color.red, 9999); //TODO debug GetTileAt(tilePos).structure = copy; } } return(true); }