void SwapCollectible(int ID) { CollectibleTB collectible = collectibleList[currentSwapID]; collectibleList[currentSwapID] = collectibleList[ID]; collectibleList[ID] = collectible; currentSwapID = -1; }
public static void InsertCollectible(CollectibleTB collectible, Tile tile) { GameObject colObj=(GameObject)Instantiate(collectible.gameObject); colObj.transform.parent=tile.transform; colObj.transform.localPosition=Vector3.zero; colObj.transform.rotation=Quaternion.Euler(0, UnityEngine.Random.Range(0, 6)*60, 0); CollectibleTB col=colObj.GetComponent<CollectibleTB>(); tile.collectible=col; col.occupiedTile=tile; }
void OnGUI() { if (window == null) { Init(); } Rect visibleRect = new Rect(0, 0, window.position.width, window.position.height); Rect contentRect = new Rect(0, 0, window.position.width - 15, contentHeight); scrollPos = GUI.BeginScrollView(visibleRect, scrollPos, contentRect); GUI.changed = false; if (GUI.Button(new Rect(window.position.width - 130, 5, 120, 25), "CollectibleManager")) { this.Close(); CollectibleManagerWindow.Init(); } startX = 3; startY = 3; height = 15; spaceY = 17; if (collectibleList.Count > 0) { GUI.SetNextControlName("CollectibleSelect"); index = EditorGUI.Popup(new Rect(startX, startY, 300, height), "Collectible:", index, nameList); if (GUI.changed) { GUI.FocusControl("CollectibleSelect"); } CollectibleTB collectible = collectibleList[index]; Effect effect = collectible.effect; EditorGUI.LabelField(new Rect(340, startY, 70, height), "Icon: "); collectible.icon = (Texture)EditorGUI.ObjectField(new Rect(380, startY, 70, 70), collectible.icon, typeof(Texture), false); effect.icon = collectible.icon; //~ cont=new GUIContent("PointCost:", "The cost of unit"); //~ unit.pointCost = EditorGUI.IntField(new Rect(startX, startY+=spaceY, 300, height), cont, unit.pointCost); cont = new GUIContent("Name:", "The name for the unit ability"); //~ EditorGUI.LabelField(new Rect(startX, startY+=20, 200, 20), "Name: "); collectible.collectibleName = EditorGUI.TextField(new Rect(startX, startY += spaceY, 300, height), cont, collectible.collectibleName); effect.name = collectible.collectibleName; startY += 7; //~ cont=new GUIContent("Is Buff:", "Check if the collectible gives buff effect, this is merely for GUI purpose"); //~ EditorGUI.LabelField(new Rect(startX, startY+=18, 200, 20), cont); //~ effect.isBuff=EditorGUI.Toggle(new Rect(startX, startY+=spaceY, 300, height), cont, effect.isBuff); cont = new GUIContent("Enable AOE:", "Check if the collectible has an area of effect (affects more than single tile)"); //~ EditorGUI.LabelField(new Rect(startX, startY+=18, 200, 20), cont); collectible.enableAOE = EditorGUI.Toggle(new Rect(startX, startY += spaceY, 300, height), cont, collectible.enableAOE); if (collectible.enableAOE) { cont = new GUIContent("AOE Range:", "The range of aoe in term of tile"); //~ EditorGUI.LabelField(new Rect(startX, startY+=18, 200, 20), cont); collectible.aoeRange = EditorGUI.IntField(new Rect(startX, startY += spaceY, 300, height), cont, collectible.aoeRange); } else { startY += spaceY; } startY += 3; if (GUI.Button(new Rect(startX, startY += spaceY, 100, height), "add effect")) { if (effect.effectAttrs.Count < 4) { effect.effectAttrs.Add(new EffectAttr()); } else { Debug.Log("Cannot have more than 4 effects"); } } for (int i = 0; i < effect.effectAttrs.Count; i++) { EffectAttrConfigurator(effect, i, startX, startY); startX += 135; } startX = 3; //~ startY+=150; //~ EditorGUI.LabelField(new Rect(startX, startY+=5, 300, 25), "Collectible Description (for runtime UI): "); //~ collectible.effect.desp=EditorGUI.TextArea(new Rect(startX, startY+=17, window.position.width-20, 50), collectible.effect.desp); //~ startY+=60; contentHeight = startY; if (GUI.changed) { if (GUI.changed) { EditorUtility.SetDirty(collectible); } if (onCollectibleUpdateE != null) { onCollectibleUpdateE(); } } } else { EditorGUI.LabelField(new Rect(startX, startY, 450, height), "No Collectible has been assigned, please do so in the CollectibleManager"); } GUI.EndScrollView(); }
void OnGUI() { if(window==null) Init(); #if !UNITY_4_3 Undo.SetSnapshotTarget(this, "UnitManagerWindow"); #else Undo.RecordObject(this, "UnitManagerWindow"); #endif int currentItemCount=collectibleList.Count; if(GUI.Button(new Rect(window.position.width-120, 5, 110, 25), "CollectibleEditor")){ this.Close(); CollectibleEditorWindow.Init(); } EditorGUI.LabelField(new Rect(5, 10, 150, 17), "Add new unit:"); newCollectible=(CollectibleTB)EditorGUI.ObjectField(new Rect(100, 10, 150, 17), newCollectible, typeof(CollectibleTB), false); if(newCollectible!=null){ if(!collectibleList.Contains(newCollectible)){ int rand=0; while(collectibleIDList.Contains(rand)) rand+=1; collectibleIDList.Add(rand); newCollectible.prefabID=rand; collectibleList.Add(newCollectible); GUI.changed=true; } newCollectible=null; } if(collectibleList.Count>0){ GUI.Box(new Rect(5, 40, 50, 20), "ID"); GUI.Box(new Rect(5+50-1, 40, 60+1, 20), "Icon"); GUI.Box(new Rect(5+110-1, 40, 160+2, 20), "Name"); GUI.Box(new Rect(5+270, 40, window.position.width-300, 20), ""); } scrollPos = GUI.BeginScrollView(new Rect(5, 60, window.position.width-12, window.position.height-50), scrollPos, new Rect(5, 55, window.position.width-30, 15+((collectibleList.Count))*50)); int row=0; for(int i=0; i<collectibleList.Count; i++){ if(i%2==0) GUI.color=new Color(.8f, .8f, .8f, 1); else GUI.color=Color.white; GUI.Box(new Rect(5, 60+i*49, window.position.width-30, 50), ""); GUI.color=Color.white; if(currentSwapID==i) GUI.color=new Color(.9f, .9f, .0f, 1); if(GUI.Button(new Rect(19, 12+60+i*49, 30, 30), collectibleList[i].prefabID.ToString())){ if(currentSwapID==i) currentSwapID=-1; else if(currentSwapID==-1) currentSwapID=i; else{ SwapCollectible(i); GUI.changed=true; } } if(currentSwapID==i) GUI.color=Color.white; if(collectibleList[i]!=null){ collectibleList[i].icon=(Texture)EditorGUI.ObjectField(new Rect(12+50, 3+60+i*49, 44, 44), collectibleList[i].icon, typeof(Texture), false); collectibleList[i].collectibleName=EditorGUI.TextField(new Rect(5+120, 6+60+i*49, 150, 17), collectibleList[i].collectibleName); EditorGUI.LabelField(new Rect(5+120, 6+60+i*49+20, 150, 17), "Prefab:"); EditorGUI.ObjectField(new Rect(5+165, 6+60+i*49+20, 105, 17), collectibleList[i].gameObject, typeof(GameObject), false); } if(delete!=i){ if(GUI.Button(new Rect(window.position.width-55, 12+60+i*49, 25, 25), "X")){ delete=i; } } else{ GUI.color = Color.red; if(GUI.Button(new Rect(window.position.width-90, 12+60+i*49, 60, 25), "Remove")){ if(currentSwapID==i) currentSwapID=-1; collectibleIDList.Remove(collectibleList[i].prefabID); collectibleList.RemoveAt(i); delete=-1; //~ if(onCreepUpdateE!=null) onCreepUpdateE(); GUI.changed=true; } GUI.color = Color.white; } row+=1; } GUI.EndScrollView(); if(GUI.changed || currentItemCount!=collectibleList.Count){ EditorUtility.SetDirty(prefab); for(int i=0; i<collectibleList.Count; i++) EditorUtility.SetDirty(collectibleList[i]); } #if UNITY_LE_4_3 if (GUI.changed || currentItemCount!=collectibleList.Count){ Undo.CreateSnapshot(); Undo.RegisterSnapshot(); } Undo.ClearSnapshotTarget(); #endif }
void OnGUI() { if (window == null) { Init(); } #if !UNITY_4_3 Undo.SetSnapshotTarget(this, "UnitManagerWindow"); #else Undo.RecordObject(this, "UnitManagerWindow"); #endif int currentItemCount = collectibleList.Count; if (GUI.Button(new Rect(window.position.width - 120, 5, 110, 25), "CollectibleEditor")) { this.Close(); CollectibleEditorWindow.Init(); } EditorGUI.LabelField(new Rect(5, 10, 150, 17), "Add new unit:"); newCollectible = (CollectibleTB)EditorGUI.ObjectField(new Rect(100, 10, 150, 17), newCollectible, typeof(CollectibleTB), false); if (newCollectible != null) { if (!collectibleList.Contains(newCollectible)) { int rand = 0; while (collectibleIDList.Contains(rand)) { rand += 1; } collectibleIDList.Add(rand); newCollectible.prefabID = rand; collectibleList.Add(newCollectible); GUI.changed = true; } newCollectible = null; } if (collectibleList.Count > 0) { GUI.Box(new Rect(5, 40, 50, 20), "ID"); GUI.Box(new Rect(5 + 50 - 1, 40, 60 + 1, 20), "Icon"); GUI.Box(new Rect(5 + 110 - 1, 40, 160 + 2, 20), "Name"); GUI.Box(new Rect(5 + 270, 40, window.position.width - 300, 20), ""); } scrollPos = GUI.BeginScrollView(new Rect(5, 60, window.position.width - 12, window.position.height - 50), scrollPos, new Rect(5, 55, window.position.width - 30, 15 + ((collectibleList.Count)) * 50)); int row = 0; for (int i = 0; i < collectibleList.Count; i++) { if (i % 2 == 0) { GUI.color = new Color(.8f, .8f, .8f, 1); } else { GUI.color = Color.white; } GUI.Box(new Rect(5, 60 + i * 49, window.position.width - 30, 50), ""); GUI.color = Color.white; if (currentSwapID == i) { GUI.color = new Color(.9f, .9f, .0f, 1); } if (GUI.Button(new Rect(19, 12 + 60 + i * 49, 30, 30), collectibleList[i].prefabID.ToString())) { if (currentSwapID == i) { currentSwapID = -1; } else if (currentSwapID == -1) { currentSwapID = i; } else { SwapCollectible(i); GUI.changed = true; } } if (currentSwapID == i) { GUI.color = Color.white; } if (collectibleList[i] != null) { collectibleList[i].icon = (Texture)EditorGUI.ObjectField(new Rect(12 + 50, 3 + 60 + i * 49, 44, 44), collectibleList[i].icon, typeof(Texture), false); collectibleList[i].collectibleName = EditorGUI.TextField(new Rect(5 + 120, 6 + 60 + i * 49, 150, 17), collectibleList[i].collectibleName); EditorGUI.LabelField(new Rect(5 + 120, 6 + 60 + i * 49 + 20, 150, 17), "Prefab:"); EditorGUI.ObjectField(new Rect(5 + 165, 6 + 60 + i * 49 + 20, 105, 17), collectibleList[i].gameObject, typeof(GameObject), false); } if (delete != i) { if (GUI.Button(new Rect(window.position.width - 55, 12 + 60 + i * 49, 25, 25), "X")) { delete = i; } } else { GUI.color = Color.red; if (GUI.Button(new Rect(window.position.width - 90, 12 + 60 + i * 49, 60, 25), "Remove")) { if (currentSwapID == i) { currentSwapID = -1; } collectibleIDList.Remove(collectibleList[i].prefabID); collectibleList.RemoveAt(i); delete = -1; //~ if(onCreepUpdateE!=null) onCreepUpdateE(); GUI.changed = true; } GUI.color = Color.white; } row += 1; } GUI.EndScrollView(); if (GUI.changed || currentItemCount != collectibleList.Count) { EditorUtility.SetDirty(prefab); for (int i = 0; i < collectibleList.Count; i++) { EditorUtility.SetDirty(collectibleList[i]); } } #if UNITY_LE_4_3 if (GUI.changed || currentItemCount != collectibleList.Count) { Undo.CreateSnapshot(); Undo.RegisterSnapshot(); } Undo.ClearSnapshotTarget(); #endif }
void GenerateGrid() { /* * DateTime timeS; * DateTime timeE; * TimeSpan timeSpan; * timeS=System.DateTime.Now; */ /* * timeE=System.DateTime.Now; * timeSpan=timeE-timeS; * Debug.Log( "Time:"+timeSpan.TotalMilliseconds); * * return; */ //clear previous tile Tile[] allTilesInScene = (Tile[])FindObjectsOfType(typeof(Tile)); foreach (Tile tile in allTilesInScene) { if (tile.unit != null) { DestroyImmediate(tile.unit.gameObject); } if (tile.obstacle != null) { DestroyImmediate(tile.obstacle.gameObject); } if (tile.collectible != null) { DestroyImmediate(tile.collectible.gameObject); } DestroyImmediate(tile.gameObject); } //Tile[] allTilesInScenes=(Tile[])FindObjectsOfType(typeof(Tile)); /* * for(int i=0; i<tileList.Count; i++){ * if(tileList[i]!=null){ * if(tileList[i].unit!=null) DestroyImmediate(tileList[i].unit.gameObject); * DestroyImmediate(tileList[i].gameObject); * } * } * tileList=new List<Tile>(); */ List <Tile> tileList = new List <Tile>(); if (gm.type == _TileType.Square) { tileList = GenerateSquareGrid(); } else if (gm.type == _TileType.Hex) { tileList = GenerateHexGrid(); } /* * mould the grid to the height of the terrain * foreach(Tile hTile in tileList){ * Vector3 pos=new Vector3(hTile.pos.x, hTile.pos.y+gm.maxHeight, hTile.pos.z); * RaycastHit hit; * * bool flag=Physics.Raycast(pos, Vector3.down, out hit, gm.maxHeight+10); * * if(flag) pos.y=hit.point.y+0.1f; * else pos.y=gm.baseHeight; * * hTile.pos=pos; * hTile.transform.position=pos; * } */ //set unwalkable tile int blockTile = (int)(tileList.Count * gm.unwalkableRate); if (blockTile > 0) { int count = 0; while (count < blockTile) { int rand = UnityEngine.Random.Range(0, tileList.Count); while (!tileList[rand].walkable || tileList[rand].unit != null) { rand = UnityEngine.Random.Range(0, tileList.Count); } if (!gm.useObstacle) { tileList[rand].walkable = false; tileList[rand].Start(); } else { int offset = 0; if (gm.enableInvUnwalkable) { offset += 1; } if (gm.enableVUnwalkable) { offset += 1; } int randT = UnityEngine.Random.Range(0, gm.obstaclePrefabList.Count + offset); if (!gm.enableInvUnwalkable && !gm.enableVUnwalkable) { } if (randT < offset) { if (gm.enableInvUnwalkable && randT == 0) { tileList[rand].SetToUnwalkable(false); } else { tileList[rand].SetToUnwalkable(true); } } else { GameObject obsObj = (GameObject)PrefabUtility.InstantiatePrefab(gm.obstaclePrefabList[randT - offset].gameObject); obsObj.transform.localScale *= gm.gridSize; obsObj.transform.parent = tileList[rand].transform; obsObj.transform.localPosition = Vector3.zero; obsObj.GetComponent <Collider>().enabled = false; Obstacle obs = obsObj.GetComponent <Obstacle>(); tileList[rand].obstacle = obs; obs.occupiedTile = tileList[rand]; tileList[rand].SetToUnwalkable(false); } } count += 1; } } for (int i = 0; i < tileList.Count; i++) { tileList[i].gameObject.layer = 8; } //set neighbour for (int i = 0; i < tileList.Count; i++) { Tile hT = tileList[i]; Vector3 pos = hT.transform.position; Collider[] cols = Physics.OverlapSphere(pos, gm.gridSize * gm.gridToTileSizeRatio * 0.6f); List <Tile> neighbour = new List <Tile>(); foreach (Collider col in cols) { Tile hTile = col.gameObject.GetComponent <Tile>(); if (hTile != null && hT != hTile) { neighbour.Add(hTile); } } hT.SetNeighbours(neighbour); } if (gm.addColletible && gm.collectiblePrefabList.Count > 0) { int ccount = UnityEngine.Random.Range(gm.minCollectibleCount, gm.maxCollectibleCount); if (ccount > 0) { List <Tile> emptyTiles = new List <Tile>(); for (int i = 0; i < tileList.Count; i++) { if (tileList[i].walkable) { emptyTiles.Add(tileList[i]); } } for (int i = 0; i < ccount; i++) { int rand = UnityEngine.Random.Range(0, emptyTiles.Count); int randID = UnityEngine.Random.Range(0, gm.collectiblePrefabList.Count); GameObject colObj = (GameObject)PrefabUtility.InstantiatePrefab(gm.collectiblePrefabList[randID].gameObject); colObj.transform.parent = emptyTiles[rand].transform; colObj.transform.localPosition = Vector3.zero; colObj.transform.rotation = Quaternion.Euler(0, UnityEngine.Random.Range(0, 6) * 60, 0); CollectibleTB col = colObj.GetComponent <CollectibleTB>(); emptyTiles[rand].collectible = col; col.occupiedTile = emptyTiles[rand]; emptyTiles.RemoveAt(rand); if (emptyTiles.Count == 0) { break; } } } } GenerateUnit(); gm.GenerateGrid(false); /* * timeE=System.DateTime.Now; * timeSpan=timeE-timeS; * //Debug.Log(counter+" tiles has been generated Time:"+timeSpan.TotalMilliseconds); */ }