void ChangeGameObjectToBrushTypeWithUndo(Hex hex, HexBrush brush) { GameObject go = GameObject.Instantiate(brush.gameObject) as GameObject; HexBrush hb = go.GetComponent <HexBrush>(); go.transform.position = hex.transform.position; Hex newHex = go.AddComponent <Hex>(); newHex.data = hex.data.Clone(); newHex.data.walkType = hb.data.walkType; newHex.data.buildingType = hb.data.buildingType; newHex.data.eventType = hb.data.eventType; newHex.data.res = AssetDatabase.GetAssetPath(brush); newHex.HexPosition = new Vector2(newHex.data.x, newHex.data.y); Undo.RegisterCreatedObjectUndo(go, "HexBrushObject"); this.garbage.Add(go); Undo.RecordObject(map, "mapData"); var index = hex.data.id; map.hexs[index] = newHex; map.cells[index] = newHex.data; Undo.DestroyObjectImmediate(hex.gameObject); Undo.DestroyObjectImmediate(hb); }
private void OnGUI() { window.mapWidth = EditorGUILayout.IntField("mapWidth", window.mapWidth); window.mapHeight = EditorGUILayout.IntField("mapHeight", window.mapHeight); defaultBrush = (HexBrush)EditorGUILayout.ObjectField(defaultBrush, typeof(HexBrush), false); GameObject[] objs = null; var s = GUILayout.SelectionGrid(selectBrushBase, window.previewBases(out objs, 0), 3); if (defaultBrush == null) { defaultBrush = (objs[0] as GameObject).GetComponent <HexBrush>(); } if (s != selectBrushBase) { selectBrushBase = s; defaultBrush = (objs[s] as GameObject).GetComponent <HexBrush>(); } if (GUILayout.Button("Create")) { window.Clean(); window.CreateMap(); if (defaultBrush) { window.ChangeAllHexToBrushType(defaultBrush); } } }
public override void OnInspectorGUI() { HexBrush brush = (HexBrush)target; DrawDefaultInspector(); Event current = Event.current; if (current.type != EventType.KeyDown) { return; } switch (current.keyCode) { case KeyCode.LeftArrow: brush.lowerHealth(); current.Use(); break; case KeyCode.RightArrow: brush.raiseHealth(); current.Use(); break; default: break; } }
Hex ChangeGameObjectToBrushType(Hex hex, HexBrush brush) { GameObject go = GameObject.Instantiate(brush.gameObject) as GameObject; HexBrush hb = go.GetComponent <HexBrush>(); go.transform.position = hex.transform.position; Hex newHex = go.AddComponent <Hex>(); newHex.data = hex.data.Clone(); newHex.data.walkType = hb.data.walkType; newHex.data.res = AssetDatabase.GetAssetPath(brush); newHex.HexPosition = new Vector2(newHex.data.x, newHex.data.y); garbage.Add(go); var index = map.HexPositionToIndex(hex.data.x, hex.data.y); map.hexs[index] = newHex; map.cells[index] = newHex.data; GameObject.DestroyImmediate(hex.gameObject); GameObject.DestroyImmediate(hb); return(newHex); }
public void ChangeAllHexToBrushType(HexBrush brush) { var array = map.hexs; for (int i = 0; i < array.Length; i++) { var hex = array[i]; ChangeGameObjectToBrushType(hex, brush); } }
void OnGUIBrushInfo(Rect size) { GUILayout.BeginArea(size); BrushInfo = EditorGUILayout.BeginFoldoutHeaderGroup(BrushInfo, "BrushInfo"); if (BrushInfo) { if (currentSelect) { HexBrush hb = currentSelect.GetComponent <HexBrush>(); if (hb) { MapCellTool.DrawBrush(hb.data); } } } EditorGUILayout.EndFoldoutHeaderGroup(); GUILayout.EndArea(); }
void AddBuildToHex(Hex hex, HexBrush brush) { var res = AssetDatabase.GetAssetPath(brush.gameObject); if (res != hex.data.buildingRes) { hex.data.buildingRes = res; hex.data.buildingType = brush.data.buildingType; hex.data.eventType = brush.data.eventType; hex.data.walkType = brush.data.walkType; if (hex.transform.childCount == 1) { var trans = hex.transform.GetChild(0); GameObject.DestroyImmediate(trans.gameObject); } GenBuildingRes(hex); } }
void AddBuildToHexWithUndo(Hex hex, HexBrush brush) { var res = AssetDatabase.GetAssetPath(brush.gameObject); if (res != hex.data.buildingRes) { Undo.RecordObject(hex, hex.name); hex.data.buildingRes = res; hex.data.buildingType = brush.data.buildingType; hex.data.eventType = brush.data.eventType; hex.data.walkType = brush.data.walkType; if (hex.transform.childCount == 1) { var trans = hex.transform.GetChild(0); Undo.DestroyObjectImmediate(trans.gameObject); } GenBuildingResWithUndo(hex); } }
void ChangeGameObjectType(GameObject [] gos) { if (currentSelect != null) { foreach (var k in gos) { Hex cell = k.GetComponent <Hex>(); HexBrush hb = currentSelect.GetComponent <HexBrush>(); if (cell != null && cell.data.res != AssetDatabase.GetAssetPath(currentSelect)) //选中是地表 { if (hb.data.buildingType == MapCellData.BuildingType.Floor) //画刷是地面 { ChangeGameObjectToBrushTypeWithUndo(cell, hb); } else { AddBuildToHexWithUndo(cell, hb);//画刷是建筑 } } if (k) { HexBuilding building = k.GetComponent <HexBuilding>(); if (building && building.hex) //当前选中的是建筑 { if (hb.data.buildingType == MapCellData.BuildingType.Building) //画刷是建筑 { AddBuildToHexWithUndo(building.hex, hb); } else if (hb.data.buildingType == MapCellData.BuildingType.Floor)//画刷是Floor,那就替换地面了 { ChangeGameObjectToBrushTypeWithUndo(building.hex, hb); } } } } } }