纳金网

标题: Unity最简单的物体位置排列 [打印本页]

作者: 狂风大尉    时间: 2015-6-29 00:26
标题: Unity最简单的物体位置排列

游戏中经常把一些游戏物体进行有规律的排列,以下是我写的最简单的排列。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class ObjSort
  5. {
  6.     /// <summary>
  7.     ///
  8.     /// </summary>
  9.     /// <param name="parent"></param>
  10.     /// <param name="objs"></param>
  11.     /// <param name="row">多少列</param>
  12.     /// <param name="cellWidth">物体的宽</param>
  13.     /// <param name="cellHeight">物体的高</param>
  14.     /// <param name="spaceWidth">物体左右空隙</param>
  15.     /// <param name="spaceHeight">物体上下空隙</param>
  16.     public static void Stor(GameObject parent, List<GameObject> objs, int row, float cellWidth,
  17.         float cellHeight, float spaceWidth, float spaceHeight)
  18.     {
  19.         if (objs == null)
  20.         {

  21.             return;
  22.         }
  23.         for (int i = 0; i < objs.Count; i++)
  24.         {
  25.             GameObject obj = objs[i];
  26.             obj.transform.parent = parent.transform;
  27.             obj.transform.localPosition = Vector3.zero;
  28.             obj.transform.localScale = Vector3.one;
  29.             obj.transform.localEulerAngles = Vector3.zero;
  30.             int w = i / row;
  31.             int h = i % row;
  32.             float x = (cellWidth + spaceWidth) * h;
  33.             float y = -(cellHeight + spaceHeight) * w;
  34.             obj.transform.localPosition = new Vector3(x, y, 0);
  35.         }
  36.     }
  37. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5