private void PreBuild(GameObject prefab) { var go = Instantiate(prefab); var varwinObjectDescriptor = go.GetComponent <VarwinObjectDescriptor>(); varwinObjectDescriptor.PreBuild(); CreateObjectUtils.ApplyPrefabInstanceChanges(go); var vods = FindObjectsOfType <VarwinObjectDescriptor>(); foreach (var vod in vods) { if (vod.Guid == varwinObjectDescriptor.Guid) { CreateObjectUtils.RevertPrefabInstanceChanges(vod.gameObject); } } CreateObjectUtils.SafeDestroy(go); }
private static void OnScriptsReloaded() { try { GetWindow <ImportModelsWindow>().Close(); int createdNum = 0; if (File.Exists(CreateObjectTempModel.TempFilePath)) { string config = File.ReadAllText(CreateObjectTempModel.TempFilePath); CreateObjectTempModel temp = JsonConvert.DeserializeObject <CreateObjectTempModel>(config); File.Delete(CreateObjectTempModel.TempFilePath); if (temp == null) { Debug.LogError("Temp build file is broken! Can't finish objects creation."); return; } _modelsList = temp.Objects; _buildNow = temp.BuildNow; foreach (CreateObjectModel fileModel in _modelsList) { Debug.Log("Creating prefab for " + fileModel.ObjectName); GameObject gameObject, prefab; //Check if it's model import if (fileModel.ModelImportPath != null) { if (fileModel.Skip) { continue; } GameObject modelPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(fileModel.ModelImportPath); if (modelPrefab == null) { Debug.LogError("Can't create object " + fileModel.ObjectName + ". Imported file is incorrect: " + fileModel.Path); Directory.Delete(fileModel.ObjectFolder, true); continue; } gameObject = Instantiate(modelPrefab, new Vector3(0, 0, 0), Quaternion.identity); //Calculate bounds Bounds bounds = GetBounds(gameObject); float maxBound = Mathf.Max(bounds.size.x, bounds.size.y, bounds.size.z); float scale = fileModel.BiggestSideSize / maxBound; gameObject.transform.localScale = Vector3.one * scale; Rigidbody objectBody = gameObject.AddComponent <Rigidbody>(); objectBody.isKinematic = !fileModel.IsPhysicsOn; objectBody.mass = fileModel.Mass; InteractableObjectBehaviour objectBehaviour = gameObject.AddComponent <InteractableObjectBehaviour>(); objectBehaviour.SetIsGrabbable(fileModel.IsGrabbable); objectBehaviour.SetIsUsable(false); objectBehaviour.SetIsTouchable(false); CreateObjectUtils.AddObjectId(gameObject); MeshFilter[] meshes = gameObject.GetComponentsInChildren <MeshFilter>(); foreach (MeshFilter meshFilter in meshes) { MeshCollider collider = meshFilter.gameObject.AddComponent <MeshCollider>(); collider.sharedMesh = meshFilter.sharedMesh; collider.convex = true; } if (meshes == null || meshes.Length == 0) { BoxCollider box = gameObject.AddComponent <BoxCollider>(); box.center = bounds.center; box.size = bounds.size; } prefab = CreatePrefab(gameObject, fileModel.PrefabPath, fileModel.Guid, fileModel.ObjectName, fileModel.ClassName, fileModel.Extras); } else { gameObject = AssetDatabase.LoadAssetAtPath <GameObject>(fileModel.PrefabPath); if (gameObject == null) { Debug.LogError("Can't create " + fileModel.ObjectName + ": no prefab and no model. How did it happen? Please try again."); return; } CreateNew(gameObject, fileModel.PrefabPath, fileModel.Guid, fileModel.ObjectName, fileModel.ClassName, fileModel.Extras, true, true); } AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); if (fileModel.ModelImportPath != null) { DestroyImmediate(gameObject); } createdNum++; gameObject = AssetDatabase.LoadAssetAtPath <GameObject>(fileModel.PrefabPath); var instance = PrefabUtility.InstantiatePrefab(gameObject); instance.name = instance.name.Replace("(Clone)", ""); EditorGUIUtility.PingObject(instance); } EditorUtility.DisplayDialog("Done!", createdNum + " objects were created!", "OK"); if (temp.BuildNow) { ObjectsBuilderWindow.BuildObjects(_modelsList.ToArray()); } GetWindow <CreateObjectWindow>().Close(); } } catch (Exception e) { EditorUtility.DisplayDialog("Error!", $"{e.Message}:\nProblem when creating an objects", "OK"); Debug.LogException(e); } }
private static GameObject CreateNew(GameObject obj, string localPath, string guid, string objectName, string classFullName, CreateObjectModel.AssetExtras licenseData, bool withMainClass = true, bool overwrite = false) { if (withMainClass) { CreateObjectUtils.AddComponent(obj, localPath, classFullName, $"{objectName}"); } var config = obj.GetComponent <VarwinObjectDescriptor>(); if (config == null) { config = obj.AddComponent <VarwinObjectDescriptor>(); if (config == null) { obj = Instantiate(obj); config = obj.AddComponent <VarwinObjectDescriptor>(); } } if (string.IsNullOrWhiteSpace(config.Guid)) { config.Guid = guid; config.RootGuid = guid; config.Name = objectName; config.Prefab = localPath; config.AuthorName = _authorName; config.AuthorEmail = _authorEmail; config.AuthorUrl = _authorUrl; config.LicenseCode = _licenseCode; config.BuiltAt = DateTimeOffset.Now.ToString(); config.MobileReady = SdkSettings.MobileFeature.Enabled && _mobileReady; } if (licenseData != null) { Regex reg = new Regex(@"(.+)\s+\((.+)\)"); var authorData = reg.Match(licenseData.Author); try { config.AuthorName = authorData.Groups[1].Value; config.AuthorUrl = authorData.Groups[2].Value; } catch { Debug.LogWarning("cannot read author name and author url properties"); } reg = new Regex(@"([a-zA-Z-]+)-([0-9\.]+)\s+\((.+)\)"); var license = reg.Match(licenseData.License); try { config.LicenseCode = license.Groups[1].Value.ToLower(); } catch { Debug.LogWarning("cannot read license code property"); } } GameObject prefab; if (overwrite) { prefab = obj; } else { Object instanceRoot = PrefabUtility.InstantiatePrefab(obj); Object prefabInstance = PrefabUtility.GetCorrespondingObjectFromSource(obj); bool isPrefabInstance = (instanceRoot == null); if (isPrefabInstance) { instanceRoot = PrefabUtility.InstantiatePrefab(prefabInstance); } bool success = false; if (instanceRoot != null) { GameObject fixObject = null; if (!isPrefabInstance) { fixObject = (GameObject)PrefabUtility.InstantiatePrefab(obj);; } PrefabUtility.UnpackPrefabInstance((GameObject)instanceRoot, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); prefab = PrefabUtility.SaveAsPrefabAsset((GameObject)instanceRoot, localPath, out success); if (!isPrefabInstance) { var objectDescriptor = fixObject.GetComponent <VarwinObjectDescriptor>(); if (objectDescriptor != null) { DestroyImmediate(objectDescriptor); PrefabUtility.ApplyPrefabInstance(fixObject, InteractionMode.AutomatedAction); } DestroyImmediate(fixObject); } DestroyImmediate(instanceRoot); } else { prefab = PrefabUtility.SaveAsPrefabAsset(obj, localPath, out success); DestroyImmediate(obj); } EditorGUIUtility.PingObject(prefab); if (success) { if (isPrefabInstance) { DestroyImmediate(obj); } return(prefab); } PrefabUtility.RevertPrefabInstance(obj, InteractionMode.AutomatedAction); Debug.LogError("Can not create prefab!"); return(null); } return(prefab); }