/// <summary> /// Called by our generic menu when a new forge is added. /// </summary> private void OnWidgetAdded(object widgetType) { m_Target.OnWidgetAdded(widgetType); m_AddForgeButtonSelected = false; }
/// <summary> /// Reads the yaml defined on disk if there is one for a Scriptable Forge or /// creates a new one. /// </summary> private static void ReadInstanceFromDiskOrCreateNew() { // Store our path string savePath = GetSavePath(); ScriptableForge m_Instance = null; List <Widget> m_LoadedWidgets = new List <Widget>(); // Does it exist already? if (File.Exists(savePath)) { // It does so lets load it Object[] loadedObject = InternalEditorUtility.LoadSerializedFileAndForget(savePath); // Loop over all our objects and find the one we care about (There should really only be one) for (int i = 0; i < loadedObject.Length; i++) { if (loadedObject[i] is ScriptableForge) { // Save our instance m_Instance = loadedObject[i] as ScriptableForge; } else { if (loadedObject[i] is Widget) { m_LoadedWidgets.Add(loadedObject[i] as Widget); } } } } else { // We don't have a save file so we create a new one. m_Instance = CreateInstance <ScriptableForge>(); } // Initialize all the widgets. for (int i = 0; i < m_LoadedWidgets.Count; i++) { m_LoadedWidgets[i].Initalize(m_Instance); } // Sort them in order. m_LoadedWidgets.Sort(); // Save them back to our instance. m_Instance.Widgets = m_LoadedWidgets; // Regenerate Widgets if they are set to auto build. for (int i = 0; i < m_LoadedWidgets.Count; i++) { m_LoadedWidgets[i].OnLoaded(); } // Get all our widget types. List <Type> widgetTypes = GetDefinedWidgetTypes(includeAbstractTypes: false); // Remove all that are not required. for (int i = widgetTypes.Count - 1; i >= 0; i--) { if (Attribute.GetCustomAttribute(widgetTypes[i], typeof(RequiredWidgetAttribute)) != null) { bool foundWidget = false; for (int x = 0; x < m_LoadedWidgets.Count; x++) { if (m_LoadedWidgets[x].GetType() == widgetTypes[i]) { foundWidget = true; // We found the type so we move on. break; } } if (!foundWidget) { m_Instance.OnWidgetAdded(widgetTypes[i]); } } } }