/// <summary> /// Updates the active node data. /// <param name="activeNode">The active node.</param> /// </summary> void UpdateActiveNode(ActionNode activeNode) { var activeNodeType = activeNode != null?activeNode.GetType() : null; // The active node type has changed? if (activeNodeType != m_ActiveNodeType) { if (activeNode == null) { m_ActiveNodeType = null; m_ActiveNodeInfo = null; m_ActiveNodeIcon = null; m_ActiveNodeTypeName = string.Empty; return; } else { m_ActiveNodeType = activeNodeType; m_ActiveNodeInfo = AttributeUtility.GetAttribute <NodeInfoAttribute>(m_ActiveNodeType, true) ?? new NodeInfoAttribute(); m_ActiveNodeIcon = IconUtility.GetIcon(m_ActiveNodeType); m_ActiveNodeTypeName = " (" + m_ActiveNodeType.Name + ")"; } } }
/// <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()); }
/// <summary> /// Shows the context menu. /// </summary> void OnContextMenu() { var menu = new GenericMenu(); var activeFsm = BehaviourWindow.activeFsm; if (activeFsm != null) { m_LastMousePos = Event.current.mousePosition; // Get the states scripts MonoScript[] stateScripts = FileUtility.GetScripts <InternalStateBehaviour>(); for (int i = 0; i < stateScripts.Length; i++) { System.Type type = stateScripts[i].GetClass(); // Get the component path string componentPath = "Add State/"; AddComponentMenu componentMenu = AttributeUtility.GetAttribute <AddComponentMenu>(type, false); if (componentMenu == null || componentMenu.componentMenu != string.Empty) { componentMenu = AttributeUtility.GetAttribute <AddComponentMenu>(type, true); if (componentMenu != null && componentMenu.componentMenu != string.Empty) { componentPath += componentMenu.componentMenu; } else { componentPath += type.ToString().Replace('.', '/'); } menu.AddItem(new GUIContent(componentPath), false, delegate() { InternalStateBehaviour newState = StateUtility.AddState(activeFsm, type); // Sets the newState position and dirty flag if (newState != null) { newState.position = m_LastMousePos - new Vector2(StateGUI.defaultWidth, StateGUI.defaultHeight) * .5f; EditorUtility.SetDirty(newState); } }); } } } else { menu.AddDisabledItem(new GUIContent("Add State")); } // Separator menu.AddSeparator(""); menu.AddItem(new GUIContent("Copy FSM"), false, delegate() { StateUtility.statesToPaste = new InternalStateBehaviour[] { activeFsm }; }); if (StateUtility.statesToPaste != null && StateUtility.statesToPaste.Length > 0 && activeFsm != null) { menu.AddItem(new GUIContent("Paste State"), false, delegate() { StateUtility.PasteStates(activeFsm); }); } else { menu.AddDisabledItem(new GUIContent("Paste State")); } // Separator menu.AddSeparator(""); menu.AddItem(new GUIContent("Delete FSM"), false, delegate() { StateUtility.Destroy(activeFsm); }); // menu.AddSeparator(""); // Separator // if (BehaviourWindow.Instance != null) // menu.AddItem(new GUIContent("Refresh"), false, BehaviourWindow.Instance.Refresh); // else // menu.AddDisabledItem(new GUIContent("Refresh")); // Shows the context menu menu.ShowAsContext(); }
/// <summary> /// Returns an icon fot the supplied type. /// <param name="type">The type to get the icon.</param> /// <param name="obj">Optional paramater to get icon using EditorGUIUtility.ObjectContent.</param> /// <returns>The icon for the supplied type.</returns> /// </summary> public static Texture GetIcon(System.Type type, UnityEngine.Object obj = null) { Texture texture; s_Icons.TryGetValue(type, out texture); if (texture == null) { if (typeof(ActionNode).IsAssignableFrom(type)) { var attribute = AttributeUtility.GetAttribute <NodeInfoAttribute>(type, true) ?? new NodeInfoAttribute(); texture = Resources.Load("Icons/" + attribute.icon) as Texture ?? EditorGUIUtility.FindTexture(attribute.icon + " Icon"); if (texture == null) { texture = EditorGUIUtility.FindTexture(attribute.icon); } } else if (typeof(UnityEngine.Object).IsAssignableFrom(type)) { texture = EditorGUIUtility.ObjectContent(obj, type).image; if (texture == null) { EditorGUIUtility.FindTexture(type.Name + " Icon"); } } else if (typeof(BehaviourMachine.Variable).IsAssignableFrom(type)) { var customVarAttr = AttributeUtility.GetAttribute <CustomVariableAttribute>(type, false); if (customVarAttr != null) { texture = Resources.Load("Icons/" + customVarAttr.icon) as Texture ?? EditorGUIUtility.FindTexture(customVarAttr.icon + " Icon"); if (texture == null) { texture = EditorGUIUtility.FindTexture(customVarAttr.icon); } } } else { texture = EditorGUIUtility.FindTexture(type.Name + " Icon"); } if (texture == null) { texture = EditorGUIUtility.FindTexture("DefaultAsset Icon"); } if (texture != null) { if (!s_Icons.ContainsKey(type)) { s_Icons.Add(type, texture); } else { s_Icons[type] = texture; } } } return(texture); }
/// <summary> /// Reset target node. /// </summary> void OpenNodeReference() { var nodeInfo = AttributeUtility.GetAttribute <NodeInfoAttribute>(target.GetType(), false) ?? new NodeInfoAttribute(); Application.OpenURL(nodeInfo.url); }