Unity3D實(shí)現(xiàn)物體排成弧行
本文實(shí)例為大家分享了Unity3D實(shí)現(xiàn)物體排成弧行的具體代碼,供大家參考,具體內(nèi)容如下
一般用在Pico、HTC、DP等VR設(shè)備中
效果:

完整代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CanvasPositionManager : MonoBehaviour
{
private float radius = 700f;//圓的半徑
private int numberOfObjects;//每行排列多少個(gè)物體
private int theChildCount;//需要排列的物體的總個(gè)數(shù)
private void Awake()
{
if (this.transform.name == "GGKFTherUIP")//這里可以忽略,是我自己的需求,根據(jù)不同場(chǎng)景中的物體名字決定一行排列多少個(gè)
{
numberOfObjects = 5;
}
else
{
numberOfObjects = 10;
}
theChildCount = this.transform.childCount;//物體總個(gè)數(shù)就是當(dāng)前物體下的子物體的個(gè)數(shù)
GerCurP(this.transform);//排列
}
private void Start()
{
}
/// <summary>
/// 半圓排列
/// </summary>
/// <param name="trans"></param>
public void GerCurP(Transform trans)
{
if (theChildCount <= numberOfObjects)//如果總個(gè)數(shù)小于等于一行的個(gè)數(shù),那只需要排列一行
{
print("個(gè)數(shù)不超過(guò)十個(gè)");
for (int i = 0; i < trans.childCount; i++)
{
float angle = i * Mathf.PI/ numberOfObjects;//根據(jù)每個(gè)物體(i)乘圓周率(Π)
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
this.transform.GetChild(i).position = pos;
}
}
else
{
print("個(gè)數(shù)!!!超過(guò)十個(gè)");
int temp = trans.childCount / numberOfObjects;//行數(shù)(偽行數(shù))
int tempNumber;//記過(guò)下邊的if else計(jì)算,得出真正所需的行數(shù)(真行數(shù))
float highUp = 0;
if (temp % numberOfObjects == 0)
{
tempNumber = temp;
}
else//對(duì)10取余不為零,補(bǔ)一行
{
tempNumber = temp + 1;
}
Debug.Log("總共有幾行" + tempNumber);
//排列思路:(我的每個(gè)物體高度是200)第一行排在-200,然后每行依次+200,最后一行排在第一行下邊也就是-400,這樣開(kāi)起來(lái)比較居中。因?yàn)榕帕刑嘈袝?huì)看不清楚內(nèi)容,所以一般五六行就夠了,所以采用比較固(僵)定(硬)的排列方式,可以根據(jù)自己需求更改。
for (int i = 0; i < tempNumber; i++)//循環(huán)幾列
{
if (i == tempNumber - 1)//最后一行Y坐標(biāo)需要排在第一行的下邊(固定值,-400位置)
{
for (int j = (numberOfObjects * i); j < trans.childCount; j++)//最后一行的頭到最終末尾
{
if (j >= (numberOfObjects * i) && j < trans.childCount)
{
float angle = (j - (numberOfObjects * i)) * Mathf.PI/ numberOfObjects;//每行的每個(gè)點(diǎn)占圓周率的比例
print(angle);
Vector3 pos = new Vector3(-Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;//對(duì)angle取余弦和正弦值再乘以半徑獲得當(dāng)前物體在的坐標(biāo)
this.transform.GetChild(j).position = new Vector3(pos.x, pos.y - 400, pos.z);//坐標(biāo)賦值
}
}
}
else
{
for (int j = (numberOfObjects * i); j < numberOfObjects * (i + 1); j++)//每行的開(kāi)頭到當(dāng)前行的末尾
{
if (j >= (numberOfObjects * i) && j < numberOfObjects * (i + 1))
{
float angle = (j - (numberOfObjects * i)) * Mathf.PI/ numberOfObjects;//
print(angle);
Vector3 pos = new Vector3(-Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
this.transform.GetChild(j).position = new Vector3(pos.x, pos.y + highUp - 200, pos.z);
}
}
}
highUp += 200;
}
}
}
}
調(diào)整所有對(duì)象的朝向(每個(gè)物體都掛載)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testSortUI : MonoBehaviour {
private Transform centralPoint;//這個(gè)是圓的中心點(diǎn)
private void Start()
{
centralPoint = GameObject.FindGameObjectWithTag("contralpoint").transform;
this.transform.forward = this.transform.position - centralPoint.up;//所有物體看向圓心
this.transform.localEulerAngles = new Vector3(0, this.transform.localEulerAngles.y, this.transform.localEulerAngles.z);//微調(diào),使得此物體看向正前方,將此行注釋?zhuān)梢钥吹矫黠@區(qū)別
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法,很適合初學(xué)者更好的理解C#字符串原理,需要的朋友可以參考下2014-08-08
c# SQLHelper(for winForm)實(shí)現(xiàn)代碼
利用C#自定義一個(gè)時(shí)間類(lèi)型YearMonth
unity實(shí)現(xiàn)屏幕上寫(xiě)字效果
C# Marshal類(lèi)基本概念和入門(mén)實(shí)例講解
C#實(shí)現(xiàn)綁定DataGridView與TextBox之間關(guān)聯(lián)的方法

