public static void SwapPrefab(SearchJob job, SearchResult result, GameObject gameObjToSwap, GameObject prefab, bool updateTransform, bool rename) { Transform swapParent = gameObjToSwap.transform.parent; int index = gameObjToSwap.transform.GetSiblingIndex(); result.replaceStrRep = prefab.name; result.strRep = gameObjToSwap.name; // Debug.Log("[ReplaceItemSwapObject] Instantiating:" +prefab, prefab); GameObject newObj = PrefabUtility.InstantiatePrefab(prefab) as GameObject; if (newObj != null) { newObj.transform.parent = swapParent; newObj.transform.SetSiblingIndex(index); Transform oldT = gameObjToSwap.transform; if (updateTransform) { newObj.transform.rotation = oldT.rotation; newObj.transform.localPosition = oldT.localPosition; newObj.transform.localScale = oldT.localScale; } if (rename) { newObj.name = gameObjToSwap.name; } result.pathInfo = PathInfo.GetPathInfo(newObj, job); UnityEngine.Object.DestroyImmediate(gameObjToSwap); } else { Debug.Log("[Search & Replace] No object instantiated...hrm!"); } }
public override void SearchGameObject(SearchJob job, GameObject go) { job.assetData.searchExecuted = true; //We only do this search if its a game object AND its a search (NOT a replace). if (searchAsGameObject != null && job.options.searchType == SearchType.Search && subsearch == null) { // if(searchAsGameObject == go) // { // Debug.Log("[SearchItemGlobal] FOUND THING"); // } #if UNITY_2018_2 || UNITY_2018_3 || UNITY_2018_4 UnityEngine.Object goPrefab = PrefabUtility.GetCorrespondingObjectFromSource(go); #else UnityEngine.Object goPrefab = PrefabUtility.GetPrefabParent(go); #endif // Debug.Log("[SearchItemGlobal] goPrefab:"+goPrefab); if (goPrefab != null && goPrefab == searchAsGameObject) { SearchResult result = new SearchResult(); result.strRep = ""; result.pathInfo = PathInfo.GetPathInfo(go, job); result.actionTaken = SearchAction.InstanceFound; job.MatchFound(result, this); } } typeField.SearchGameObject(job, this, go); }
public override void SearchGameObject(SearchJob job, GameObject go) { job.assetData.searchExecuted = true; // Debug.Log("[SearchItemInstances] GO:"+go.name); PrefabType goType = PrefabUtility.GetPrefabType(go); if (goType == PrefabType.PrefabInstance) { #if UNITY_2018_2 || UNITY_2018_3 || UNITY_2018_4 UnityEngine.Object goPrefabObj = PrefabUtility.GetCorrespondingObjectFromSource(go); #else UnityEngine.Object goPrefabObj = PrefabUtility.GetPrefabParent(go); #endif GameObject root = PrefabUtility.FindPrefabRoot(go); bool isRootOfPrefab = root == go; if (isRootOfPrefab && goPrefabObj == objID.obj) { //Instance found! // Debug.Log("[SearchItemInstances] instance found!"+go.name); SearchResult result = new SearchResult(); result.strRep = ""; result.pathInfo = PathInfo.GetPathInfo(go, job); result.actionTaken = SearchAction.InstanceFound; job.MatchFound(result, this); replaceItem.ReplaceInstance(job, go, result); } } }
public override void OnAssetSearchEnd(SearchJob job, SearchItem item) { if (job.searchIncludesScripts && matchAssetMissingScript && assetMissingScript) { SearchResult result = new SearchResult(); result.strRep = job.assetData.assetPath; result.pathInfo = PathInfo.GetPathInfo(AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(job.assetData.assetPath), job.assetData); result.actionTaken = SearchAction.AssetMissingScript; job.MatchFound(result, item); } }
static void replaceInstances(SearchJob parentJob, SearchItem item, GameObject oldValue, GameObject newValue) { SearchItemSet searchSet = new SearchItemSet(); searchSet.OnDeserialization(); searchSet.AddNew(Keys.Global, false); SearchItemGlobal searchItem = (SearchItemGlobal)searchSet.items[0]; searchItem.SetType("Object"); DynamicTypeObject dto = (DynamicTypeObject)searchItem.GetDTDFor(typeof(UnityEngine.Object)); dto.SearchSubObjects = true; dto.SetObject(oldValue); ReplaceItemObject replaceItem = (ReplaceItemObject)dto.ReplaceItem; replaceItem.SetObject(newValue); SearchOptions options = new SearchOptions(); options.searchType = SearchType.SearchAndReplace; // does this matter anymore since asset scope is now essentially defined by what assets are passed in? SearchScopeData searchScope = new SearchScopeData(ProjectScope.SpecificLocation, AssetScope.Prefabs, new ObjectID(AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(parentJob.assetData.assetPath)), false, parentJob.scope.loadPrefab); SearchJob subJob = new SearchJob(searchSet, options, searchScope); SearchAssetData assetData = parentJob.assetData.Clone(); subJob.AddAsset(assetData); subJob.Execute(); // Now that we've executed the job we have to save a list of all objects from search results, because as soon as // we swap out the new object, the old object's position may shift in the hierarchy, making the PathInfo stale. SearchResultGroup group = subJob.resultSet.results[searchItem.sortIndex]; List <UnityEngine.Object> resultObjects = new List <Object>(); foreach (SearchResult result in group.results) { UnityEngine.Object resultObj = EditorUtility.InstanceIDToObject(result.pathInfo.objectID); resultObjects.Add(resultObj); } UnityEngine.Object.DestroyImmediate(oldValue); // now that we've deleted the object, let's rebuild the objects. for (int i = 0; i < resultObjects.Count; i++) { SearchResult result = group.results[i]; result.pathInfo = PathInfo.GetPathInfo(resultObjects[i], assetData); } parentJob.AddResultsFromSubsearch(item, subJob.resultSet.results[searchItem.sortIndex]); }
public static bool SwapPrefab(SearchJob job, SearchItem item, SearchResult result, GameObject gameObjToSwap, GameObject prefab, bool updateTransform, bool rename) { string reason; if (!PrefabUtil.IsPrefabObjectReplaceable(gameObjToSwap, job.assetData, out reason)) { result.actionTaken = SearchAction.InstanceNotReplaced; result.error = reason; return(false); } Transform swapParent = gameObjToSwap.transform.parent; int index = gameObjToSwap.transform.GetSiblingIndex(); result.replaceStrRep = prefab.name; result.strRep = gameObjToSwap.name; // Debug.Log("[ReplaceItemSwapObject] Instantiating:" +prefab, prefab); GameObject newObj = PrefabUtility.InstantiatePrefab(prefab) as GameObject; if (newObj != null) { newObj.transform.parent = swapParent; newObj.transform.SetSiblingIndex(index); Transform oldT = gameObjToSwap.transform; if (updateTransform) { newObj.transform.rotation = oldT.rotation; newObj.transform.localPosition = oldT.localPosition; newObj.transform.localScale = oldT.localScale; } if (rename) { newObj.name = gameObjToSwap.name; } result.pathInfo = PathInfo.GetPathInfo(newObj, job.assetData); job.assetData.assetIsDirty = true; replaceInstances(job, item, gameObjToSwap, newObj); return(true); } else { Debug.Log("[Search & Replace] No object instantiated...hrm!"); return(false); } }
public override void OnSearchEnd(SearchJob job, SearchItem item) { if (showUnusedScripts) { foreach (var kvp in typeHash) { ClassSearchInfo csi = kvp.Value; if (csi.numFound == 0) { SearchResult result = new SearchResult(); result.pathInfo = PathInfo.GetPathInfo(csi.script, job); result.actionTaken = SearchAction.NotFound; result.strRep = kvp.Key.ToString(); job.MatchFound(result, item); } } } }
protected override void replace(SearchJob job, SerializedProperty prop, SearchResult result) { if (subItem != null) { UnityEngine.Object originalObj = prop.serializedObject.targetObject; // figure out how this object relates to our replace object. FieldData fd = initializationContext.fieldData; SerializedProperty replaceProp = null; if (fd.objectType == originalObj.GetType()) { // easy enough! they match. replaceProp = prop.serializedObject.FindProperty(fd.fieldName); } else { GameObject go = ObjectUtil.GetGameObject(originalObj); if (go != null) { Component c = go.GetComponent(fd.objectType); if (c != null) { SerializedObject so = new SerializedObject(c); replaceProp = so.FindProperty(fd.fieldName); } } } if (replaceProp != null) { result.pathInfo = PathInfo.GetPathInfo(replaceProp.serializedObject.targetObject, job); result.strRep = subItem.StringValueFor(replaceProp); subItem.ReplaceInternal(job, replaceProp, result); replaceProp.serializedObject.ApplyModifiedProperties(); // This is pretty slow. We may want to find a better solution. if (PrefabUtil.isInstanceModification(replaceProp)) { PrefabUtility.RecordPrefabInstancePropertyModifications(replaceProp.serializedObject.targetObject); } } } }
public override void SearchGameObject(SearchJob job, GameObject go) { job.assetData.searchExecuted = true; PrefabTypes goType = PrefabUtil.GetPrefabType(go); if (goType == PrefabTypes.PrefabInstance || goType == PrefabTypes.NestedPrefabInstance || goType == PrefabTypes.PrefabVariantInstance) { GameObject goPrefabObj = null; #if UNITY_2018_3_OR_NEWER if (goType == PrefabTypes.PrefabInstance || goType == PrefabTypes.NestedPrefabInstance) { // nested prefabs or normal instances means we should search for the *original* source. // prefab variant instances will show up here as nestedprefab instances // if the variants are in variants. :P goPrefabObj = PrefabUtility.GetCorrespondingObjectFromOriginalSource(go); } else { // PrefabVariantInstance means we need the corresponding source. goPrefabObj = PrefabUtility.GetCorrespondingObjectFromSource(go); } GameObject root = go; #else goPrefabObj = (GameObject)PrefabUtility.GetPrefabParent(go); GameObject root = PrefabUtility.FindPrefabRoot(go); #endif bool isRootOfPrefab = root == go; if (isRootOfPrefab && goPrefabObj == objID.obj) { //Instance found! // Debug.Log("[SearchItemInstances] instance found!"+go.name); SearchResult result = new SearchResult(); result.strRep = ""; result.pathInfo = PathInfo.GetPathInfo(go, job.assetData); result.actionTaken = SearchAction.InstanceFound; job.MatchFound(result, this); replaceItem.ReplaceInstance(job, this, go, result); } } }
public SearchResultProperty(SerializedProperty p, string sRep, SearchJob job) { strRep = sRep; pathInfo = PathInfo.GetPathInfo(p, job.assetData); }