Exemplo n.º 1
0
    Dictionary <string, Element> ImportElements(ExcelImporter excel)
    {
        if (!excel.TryGetNamedRange("Elements", out ExcelImporter.Range range))
        {
            throw new System.IO.InvalidDataException($"No Elements range was found!");
        }

        var assets        = GetAllAssetsOfType <Element>();
        int originalCount = assets.Count;

        var elementsInUse = new List <Element>();

        for (int row = 1; row <= range.RowCount; row++)
        {
            string name = range.GetValue <string>(row, 1);
            if (string.IsNullOrWhiteSpace(name))
            {
                continue;
            }

            var element = GetOrCreateAsset(name, assets, "Elements");

            elementsInUse.Add(element);
        }

        if (!excel.TryGetNamedRange("ElementMultiplier", out range))
        {
            throw new System.IO.InvalidDataException($"No Element Multiplier range was found!");
        }

        MarkChangesForSaving(elementCollection);
        elementCollection.SetElementCount(elementsInUse.Count);

        var multipliers = new float[elementsInUse.Count];

        for (int row = 0; row < elementsInUse.Count; row++)
        {
            for (int column = 0; column < elementsInUse.Count; column++)
            {
                multipliers[column] = range.GetValue <float>(row + 1, column + 1);
            }
            elementCollection.SetElement(row, elementsInUse[row], multipliers);
        }

        Debug.Log($"Successfully imported {elementsInUse.Count} elements " +
                  $"({assets.Count - originalCount} new, {assets.Count - elementsInUse.Count} unused).");
        return(assets);
    }