Replace With Tag

From Unify Community Wiki
Jump to: navigation, search

This is a script that will allow you to swap out all items with a certain tag , after putting it in the editor folder you need to click Window, and then select tileswapper .

It's named TileSwap since I coded it to work with the various tile mappers available on the asset store( or any other placement script) . Lets say your working on a game and everything tagged Wall needs to be swapped out with a different wall object SAVE BEFORE USING THIS SCRIPT , IF YOU USE IT WRONG IT CAN DESTROY YOUR LEVELS. After selecting TileSwapper under the Window Tab select the game object prefab you want to use as a replacement , type in the tag you want to find and replace , and click swap tile( if you want to delete the old objects then click the toggle box . )

As a bonus I noticed a problem when posting this script, the new items didn't share the tags of the old items, that was a quick fix however and now when you swap out the items they will share the same tag . This can be very usefull for prototyping . Enjoy and feel free to improve it !

C# - TileSwap.cs

// to compile to a platform you need to uncomment the conditional statement
//#if Unity_Editor
using UnityEngine;
using System.Collections;
using UnityEditor ;
public class TileSwap : EditorWindow{
// private string scriptText = string.Empty;	
public string "TagHere" ; // this is needed to stop an exception  the value doesn't matter
public Object ReplacementItem ; 
	public GameObject[] replaceTiles;
public bool myBool ;	
//public GameObject[] replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
 public GameObject Test ; 
  public TileSwap _target;
    void OnEnable()    
       _target = (TileSwap)_target;
public static void ShowWindow()
	//	_target = (TileSwap)_target;
		//Show existing window instance. If one doesn't exist, make one.
	void OnGUI()
	//GameObject TileNew = GameObject.FindGameObjectWithTag("SwapMe");	
		//ReplacementItem = EditorGUILayout.ObjectField(ReplacementItem,GameObject);
	ReplacementItem =	EditorGUILayout.ObjectField("New Tile", ReplacementItem, typeof(UnityEngine.Object));
// outputs "Assets/TileSwap.cs(28,80): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected"
		this.SwapTag = EditorGUILayout.TextField ("Object Tag", SwapTag);
			myBool = EditorGUILayout.Toggle ("Delete Replaced Items", myBool);
		GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
	//	myBool = EditorGUILayout.Toggle ("Delete Replaced Items", myBool);
    	//SwapTag ="SwapMe";
		//  DrawDefaultInspector(); //if you are just adding a button
      if(GUILayout.Button ("Swap Tile")){
			GameObject TileNew = GameObject.FindGameObjectWithTag("SwapMe");
			replaceTiles = GameObject.FindGameObjectsWithTag(SwapTag);
			 foreach (GameObject replaceTile in replaceTiles) {
			GameObject NewItem ;
		NewItem =	Instantiate(ReplacementItem, replaceTile.transform.position, replaceTile.transform.rotation) as GameObject  ;
			NewItem.tag = SwapTag ;
				//replaceTile.tag = SwapTag
					//				Destroy(replaceTile);	
				//Instantiate(ReplacementItem,Test.transform.position, Test.transform.rotation) as GameObject;
         //do what you need to here when the button is pressed
Personal tools