public static IvyDataAsset CreateNewDataAsset(string mainFolder, string sceneName, string path) { #if UNITY_EDITOR if (!AssetDatabase.IsValidFolder(path)) { var folderGUID = AssetDatabase.CreateFolder(mainFolder, sceneName); path = AssetDatabase.GUIDToAssetPath(folderGUID); } #endif IvyDataAsset asset = ScriptableObject.CreateInstance <IvyDataAsset>(); #if UNITY_EDITOR AssetDatabase.CreateAsset(asset, path + "/HederaData.asset"); AssetDatabase.SaveAssets(); #endif return(asset); }
public static bool TryDestroyMesh(long meshID, IvyDataAsset asset, bool saveChanges = true) { if (asset.meshList.ContainsKey(meshID) && asset.meshList[meshID] != null) { #if UNITY_EDITOR Undo.DestroyObjectImmediate(asset.meshList[meshID]); #endif asset.meshList.Remove(meshID); if (!saveChanges) { #if UNITY_EDITOR AssetDatabase.SaveAssets(); #endif } return(true); } return(false); }
public static IvyDataAsset GetDataAsset(GameObject forObject) { var scene = forObject.scene; var mainFolder = Path.GetDirectoryName(scene.path); string path = mainFolder + "/" + scene.name + "/HederaData.asset"; #if UNITY_EDITOR var asset = AssetDatabase.LoadAssetAtPath <IvyDataAsset>(path); #else IvyDataAsset asset = dataAsset; #endif if (asset == null) { asset = CreateNewDataAsset(mainFolder, scene.name, Path.GetDirectoryName(path)); dataAsset = asset; } return(asset); }
static void CheckMeshDataAsset(ref long meshID, IvyDataAsset myAsset, IvyProfile.MeshCompression meshCompress) { if (meshID == 0) { meshID = IvyRoot.GetRandomLong(); } if (!myAsset.meshList.ContainsKey(meshID) || myAsset.meshList[meshID] == null) { var newMesh = new Mesh(); #if UNITY_EDITOR MeshUtility.SetMeshCompression(newMesh, (ModelImporterMeshCompression)meshCompress); AssetDatabase.AddObjectToAsset(newMesh, AssetDatabase.GetAssetPath(myAsset)); #endif if (myAsset.meshList.ContainsKey(meshID) && myAsset.meshList[meshID] == null) { myAsset.meshList[meshID] = newMesh; } else { myAsset.meshList.Add(meshID, newMesh); } } }
//called whenever the inspector gui gets rendered public override void OnInspectorGUI() { EditorGUILayout.Space(); if (ivyBehavior == null) { ivyBehavior = (IvyBehavior)target; } wasPartOfPrefab = IvyCore.IsPartOfPrefab(ivyBehavior.gameObject); bool isInARealScene = !string.IsNullOrEmpty(ivyBehavior.gameObject.scene.path) && ivyBehavior.gameObject.activeInHierarchy; if (isInARealScene) { lastDataAsset = IvyCore.GetDataAsset(ivyBehavior.gameObject); } EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUI.BeginChangeCheck(); ivyBehavior.profileAsset = EditorGUILayout.ObjectField(ivyBehavior.profileAsset, typeof(IvyProfileAsset), false) as IvyProfileAsset; if (EditorGUI.EndChangeCheck() || (ivyProfileEditor == null && ivyBehavior.profileAsset != null)) { ivyProfileEditor = Editor.CreateEditor(ivyBehavior.profileAsset); ((IvyProfileEditor)ivyProfileEditor).viewedFromMonobehavior = true; } // destroy old editor / cleanup if (ivyBehavior.profileAsset == null && ivyProfileEditor != null) { DestroyImmediate(ivyProfileEditor); } if (ivyBehavior.profileAsset == null || ivyProfileEditor == null) { EditorGUILayout.HelpBox("Please assign an Ivy Profile Asset.", MessageType.Warning); if (GUILayout.Button("Create new Ivy Profile Asset...")) { var newAsset = IvyCore.CreateNewAsset(""); if (newAsset != null) { ivyBehavior.profileAsset = newAsset; ivyBehavior.showProfileFoldout = true; Selection.activeGameObject = ivyBehavior.gameObject; } } EditorGUILayout.EndVertical(); return; } var ivyProfile = ivyBehavior.profileAsset.ivyProfile; if (!IvyCore.ivyBehaviors.Contains(ivyBehavior)) { IvyCore.ivyBehaviors.Add(ivyBehavior); } GUIContent content = null; EditorGUI.indentLevel++; ivyBehavior.showProfileFoldout = EditorGUILayout.Foldout(ivyBehavior.showProfileFoldout, "Ivy Profile Settings", true); EditorGUI.indentLevel--; if (EditorGUILayout.BeginFadeGroup(ivyBehavior.showProfileFoldout ? 1 : 0)) { ivyProfileEditor.OnInspectorGUI(); } EditorGUILayout.EndFadeGroup(); EditorGUILayout.EndVertical(); DrawUILine(); GUILayout.Label("Ivy Painter", EditorStyles.boldLabel); if (!isInARealScene) { EditorGUILayout.HelpBox("Painting / mesh generation only works in saved scenes on active game objects.\n- Save the scene?\n- Put this game object in a saved scene?\n- Make sure it is active?", MessageType.Error); GUI.enabled = false; } // plant root creation button var oldColor = GUI.color; GUI.color = isPlantingModeActive ? Color.yellow : Color.Lerp(Color.yellow, oldColor, 0.69f); content = new GUIContent(!isPlantingModeActive ? " Start Painting Ivy": " Stop Painting Ivy", iconPaint, "while painting, left-click and drag in the Scene view on any collider"); if (GUILayout.Button(content, GUILayout.Height(20))) { isPlantingModeActive = !isPlantingModeActive; } GUI.color = oldColor; content = new GUIContent(" Enable Growth Sim AI", "If disabled, then you can just paint ivy without simulation or AI, which is useful when you want small strokes or full control."); ivyBehavior.enableGrowthSim = EditorGUILayout.ToggleLeft(content, ivyBehavior.enableGrowthSim); content = new GUIContent(" Make Mesh During Painting / Growth", "Generate 3D ivy mesh during painting and growth. Very cool, but very processing intensive. If your computer gets very slow while painting, then disable this."); ivyBehavior.generateMeshDuringGrowth = EditorGUILayout.ToggleLeft(content, ivyBehavior.generateMeshDuringGrowth); int visibleIvy = ivyBehavior.ivyGraphs.Where(ivy => ivy.isVisible).Count(); GUI.enabled = isInARealScene && visibleIvy > 0; EditorGUILayout.BeginHorizontal(EditorStyles.helpBox); content = new GUIContent(" Re-mesh Visible", iconMesh, "Remake meshes for all visible ivy, all at once. Useful when you change your ivy profile settings, and want to see the new changes."); if (GUILayout.Button(content, EditorStyles.miniButtonLeft, GUILayout.MaxWidth(EditorGUIUtility.currentViewWidth * 0.45f), GUILayout.Height(16))) { if (EditorUtility.DisplayDialog("Hedera: Remake All Visible Meshes", string.Format("Are you sure you want to remake {0} meshes all at once? It also might be very slow or crash your editor.", visibleIvy), "YES!", "Maybe not...")) { foreach (var ivy in ivyBehavior.ivyGraphs) { if (!ivy.isVisible) { continue; } if (ivy.rootGO != null) { Undo.RegisterFullObjectHierarchyUndo(ivy.rootGO, "Hedera > Re-mesh Visible"); } else { IvyMesh.InitOrRefreshRoot(ivy, ivyProfile); Undo.RegisterCreatedObjectUndo(ivy.rootGO, "Hedera > Re-mesh Visible"); } IvyMesh.GenerateMesh(ivy, ivyProfile, ivyProfile.useLightmapping, true); } } } content = new GUIContent(" Merge Visible", iconLeaf, "Merge all visible ivy into a single ivy / single mesh. This is (usually) good for optimizing the 3D performance of your scene, especially if you have a lot of ivy everywhere."); if (GUILayout.Button(content, EditorStyles.miniButtonRight, GUILayout.Height(16))) { if (EditorUtility.DisplayDialog("Hedera: Merge All Visible Ivy Strokes", string.Format("Are you sure you want to merge {0} ivy plants into one?", visibleIvy), "YES!", "Maybe not...")) { Undo.RegisterCompleteObjectUndo(ivyBehavior, "Hedera > Merge Visible"); Undo.SetCurrentGroupName("Hedera > Merge Visible"); //var rootIvyB = IvyCore.StartDestructiveEdit(ivyBehavior, applyAllOverrides:true ); IvyCore.MergeVisibleIvyGraphs(ivyBehavior, ivyProfile); // IvyCore.CommitDestructiveEdit(); } } EditorGUILayout.EndHorizontal(); GUI.enabled = isInARealScene; if (ivyBehavior.ivyGraphs.Count == 0) { EditorGUILayout.HelpBox("To paint Ivy, first click [Start Painting Ivy]... then hold down [Left Mouse Button] on a collider in the Scene view, and drag.", MessageType.Info); } lastMeshIDs.Clear(); IvyGraph ivyGraphObjJob = null; // used to pull .OBJ export out of the for() loop var oldBGColor = GUI.backgroundColor; var pulseColor = Color.Lerp(oldBGColor, Color.yellow, Mathf.PingPong(System.Convert.ToSingle(EditorApplication.timeSinceStartup) * 2f, 1f)); for (int i = 0; i < ivyBehavior.ivyGraphs.Count; i++) { GUI.enabled = isInARealScene; var ivy = ivyBehavior.ivyGraphs[i]; if (ivy.isGrowing) { GUI.backgroundColor = pulseColor; } lastMeshIDs.Add(ivy.leafMeshID); lastMeshIDs.Add(ivy.branchMeshID); EditorGUILayout.BeginHorizontal(EditorStyles.helpBox); GUI.backgroundColor = oldBGColor; GUI.color = ivy.isVisible ? oldColor : Color.gray; var eyeIcon = ivy.isVisible ? iconVisOn : iconVisOff; content = new GUIContent(eyeIcon, "Click to toggle visibility for this ivy plant.\n(Enable / disable the game object.)"); if (GUILayout.Button(content, EditorStyles.miniButtonLeft, GUILayout.Height(16), GUILayout.Width(24))) { ivy.isVisible = !ivy.isVisible; if (ivy.rootGO != null) { ivy.rootGO.SetActive(ivy.isVisible); } } GUI.color = oldColor; GUI.color = ivy != currentIvyGraphMove ? oldColor : Color.gray; content = new GUIContent(iconMove, "Click to start moving the seed position for this ivy plant."); if (GUILayout.Button(content, EditorStyles.miniButtonRight, GUILayout.Height(16), GUILayout.Width(24))) { if (ivy.rootGO != null) { ivy.rootGO.transform.position = ivy.seedPos; } currentIvyGraphMove = ivy == currentIvyGraphMove ? null : ivy; } GUI.color = oldColor; if (ivy.rootGO != null) { GUI.enabled = false; EditorGUILayout.ObjectField(ivy.rootGO, typeof(GameObject), true); GUI.enabled = isInARealScene; } else { string ivyLabel = string.Format( "(no mesh) {0} ivy", ivy.roots.Count, ivy.seedPos ); GUILayout.Label(ivyLabel, EditorStyles.miniLabel); } if (!ivy.isGrowing) { content = new GUIContent(iconMesh, "Make (or remake) the 3D mesh for this ivy"); if (GUILayout.Button(content, EditorStyles.miniButtonLeft, GUILayout.Width(24), GUILayout.Height(16))) { if (ivy.rootGO != null) { Undo.RegisterFullObjectHierarchyUndo(ivy.rootGO, "Hedera > Make Mesh"); } else { IvyMesh.InitOrRefreshRoot(ivy, ivyProfile); Undo.RegisterCreatedObjectUndo(ivy.rootGO, "Hedera > Make Mesh"); } IvyMesh.GenerateMesh(ivy, ivyProfile, ivyProfile.useLightmapping, true); Repaint(); } GUI.enabled = ivy.branchMF != null || ivy.leafMF != null; content = new GUIContent("OBJ", iconExport, "Export ivy mesh to .OBJ file\n(Note: .OBJs only support one UV channel so they cannot have lightmap UVs, Unity must unwrap them upon import)"); if (GUILayout.Button(content, EditorStyles.miniButtonMid, GUILayout.Width(24), GUILayout.Height(16))) { ivyGraphObjJob = ivy; } GUI.enabled = isInARealScene; content = new GUIContent(iconTrash, "Delete this ivy as well as its mesh objects."); if (GUILayout.Button(content, EditorStyles.miniButtonRight, GUILayout.Width(24), GUILayout.Height(16))) { if (ivy.rootGO != null) { IvyCore.DestroyObject(ivy.rootGO); } IvyCore.TryToDestroyMeshes(ivyBehavior, ivy); Undo.RegisterCompleteObjectUndo(ivyBehavior, "Hedera > Delete Ivy"); ivyBehavior.ivyGraphs.Remove(ivy); EditorGUILayout.EndHorizontal(); i--; continue; } } else { if (GUILayout.Button("Stop Growing", EditorStyles.miniButton)) { ivy.isGrowing = false; } } EditorGUILayout.EndHorizontal(); } if (ivyBehavior.ivyGraphs.Where(ivy => ivy.isGrowing).Count() > 0) { EditorGUILayout.Space(); GUI.color = pulseColor; if (GUILayout.Button("Stop All Growing")) { IvyCore.ForceStopGrowing(); } GUI.color = oldColor; } GUI.enabled = true; EditorGUILayout.Space(); content = new GUIContent("Debug Color", "When ivy doesn't have a mesh, Hedera will visualize the ivy structure as a debug wireframe with this color in the Scene view."); ivyBehavior.debugColor = EditorGUILayout.ColorField(content, ivyBehavior.debugColor); EditorGUILayout.Space(); // was getting GUI errors doing OBJ export inline, so let's do it outside of the for() loop /* * if ( ivyGraphObjJob != null) { * var filename = ObjExport.SaveObjFile( new GameObject[] { ivyGraphObjJob.rootGO }, true ); * if ( isInARealScene * && !string.IsNullOrEmpty(filename) * && filename.StartsWith(Application.dataPath) * && AssetDatabase.IsMainAssetAtPathLoaded("Assets" + filename.Substring( Application.dataPath.Length )) * ) { * int choice = EditorUtility.DisplayDialogComplex("Hedera: Instantiate .OBJ into scene?", "You just exported ivy into a .OBJ into your project.\nDo you want to replace the ivy with the .OBJ?", "Yes, and delete old ivy", "No, don't instantiate", "Yes, and hide old ivy"); * * if ( choice == 0 || choice == 2) { * var prefab = AssetDatabase.LoadAssetAtPath<Object>( "Assets" + filename.Substring( Application.dataPath.Length ) ); * var newObj = (GameObject)PrefabUtility.InstantiatePrefab( prefab ); * Undo.RegisterCreatedObjectUndo( newObj, "Hedera > Instantiate OBJ" ); * newObj.transform.SetParent( ivyBehavior.transform ); * newObj.transform.position = ivyGraphObjJob.seedPos; * * var renders = newObj.GetComponentsInChildren<Renderer>(); * renders[0].material = ivyProfile.branchMaterial; * if ( renders.Length > 1) { renders[1].material = ivyProfile.leafMaterial; } * * if ( choice == 0 ) { // remove old ivy * if ( ivyGraphObjJob.rootGO != null) { * IvyCore.DestroyObject( ivyGraphObjJob.rootGO ); * } * IvyCore.TryToDestroyMeshes( ivyBehavior, ivyGraphObjJob); * Undo.RegisterCompleteObjectUndo( ivyBehavior, "Hedera > Instantiate OBJ" ); * ivyBehavior.ivyGraphs.Remove(ivyGraphObjJob); * } else { // just turn off old ivy * Undo.RegisterCompleteObjectUndo( ivyBehavior, "Hedera > Instantiate OBJ" ); * ivyGraphObjJob.isVisible = false; * if ( ivyGraphObjJob.rootGO != null) { * ivyGraphObjJob.rootGO.SetActive( false ); * } * } * } * } * } */ }