public void CreateNewBehavior([NotNull] Type behaviorType, Vector2 position) { Undo.SetCurrentGroupName("Behavior Tree changed"); int undoGroup = Undo.GetCurrentGroup(); Undo.RegisterCompleteObjectUndo(m_serializedBehaviorTree, "Changed Behavior Tree"); var behavior = (SerializedBehavior_Base)ScriptableObject.CreateInstance(behaviorType); behavior.name = behaviorType.Name; AssetDatabase.AddObjectToAsset(behavior, m_serializedBehaviorTree); var node = new SerializedBehaviorTreeNode(behavior, this); AddElement(node); m_nodes.Add(node); SerializedProperty serializedDatas = m_serializedBehaviorTreeObject .FindProperty(SerializedBehaviorDataPropertyName); int index = serializedDatas.arraySize++; SerializedProperty serializedData = serializedDatas.GetArrayElementAtIndex(index); serializedData.FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue = behavior; position -= m_editorWindow.position.position; position = contentViewContainer.WorldToLocal(position); serializedData.FindPropertyRelative(NodeGraphInfoPropertyName) .FindPropertyRelative(PositionPropertyName).vector2Value = position; node.SetPosition(new Rect(position, s_defaultSize)); node.RefreshExpandedState(); node.RefreshPorts(); m_serializedBehaviorTreeObject.ApplyModifiedProperties(); Undo.CollapseUndoOperations(undoGroup); }
private void UpdatePositions([NotNull] SerializedProperty serializedBehaviorDataArray) { for (int i = 0, count = serializedBehaviorDataArray.arraySize; i < count; ++i) { SerializedProperty serializedBehaviorData = serializedBehaviorDataArray.GetArrayElementAtIndex(i); var serializedBehavior = (SerializedBehavior_Base)serializedBehaviorData .FindPropertyRelative(SerializedBehaviorPropertyName).objectReferenceValue; SerializedBehaviorTreeNode node = FindNode(serializedBehavior); Vector2 position = serializedBehaviorData.FindPropertyRelative(NodeGraphInfoPropertyName) .FindPropertyRelative(PositionPropertyName).vector2Value; node.SetPosition(new Rect(position, s_defaultSize)); } }