Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         public override void CalculateLayoutInputVertical()
         {
             int columnCount = columnWidths.Length;
             int rowCount = Mathf.CeilToInt(rectChildren.Count / (float)columnCount);

             preferredRowHeights = new float[rowCount];

             float totalMinHeight = padding.vertical;
             float totalPreferredHeight = padding.vertical;

             if (rowCount > 1)
             {
                 float heightFromSpacing = ((rowCount - 1) * rowSpacing);
                 totalMinHeight += heightFromSpacing;
                 totalPreferredHeight += heightFromSpacing;
             }

             if (flexibleRowHeight)
             {
                 // If flexibleRowHeight is enabled, find the max value for minimum and preferred heights in each row

                 float maxMinimumHeightInRow = 0;
                 float maxPreferredHeightInRow = 0;

                 for (int i = 0; i < rowCount; i++)
                 {
                     maxMinimumHeightInRow = minimumRowHeight;
                     maxPreferredHeightInRow = minimumRowHeight;

                     for (int j = 0; j < columnCount; j++)
                     {
                         int childIndex = (i * columnCount) + j;

                         // Safeguard against tables with incomplete rows
                         if (childIndex == rectChildren.Count)
                             break;

                         maxPreferredHeightInRow = Mathf.Max(LayoutUtility.GetPreferredHeight(rectChildren[childIndex]), maxPreferredHeightInRow);
                         maxMinimumHeightInRow = Mathf.Max(LayoutUtility.GetMinHeight(rectChildren[childIndex]), maxMinimumHeightInRow);
                     }

                     totalMinHeight += maxMinimumHeightInRow;
                     totalPreferredHeight += maxPreferredHeightInRow;

                     // Add calculated row height to a commonly accessible array for reuse in SetLayoutVertical()
                     preferredRowHeights[i] = maxPreferredHeightInRow;
                 }
             }
             else
             {
                 // If flexibleRowHeight is disabled, then use the minimumRowHeight to calculate vertical layout information
                 for (int i = 0; i < rowCount; i++)
                     preferredRowHeights[i] = minimumRowHeight;

                 totalMinHeight += rowCount * minimumRowHeight;
                 totalPreferredHeight = totalMinHeight;
             }

             totalPreferredHeight = Mathf.Max(totalMinHeight, totalPreferredHeight);
             SetLayoutInputForAxis(totalMinHeight, totalPreferredHeight, 1, 1);
         }