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]); }
// Fixes nulls in serialization manually...*sigh*. public override void OnDeserialization() { if (typeField == null) { typeField = new DynamicTypeField(); typeField.showMoreOptions = true; } typeField.searchItem = this; typeHash = new Dictionary <string, Type>(); typeHash["Text"] = typeof(string); typeHash["Object"] = typeof(UnityEngine.Object); typeHash["Float, Double"] = typeof(float); typeHash["Integer, Long"] = typeof(long); typeHash["Boolean"] = typeof(bool); typeHash["Color"] = typeof(Color); typeHash["Vector2"] = typeof(Vector2); typeHash["Vector3"] = typeof(Vector3); typeHash["Vector4"] = typeof(Vector4); typeHash["Quaternion"] = typeof(Quaternion); typeHash["Rect"] = typeof(Rect); string classSearchLabel = "Scripts"; typeHash[classSearchLabel] = typeof(Type); typeOptions = typeHash.Keys.ToArray(); subTypeHash = new Dictionary <string, Type>(typeHash); subTypeHash.Remove(classSearchLabel); subTypeOptions = subTypeHash.Keys.ToArray(); typeField.OnDeserialization(); // sigh...this is getting old. typeField.showMoreOptions = showMoreOptions; typeField.depth = depth; initializationContext = new InitializationContext(typeHash[typeOptions[typeIndex]]); // I feel like this could be improved. If perhaps we implement more places where // replace functionality becomes more complicated this should be revisited. DynamicTypeObject objData = (DynamicTypeObject)GetDTDFor(typeof(UnityEngine.Object)); objData.InitReplaceOptionsForGlobal(); typeField.SetType(initializationContext); OnDeserializeSubSearch(); }
// Initializes our DTDs and creates our typeHash. All these null checks // deal with the limitations of C#'s serialization subsystem. public void OnDeserialization() { typeHash = new Dictionary <Type, DynamicTypeData>(); if (dynamicString == null) { dynamicString = new DynamicTypeString(); } dynamicString.parent = this; dynamicString.OnDeserialization(); typeHash[typeof(string)] = dynamicString; if (dynamicObject == null) { dynamicObject = new DynamicTypeObject(); } dynamicObject.parent = this; dynamicObject.OnDeserialization(); typeHash[typeof(UnityEngine.Object)] = dynamicObject; if (dynamicFloat == null) { dynamicFloat = new DynamicTypeFloat(); } dynamicFloat.parent = this; dynamicFloat.OnDeserialization(); typeHash[typeof(float)] = dynamicFloat; if (dynamicInt == null) { dynamicInt = new DynamicTypeInt(); } dynamicInt.parent = this; dynamicInt.OnDeserialization(); typeHash[typeof(int)] = dynamicInt; typeHash[typeof(uint)] = dynamicInt; typeHash[typeof(short)] = dynamicInt; typeHash[typeof(ushort)] = dynamicInt; typeHash[typeof(byte)] = dynamicInt; typeHash[typeof(sbyte)] = dynamicInt; if (dynamicLong == null) { dynamicLong = new DynamicTypeLong(); } dynamicLong.parent = this; dynamicLong.OnDeserialization(); typeHash[typeof(long)] = dynamicLong; typeHash[typeof(ulong)] = dynamicLong; if (dynamicDouble == null) { dynamicDouble = new DynamicTypeDouble(); } dynamicDouble.parent = this; dynamicDouble.OnDeserialization(); typeHash[typeof(double)] = dynamicDouble; if (dynamicBool == null) { dynamicBool = new DynamicTypeBool(); } dynamicBool.parent = this; dynamicBool.OnDeserialization(); typeHash[typeof(bool)] = dynamicBool; if (dynamicChar == null) { dynamicChar = new DynamicTypeChar(); } dynamicChar.parent = this; dynamicChar.OnDeserialization(); typeHash[typeof(char)] = dynamicChar; if (dynamicVector2 == null) { dynamicVector2 = new DynamicTypeVector2(); } dynamicVector2.parent = this; dynamicVector2.OnDeserialization(); typeHash[typeof(Vector2)] = dynamicVector2; if (dynamicVector3 == null) { dynamicVector3 = new DynamicTypeVector3(); } dynamicVector3.parent = this; dynamicVector3.OnDeserialization(); typeHash[typeof(Vector3)] = dynamicVector3; if (dynamicVector4 == null) { dynamicVector4 = new DynamicTypeVector4(); } dynamicVector4.parent = this; dynamicVector4.OnDeserialization(); typeHash[typeof(Vector4)] = dynamicVector4; if (dynamicRect == null) { dynamicRect = new DynamicTypeRect(); } dynamicRect.parent = this; dynamicRect.OnDeserialization(); typeHash[typeof(Rect)] = dynamicRect; if (dynamicColor == null) { dynamicColor = new DynamicTypeColor(); } dynamicColor.parent = this; dynamicColor.OnDeserialization(); typeHash[typeof(Color)] = dynamicColor; typeHash[typeof(Color32)] = dynamicColor; if (dynamicQuat == null) { dynamicQuat = new DynamicTypeQuaternion(); } dynamicQuat.parent = this; dynamicQuat.OnDeserialization(); typeHash[typeof(Quaternion)] = dynamicQuat; if (dynamicEnum == null) { dynamicEnum = new DynamicTypeEnum(); } dynamicEnum.parent = this; dynamicEnum.OnDeserialization(); typeHash[typeof(Enum)] = dynamicEnum; if (dynamicCollection == null) { dynamicCollection = new DynamicTypeCollection(); } dynamicCollection.parent = this; dynamicCollection.OnDeserialization(); typeHash[typeof(Array)] = dynamicCollection; typeHash[typeof(List <>)] = dynamicCollection; typeHash[typeof(Dictionary <int, string>)] = dynamicCollection; if (dynamicCustomData == null) { dynamicCustomData = new DynamicCustomData(); } dynamicCustomData.parent = this; dynamicCustomData.OnDeserialization(); if (dynamicTypePropSheet == null) { dynamicTypePropSheet = new DynamicTypePropSheet(); } dynamicTypePropSheet.parent = this; typeHash[typeof(PropSheet <>)] = dynamicTypePropSheet; dynamicTypePropSheet.OnDeserialization(); if (dynamicTypeClass == null) { dynamicTypeClass = new DynamicTypeClass(); } dynamicTypeClass.parent = this; typeHash[typeof(Type)] = dynamicTypeClass; dynamicTypeClass.OnDeserialization(); }