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:

   protected void LayoutRow(IList contents, float rowWidth, float rowHeight, float maxWidth, float xOffset, float yOffset, int axis)
   {
    var xPos = xOffset;

    if (!ChildForceExpandWidth && IsCenterAlign)
     xPos += (maxWidth - rowWidth) * 0.5f;
    else if (!ChildForceExpandWidth && IsRightAlign)
     xPos += (maxWidth - rowWidth);

    var extraWidth = 0f;
    var extraSpacing = 0f;

    if (ChildForceExpandWidth) {
     extraWidth = (maxWidth - rowWidth)/_rowList.Count;
    }
    else if (ExpandHorizontalSpacing) {
     extraSpacing = (maxWidth - rowWidth)/(_rowList.Count - 1);
     if (_rowList.Count > 1) {
      if (IsCenterAlign)
       xPos -= extraSpacing * 0.5f * (_rowList.Count - 1);
      else if (IsRightAlign)
       xPos -= extraSpacing * (_rowList.Count - 1);
     }
    }

    for (var j = 0; j < _rowList.Count; j++) {

     var index = IsLowerAlign ? _rowList.Count - 1 - j : j;

     var rowChild = _rowList[index];

     var rowChildWidth = LayoutUtility.GetPreferredSize(rowChild, 0) + extraWidth;
     var rowChildHeight = LayoutUtility.GetPreferredSize(rowChild, 1);

     if (ChildForceExpandHeight)
      rowChildHeight = rowHeight;

     rowChildWidth = Mathf.Min(rowChildWidth, maxWidth);

     var yPos = yOffset;

     if (IsMiddleAlign)
      yPos += (rowHeight - rowChildHeight) * 0.5f;
     else if (IsLowerAlign)
      yPos += (rowHeight - rowChildHeight);

     //
     if (ExpandHorizontalSpacing && j > 0)
      xPos += extraSpacing;

     if (axis == 0)
      SetChildAlongAxis(rowChild, 0, xPos, rowChildWidth);
     else
      SetChildAlongAxis(rowChild, 1, yPos, rowChildHeight);

     // Don't do horizontal spacing for the last one
     if (j < _rowList.Count - 1 )
      xPos += rowChildWidth + SpacingX;
    }
   }