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ù)不超過十個(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ù)!!!超過十個(gè)");
int temp = trans.childCount / numberOfObjects;//行數(shù)(偽行數(shù))
int tempNumber;//記過下邊的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,這樣開起來比較居中。因?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++)//每行的開頭到當(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),使得此物體看向正前方,將此行注釋,可以看到明顯區(qū)別
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 動(dòng)態(tài)調(diào)用WebService的示例
這篇文章主要介紹了C# 動(dòng)態(tài)調(diào)用WebService的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11
HTML文本框的值改變后觸發(fā)后臺(tái)代碼的方法
asp.net用日期插件,當(dāng)選中一個(gè)日期時(shí)觸發(fā)一個(gè)事件,以查詢當(dāng)前日期的數(shù)據(jù)。這是要跟數(shù)據(jù)庫交互的。先貼出控件代碼:2013-04-04
C#使用MailAddress類發(fā)送html格式郵件的實(shí)例代碼
這篇文章主要介紹如何使用C#的MailAddress類發(fā)送郵件的方法,大家參考使用吧2013-11-11
C#調(diào)用帶結(jié)構(gòu)體指針Dll的方法
在C#到底該如何安全的調(diào)用這樣的DLL接口函數(shù)呢?本文將詳細(xì)介紹如何調(diào)用各種參數(shù)的方法,對(duì)C#結(jié)構(gòu)體指針DLL相關(guān)知識(shí)感興趣的朋友一起看看吧2021-07-07
C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)
這篇文章主要介紹了如何使用C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)的方法,并附上全部源碼,分享給大家,有需要的小伙伴可以參考下。2015-12-12

