/// <summary> /// Loads nodes from m_NodeSerialization. /// </summary> public void LoadNodes() { // Debug.Log("Loading Nodes " + name); // Validate the NodeSerialization object if (m_NodeSerialization == null) { m_NodeSerialization = new NodeSerialization(); } // Is playing and this tree is enabled? if (m_NodesEnabled) { // Force ResetStatus if (m_FunctionNodes != null) { for (int i = 0; i < m_FunctionNodes.Length; i++) { m_FunctionNodes[i].ResetStatus(); } } // Call OnDisable in nodes if (m_Nodes != null) { for (int i = 0; i < m_Nodes.Length; i++) { m_Nodes[i].OnDisable(); } } } // Load nodes m_Nodes = m_NodeSerialization.LoadNodes(gameObject, this); if (m_HierarchyChanged) { m_HierarchyChanged = false; UpdateNodes(); } if (m_NodeSerialization.resaveNodes) { SaveNodes(); } m_FunctionNodes = this.GetFunctionNodes(); // Awake nodes? if (m_NodesWaked) { this.WakeNodes(); } // Reenable nodes if (m_NodesEnabled) { this.OnEnableNodes(); } }
/// <summary> /// Returns a copy of this node. /// <param name = "newOwner">The owner of the new node.</param> /// <returns>The copy of the node.</returns> /// </summary> public virtual ActionNode Copy(INodeOwner newOwner) { // Copy node var copy = newOwner.AddNode(GetType()); // Copy SerializedFields var fields = NodeSerialization.GetSerializedFields(copy.GetType()); for (int i = 0; i < fields.Length; i++) { // Do not copy the instanceID field if (fields[i].Name != "instanceID") { fields[i].SetValue(copy, fields[i].GetValue(this)); } } return(copy); }
/// <summary> /// Loads nodes from m_NodeSerialization. /// </summary> public void LoadNodes() { // Debug.Log("Loading Nodes " + name); // Disable nodes? if (m_NodesEnabled) { this.ForceOnDisableNodes(); } if (m_NodeSerialization == null) { m_NodeSerialization = new NodeSerialization(); } m_Nodes = m_NodeSerialization.LoadNodes(gameObject, this); if (m_NodeSerialization.resaveNodes) { SaveNodes(); } this.CreateRuntimeListsOfNodes(); m_IsDirty = false; // Call Awake in nodes if (!m_IsPrefab && Application.isPlaying) { for (int i = 0; i < m_Nodes.Length; i++) { m_Nodes[i].Awake(); } } // Reenabled nodes if (m_NodesEnabled) { this.OnEnableNodes(); } }
/// <summary> /// Loads nodes from m_NodeSerialization. /// </summary> public void LoadNodes() { // Debug.Log("Loading Nodes " + name); // Disable nodes? if (m_NodesEnabled) { this.ForceOnDisableNodes(); } if (m_NodeSerialization == null) { m_NodeSerialization = new NodeSerialization(); } m_Nodes = m_NodeSerialization.LoadNodes(gameObject, this); if (m_NodeSerialization.resaveNodes) { SaveNodes(); } this.CreateRuntimeListsOfNodes(); m_IsDirty = false; // Awake nodes? if (m_NodesWaked) { this.WakeNodes(); } // Reenabled nodes? if (m_NodesEnabled) { this.OnEnableNodes(); } }
/// <summary> /// Returns a set of serialized properties in an object. /// <param name="serializedNode">The target serialized node.</param> /// <param name="target">The object to get the properties.</param> /// <param name="targetType">The target object type.</param> /// <param name="currentPath">The property path of the target.</param> /// <returns>The serialized properties in the target object.</returns> /// </summary> static SerializedNodeProperty[] GetPropertiesData(SerializedNode serializedNode, object target, Type targetType, string currentPath = "") { // Create the property data list var propertyData = new List <SerializedNodeProperty>(); // Get serialized fields for the target type FieldInfo[] serializedFields = NodeSerialization.GetSerializedFields(targetType); for (int i = 0; i < serializedFields.Length; i++) { // Get field FieldInfo field = serializedFields[i]; // Get field type var fieldType = field.FieldType; // Get the field property attribute var propertyAttr = AttributeUtility.GetAttribute <PropertyAttribute>(field, true); // Get the property type var propertyType = SerializedNode.GetPropertyType(fieldType); // Create the property data var currentSerializedField = new SerializedNodeField(serializedNode, currentPath + field.Name, propertyType, target, field); propertyData.Add(currentSerializedField); // Variable? if (propertyType == NodePropertyType.Variable) { // Get the field value object fieldValue = target != null?field.GetValue(target) : null; // Get the children fields SerializedNodeProperty[] children = SerializedNode.GetPropertiesData(serializedNode, fieldValue, fieldValue != null ? fieldValue.GetType() : fieldType, currentPath + field.Name + "."); // Create the property drawer for the "Value" child property if (propertyAttr != null && currentSerializedField.isConcreteVariable) { foreach (var child in children) { // It is the "Value" property? if (child.label == "Value") { child.customDrawer = NodePropertyDrawer.GetDrawer(propertyAttr); } } } // Set children currentSerializedField.SetChildren(children); } // Array? else if (propertyType == NodePropertyType.Array) { // Get the array value Array array = target != null?field.GetValue(target) as Array : null; // Get array element type var elementType = fieldType.GetElementType(); // Create the array children list var childrenList = new List <SerializedNodeProperty>(); // Create the array size childrenList.Add(new SerializedArraySize(target, serializedNode, currentSerializedField.path + ".size", currentSerializedField, array, elementType)); // Create children var variableInfo = AttributeUtility.GetAttribute <VariableInfoAttribute>(field, true) ?? new VariableInfoAttribute(); childrenList.AddRange(SerializedNode.GetPropertiesData(serializedNode, target, array, elementType, currentSerializedField.path + ".", variableInfo, propertyAttr)); // Set array data children currentSerializedField.SetChildren(childrenList.ToArray()); } // Get the property drawer else if (propertyAttr != null) { currentSerializedField.customDrawer = NodePropertyDrawer.GetDrawer(propertyAttr); } } return(propertyData.ToArray()); }
private void OnGUI() { scrollPos = GUILayout.BeginScrollView(scrollPos); { for (int i = 0; i < Commands.Count; ++i) { if (i > 0) { GUILayout.Space(5.0f); } var command = Commands[i]; GUILayout.BeginHorizontal(); { //Convert the command char(s) into a single string. string str = command.StartChar.ToString(); if (command.EndChar.HasValue) { str += command.EndChar.Value; } //Show the editor. str = GUILayout.TextField(str); //Parse the string into command char(s). if (str.Length > 2) { str = str[0].ToString() + str[2]; } if (str.Length > 0) { command.StartChar = str[0]; if (str.Length == 2) { command.EndChar = str[1]; } else { command.EndChar = null; } } GUILayout.Space(7.5f); //Select the node type. EditorGUI.BeginChangeCheck(); var nodeTypesArr = nodeTypes.Select(n => n.Replace("Node", "")).ToArray(); var currentStr = NodeSerialization.GetTypeName(command.Node); int strI = Math.Max(IndexOf(currentStr), 0); strI = EditorGUILayout.Popup(strI, nodeTypesArr); if (EditorGUI.EndChangeCheck()) { command.Node = NodeSerialization.MakeNode(nodeTypes[strI]); } if (GUILayout.Button("-")) { Commands.RemoveAt(i); i -= 1; GUILayout.EndHorizontal(); continue; } } GUILayout.EndHorizontal(); //Tab in and show the editor for the selected node. GUILayout.BeginHorizontal(); GUILayout.Space(20.0f); GUILayout.BeginVertical(); { command.Node.EditorGUI(); } GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.Space(15.0f); } } GUILayout.EndScrollView(); GUILayout.BeginHorizontal(); { if (GUILayout.Button("Add")) { Commands.Add(new Command('A', new BoxNode(0.5f))); } GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); GUILayout.Space(10.0f); GUILayout.BeginHorizontal(); { if (GUILayout.Button("Save")) { var path = EditorUtility.SaveFilePanel("Choose where to save", Path.Combine(Application.dataPath, "../"), "MyCommands", "commands"); if (path != "") { var errMsg = SaveToFile(path); if (errMsg != "") { Debug.LogError("Error saving to " + path + ": " + errMsg); } } } if (GUILayout.Button("Load")) { var path = EditorUtility.OpenFilePanel("Choose the file to load", Path.Combine(Application.dataPath, "../"), "commands"); if (path != "") { var errMsg = LoadFromFile(path); if (errMsg != "") { Debug.LogError("Error loading from " + path + ": " + errMsg); } } } if (GUILayout.Button("Clear") && EditorUtility.DisplayDialog("Confirm", "Are you sure you want to clear everything?", "Yes")) { ResetList(); } } GUILayout.EndHorizontal(); GUILayout.Space(10.0f); }