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 float SetLayout(float width, int axis, bool layoutInput)
   {
    var groupHeight = rectTransform.rect.height;

    // Width that is available after padding is subtracted
    var workingWidth = rectTransform.rect.width - padding.left - padding.right;

    // Accumulates the total height of the rows, including spacing and padding.
    var yOffset = IsLowerAlign ? (float)padding.bottom : (float)padding.top;

    var currentRowWidth = 0f;
    var currentRowHeight = 0f;

    for (var i = 0; i < rectChildren.Count; i++) {

     // LowerAlign works from back to front
     var index = IsLowerAlign ? rectChildren.Count - 1 - i : i;

     var child = rectChildren[index];

     var childWidth = LayoutUtility.GetPreferredSize(child, 0);
     var childHeight = LayoutUtility.GetPreferredSize(child, 1);

     // Max child width is layout group width - padding
     childWidth = Mathf.Min(childWidth, workingWidth);

     // If adding this element would exceed the bounds of the row,
     // go to a new line after processing the current row
     if (currentRowWidth + childWidth > workingWidth) {

      currentRowWidth -= SpacingX;

      // Process current row elements positioning
      if (!layoutInput) {

       var h = CalculateRowVerticalOffset(groupHeight, yOffset, currentRowHeight);
       LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth, padding.left, h, axis);

      }

      // Clear existing row
      _rowList.Clear();

      // Add the current row height to total height accumulator, and reset to 0 for the next row
      yOffset += currentRowHeight;
      yOffset += SpacingY;

      currentRowHeight = 0;
      currentRowWidth = 0;

     }

     currentRowWidth += childWidth;
     _rowList.Add(child);

     // We need the largest element height to determine the starting position of the next line
     if (childHeight > currentRowHeight) {
      currentRowHeight = childHeight;
     }

     // Don't do this for the last one
     if (i < rectChildren.Count - 1 )
      currentRowWidth += SpacingX;
    }

    if (!layoutInput) {
     var h = CalculateRowVerticalOffset(groupHeight, yOffset, currentRowHeight);
     currentRowWidth -= SpacingX;
     // Layout the final row
     LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth - (_rowList.Count > 1 ? SpacingX : 0), padding.left, h, axis);
    }

    _rowList.Clear();

    // Add the last rows height to the height accumulator
    yOffset += currentRowHeight;
    yOffset += IsLowerAlign ? padding.top : padding.bottom;

    if (layoutInput) {

     if(axis == 1)
      SetLayoutInputForAxis(yOffset, yOffset, -1, axis);

    }

    return yOffset;
   }