Unity3D實現(xiàn)物體排成弧行
更新時間:2020年09月17日 09:00:56 作者:SandmanRUN
這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)物體排成弧行,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity3D實現(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;//每行排列多少個物體 private int theChildCount;//需要排列的物體的總個數(shù) private void Awake() { if (this.transform.name == "GGKFTherUIP")//這里可以忽略,是我自己的需求,根據(jù)不同場景中的物體名字決定一行排列多少個 { numberOfObjects = 5; } else { numberOfObjects = 10; } theChildCount = this.transform.childCount;//物體總個數(shù)就是當前物體下的子物體的個數(shù) GerCurP(this.transform);//排列 } private void Start() { } /// <summary> /// 半圓排列 /// </summary> /// <param name="trans"></param> public void GerCurP(Transform trans) { if (theChildCount <= numberOfObjects)//如果總個數(shù)小于等于一行的個數(shù),那只需要排列一行 { print("個數(shù)不超過十個"); for (int i = 0; i < trans.childCount; i++) { float angle = i * Mathf.PI/ numberOfObjects;//根據(jù)每個物體(i)乘圓周率(Π) Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; this.transform.GetChild(i).position = pos; } } else { print("個數(shù)!!!超過十個"); int temp = trans.childCount / numberOfObjects;//行數(shù)(偽行數(shù)) int tempNumber;//記過下邊的if else計算,得出真正所需的行數(shù)(真行數(shù)) float highUp = 0; if (temp % numberOfObjects == 0) { tempNumber = temp; } else//對10取余不為零,補一行 { tempNumber = temp + 1; } Debug.Log("總共有幾行" + tempNumber); //排列思路:(我的每個物體高度是200)第一行排在-200,然后每行依次+200,最后一行排在第一行下邊也就是-400,這樣開起來比較居中。因為排列太多行會看不清楚內(nèi)容,所以一般五六行就夠了,所以采用比較固(僵)定(硬)的排列方式,可以根據(jù)自己需求更改。 for (int i = 0; i < tempNumber; i++)//循環(huán)幾列 { if (i == tempNumber - 1)//最后一行Y坐標需要排在第一行的下邊(固定值,-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;//每行的每個點占圓周率的比例 print(angle); Vector3 pos = new Vector3(-Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;//對angle取余弦和正弦值再乘以半徑獲得當前物體在的坐標 this.transform.GetChild(j).position = new Vector3(pos.x, pos.y - 400, pos.z);//坐標賦值 } } } else { for (int j = (numberOfObjects * i); j < numberOfObjects * (i + 1); j++)//每行的開頭到當前行的末尾 { 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)整所有對象的朝向(每個物體都掛載)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class testSortUI : MonoBehaviour { private Transform centralPoint;//這個是圓的中心點 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法
這篇文章主要介紹了C#實現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法,很適合初學者更好的理解C#字符串原理,需要的朋友可以參考下2014-08-08

c# SQLHelper(for winForm)實現(xiàn)代碼
數(shù)據(jù)連接池c# SQLHelper 實現(xiàn)代碼
2009-02-02 
C#實現(xiàn)綁定DataGridView與TextBox之間關(guān)聯(lián)的方法
這篇文章主要介紹了C#實現(xiàn)綁定DataGridView與TextBox之間關(guān)聯(lián)的方法,涉及C#綁定控件關(guān)聯(lián)性的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
2015-08-08