public void Define() { var preservation = UnitPreservation.Preserve(this); // A unit needs to undefine even if it wasn't defined, // because there might be invalid ports and connections // that we need to clear to avoid duplicates on definition. Undefine(); if (canDefine) { try { Definition(); isDefined = true; definitionException = null; AfterDefine(); } catch (Exception ex) { Undefine(); definitionException = ex; Debug.LogWarning($"Failed to define {this}:\n{ex}"); } } preservation.RestoreTo(this); }
private void ReplaceUnit() { var oldUnit = unit; var unitPosition = oldUnit.position; var preservation = UnitPreservation.Preserve(oldUnit); var options = new UnitOptionTree(new GUIContent("Unit")); options.reference = reference; var activatorPosition = new Rect(e.mousePosition, new Vector2(200, 1)); var context = this.context; LudiqGUI.FuzzyDropdown ( activatorPosition, options, null, delegate(object _option) { var option = (IUnitOption)_option; context.BeginEdit(); UndoUtility.RecordEditedObject("Replace Unit"); var graph = oldUnit.graph; oldUnit.graph.units.Remove(oldUnit); var newUnit = option.InstantiateUnit(); newUnit.guid = Guid.NewGuid(); newUnit.position = unitPosition; graph.units.Add(newUnit); preservation.RestoreTo(newUnit); option.PreconfigureUnit(newUnit); selection.Select(newUnit); GUI.changed = true; context.EndEdit(); } ); }