查看: 1043|回复: 0
打印 上一主题 下一主题

[其他] Unity最简单的物体位置排列

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-6-29 00:26:27 |只看该作者 |倒序浏览

游戏中经常把一些游戏物体进行有规律的排列,以下是我写的最简单的排列。
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-4 16:00 , Processed in 0.134879 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部