protected virtual void DrawDeformersList() { deformerList.DoLayoutList(); var newDeformers = EditorGUILayoutx.DragAndDropArea <Deformer>(); if (newDeformers != null && newDeformers.Count > 0) { Undo.RecordObjects(targets, "Added Deformers"); foreach (var t in targets) { var elements = ((Deformable)t).DeformerElements; foreach (var newDeformer in newDeformers) { elements.Add(new DeformerElement(newDeformer)); } } // I'd like to give a massive thanks and credit to Thomas Ingram for taking time out of his day to // solve an abomination of a bug and find this fix. He truly is an editor scripting legend. // Changing fields directly with multiple objects selected doesn't dirty the serialized object for some reason. // To force it to be dirty you have to call this method. serializedObject.SetIsDifferentCacheDirty(); serializedObject.Update(); } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); serializedObject.UpdateIfRequiredOrScript(); deformerList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); EditorGUILayout.Space(); var newDeformers = EditorGUILayoutx.DragAndDropArea <Deformer> (); if (newDeformers != null && newDeformers.Count > 0) { Undo.RecordObjects(targets, "Added Deformers"); foreach (var t in targets) { var elements = ((GroupDeformer)t).DeformerElements; foreach (var newDeformer in newDeformers) { elements.Add(new DeformerElement(newDeformer)); } } } EditorGUILayout.Space(); using (new EditorGUILayout.HorizontalScope()) { if (GUILayout.Button(Content.ClearDeformers, EditorStyles.miniButtonLeft)) { Undo.RecordObjects(targets, "Cleared Deformers"); foreach (var t in targets) { ((GroupDeformer)t).DeformerElements.Clear(); } } if (GUILayout.Button(Content.CleanDeformers, EditorStyles.miniButtonRight)) { Undo.RecordObjects(targets, "Cleaned Deformers"); foreach (var t in targets) { ((GroupDeformer)t).DeformerElements.RemoveAll(d => d.Component == null); } } } EditorApplication.QueuePlayerLoopUpdate(); }
public override void OnInspectorGUI() { serializedObject.UpdateIfRequiredOrScript(); using (var check = new EditorGUI.ChangeCheckScope()) { EditorGUILayout.PropertyField(properties.UpdateMode, Content.UpdateMode); if (check.changed) { serializedObject.ApplyModifiedProperties(); foreach (var t in targets) { ((Deformable)t).UpdateMode = (UpdateMode)properties.UpdateMode.enumValueIndex; } } } EditorGUILayout.PropertyField(properties.NormalsRecalculation, Content.NormalsRecalculation); EditorGUILayout.PropertyField(properties.BoundsRecalculation, Content.BoundsRecalculation); if (properties.BoundsRecalculation.hasMultipleDifferentValues || (BoundsRecalculation)properties.BoundsRecalculation.enumValueIndex == BoundsRecalculation.Custom) { using (new EditorGUI.IndentLevelScope()) { EditorGUILayout.PropertyField(properties.CustomBounds, Content.CustomBounds); } } EditorGUILayout.PropertyField(properties.ColliderRecalculation, Content.ColliderRecalculation); if (properties.ColliderRecalculation.hasMultipleDifferentValues || (ColliderRecalculation)properties.ColliderRecalculation.enumValueIndex == ColliderRecalculation.Auto) { using (new EditorGUI.IndentLevelScope()) EditorGUILayout.PropertyField(properties.MeshCollider, Content.MeshCollider); } EditorGUILayout.Space(); deformerList.DoLayoutList(); var newDeformers = EditorGUILayoutx.DragAndDropArea <Deformer> (); if (newDeformers != null && newDeformers.Count > 0) { Undo.RecordObjects(targets, "Added Deformers"); foreach (var t in targets) { var elements = ((Deformable)t).DeformerElements; foreach (var newDeformer in newDeformers) { elements.Add(new DeformerElement(newDeformer)); } } // I'd like to give a massive thanks and credit to Thomas Ingram for taking time out of his day to // solve an abomination of a bug and find this fix. He truly is an editor scripting legend. // Changing fields directly with multiple objects selected doesn't dirty the serialized object for some reason. // To force it to be dirty you have to call this method. serializedObject.SetIsDifferentCacheDirty(); serializedObject.Update(); } EditorGUILayout.Space(); using (new EditorGUILayout.HorizontalScope()) { var selectedIndex = GUILayout.Toolbar(-1, Content.UtilityToolbar, EditorStyles.miniButton, GUILayout.MinWidth(0)); switch (selectedIndex) { default: throw new System.ArgumentException($"No valid action for toolbar index {selectedIndex}."); case -1: break; case 0: Undo.RecordObjects(targets, "Cleared Deformers"); foreach (var t in targets) { ((Deformable)t).DeformerElements.Clear(); } break; case 1: Undo.RecordObjects(targets, "Cleaned Deformers"); foreach (var t in targets) { ((Deformable)t).DeformerElements.RemoveAll(d => d.Component == null); } break; case 2: foreach (var t in targets) { var deformable = t as Deformable; // C:/...<ProjectName>/Assets/ var projectPath = Application.dataPath + "/"; var assetPath = EditorUtility.SaveFilePanelInProject("Save Obj", $"{deformable.name}.obj", "obj", ""); if (string.IsNullOrEmpty(assetPath)) { break; } var fileName = assetPath; // Now that we have a unique asset path we can remove the "Assets/" and ".obj" to get the unique name. // It's pretty gross, but it works and this code doesn't need to be performant. fileName = fileName.Remove(0, 7); fileName = fileName.Remove(fileName.Length - 4, 4); ObjExporter.SaveMesh(deformable.GetMesh(), deformable.GetRenderer(), projectPath, fileName); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); } break; case 3: foreach (var t in targets) { var deformable = t as Deformable; var assetPath = EditorUtility.SaveFilePanelInProject("Save Mesh Asset", $"{deformable.name}.asset", "asset", ""); if (string.IsNullOrEmpty(assetPath)) { break; } AssetDatabase.CreateAsset(Instantiate(deformable.GetMesh()), assetPath); AssetDatabase.SaveAssets(); } break; } } EditorGUILayout.Space(); if (foldoutDebug = EditorGUILayoutx.FoldoutHeader("Debug Info", foldoutDebug)) { var vertexCount = 0; var modifiedData = DataFlags.None; var bounds = (target as Deformable).GetMesh().bounds; foreach (var t in targets) { var deformable = t as Deformable; var mesh = deformable.GetMesh(); if (mesh != null) { vertexCount += deformable.GetMesh().vertexCount; } modifiedData |= deformable.ModifiedDataFlags; } EditorGUILayout.LabelField($"Vertex Count: {vertexCount}", Styles.WrappedLabel); EditorGUILayout.LabelField($"Modified Data: {modifiedData.ToString ()}", Styles.WrappedLabel); EditorGUILayout.LabelField($"Bounds: {bounds.ToString ()}", Styles.WrappedLabel); } serializedObject.ApplyModifiedProperties(); foreach (var t in targets) { var deformable = t as Deformable; var originalMesh = deformable.GetOriginalMesh(); if (originalMesh != null && !originalMesh.isReadable) { EditorGUILayout.HelpBox(Content.ReadWriteNotEnableAlert, MessageType.Error); } } EditorApplication.QueuePlayerLoopUpdate(); }