Example #1
0
        /// <summary>
        /// Invoked when the forge is enabled. We us this to set all the components
        /// we require for our forge.
        /// </summary>
        protected void SetupComponents()
        {
            // Removed null entries (script changes can make this happen)
            for (int i = m_Components.Count - 1; i >= 0; i--)
            {
                if (m_Components[i] == null)
                {
                    m_Components.RemoveAt(i);
                }
            }
            // Get our current type
            Type forgeType = GetType();
            // Get the attribute
            RequiredWidgetComponetsAttribute requiredWidgets = Attribute.GetCustomAttribute(forgeType, typeof(RequiredWidgetComponetsAttribute)) as RequiredWidgetComponetsAttribute;
            // Keep a reference to our list of components so we can remove them at the end if their are extra
            List <ForgeComponent> componentList = new List <ForgeComponent>(m_Components);

            // If it's not null
            if (requiredWidgets != null)
            {
                // Loop over all required types
                foreach (Type requiredType in requiredWidgets.requiredTypes)
                {
                    // Set a flag to see if we have a match.
                    bool foundType = false;
                    // Loop over all components
                    for (int i = componentList.Count - 1; i >= 0; i--)
                    {
                        // Check if the type matches
                        if (componentList[i].GetType() == requiredType)
                        {
                            // We have a match.
                            foundType = true;
                            componentList.RemoveAt(i);
                            break;
                        }
                    }
                    // If we did not find a match we have to create one
                    if (!foundType)
                    {
                        // Create the instance.
                        ForgeComponent component = CreateInstance(requiredType) as ForgeComponent;
                        // Add it to our list
                        m_Components.Add(component);
                    }
                }
            }
            // Any components still left in the list are extra and should be removed
            for (int i = 0; i < componentList.Count; i++)
            {
                // Remove it.
                m_Components.Remove(componentList[i]);
                // Destroy the scriptable object.
                DestroyImmediate(componentList[i], true);
            }
            componentList = null;
        }
Example #2
0
        /// <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]);
                    }
                }
            }
        }