- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38266
- 精华
- 111
|
游戏中经常把一些游戏物体进行有规律的排列,以下是我写的最简单的排列。- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class ObjSort
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="parent"></param>
- /// <param name="objs"></param>
- /// <param name="row">多少列</param>
- /// <param name="cellWidth">物体的宽</param>
- /// <param name="cellHeight">物体的高</param>
- /// <param name="spaceWidth">物体左右空隙</param>
- /// <param name="spaceHeight">物体上下空隙</param>
- public static void Stor(GameObject parent, List<GameObject> objs, int row, float cellWidth,
- float cellHeight, float spaceWidth, float spaceHeight)
- {
- if (objs == null)
- {
- return;
- }
- for (int i = 0; i < objs.Count; i++)
- {
- GameObject obj = objs[i];
- obj.transform.parent = parent.transform;
- obj.transform.localPosition = Vector3.zero;
- obj.transform.localScale = Vector3.one;
- obj.transform.localEulerAngles = Vector3.zero;
- int w = i / row;
- int h = i % row;
- float x = (cellWidth + spaceWidth) * h;
- float y = -(cellHeight + spaceHeight) * w;
- obj.transform.localPosition = new Vector3(x, y, 0);
- }
- }
- }
复制代码 |
|