当前位置:网站首页>unity 音频可视化方案
unity 音频可视化方案
2022-06-23 07:03:00 【goose leaves a mark】
unity 音频组件自带提取频谱数据的方法:GetSpectrumData
于是复习一下ui布局:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Visualizer : MonoBehaviour
{
public AudioClip clip;
public RectTransform imagePrefab = null;
public FFTWindow windowType = FFTWindow.Rectangular;
[Space(10)]
public Transform labelPrefab;
public Transform labelRoot;
public int minHeight = 5;
public int maxHeight = 540;
public AudioSource audioSource;
[Space(20),Range(64,8192)]
public int dataLen = 64;
/** 显示的频带数 */
public int showLen = 80;
public RectTransform[] cubes = { };
// Start is called before the first frame update
void Start()
{
audioSource = new GameObject("Audio Source").AddComponent<AudioSource>();
if(!clip)
{
return;
}
audioSource.clip = clip;
audioSource.Play();
//vis = GetComponentsInChildren<VisualObject>();
initCubes();
}
private void initCubes() {
cubes = new RectTransform[showLen];
for(int i = 0; i < showLen; i++)
{
RectTransform cube = Instantiate(imagePrefab, transform);
cube.gameObject.SetActive(true);
cubes[i] = cube;
}
for (int i = 0; i < showLen; i++)
{
Transform cube = Instantiate(labelPrefab, labelRoot);
TextMeshProUGUI text = cube.GetComponent<TextMeshProUGUI>();
cube.gameObject.SetActive(true);
RectTransform t = cube.GetComponent<RectTransform>();
text.text = i + "";
text.fontSize = 5;
//t.sizeDelta = new Vector2();
}
}
// Update is called once per frame
void Update()
{
float[] spectrumDatas = new float[dataLen];
audioSource.GetSpectrumData(spectrumDatas,0,windowType);
for(int i = 0; i < cubes.Length; i++)
{
Rect rect = cubes[i].GetComponent<RectTransform>().rect;
Vector2 size = rect.size;
rect.y = Mathf.Lerp(size.y,minHeight + (maxHeight - minHeight) * spectrumDatas[i] * 2,0.1f);
Vector2 newSize = new Vector2(rect.x,rect.y);
cubes[i].GetComponent<RectTransform>().sizeDelta = newSize;
}
}
}
unity 可视化
边栏推荐
- How bootstrap clears floating styles
- HCIP之路第八次实验
- Sstable details
- Online text filter less than specified length tool
- [pit stepping record] a pit where the database connection is not closed and resources are released
- HCIP之路
- [veusz] import 2D data in CSV
- C WPF additional attribute implementation interface defines decorator
- NFS 特别注意权限的问题
- 'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1
猜你喜欢

1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task

Using the for loop to output an alphabetic triangle

Eureka服务注册与发现

小爱音箱连接网络异常解决办法

Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?

Sstable details

Deploy kubersphere in kubernetes

How to quickly and gracefully download large files from Google cloud disk (II)

Yolov5 detecting small targets (with source code)

Tp6+redis+think-queue+supervisor implements the process resident message queue /job task
随机推荐
What is distributed?
Eureka服务注册与发现
传智教育 | 多人协作开发出现代码冲突,如何合并代码?
Spock sub piling
Vs problems when connecting to SQL myconn OPen(); cannot execute
3DMAX plug-in development environment configuration and fileexport and utilities template testing
20bn Jester complete dataset Download
[AI practice] xgbgressor model accelerates training and uses GPU to train xgbgressor in seconds
Unity图片加载和保存
Spock constraint - call frequency / target / method parameters
Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family
Yolov5 detecting small targets (with source code)
利用for循环输出一个字母三角形
Download the OSS file and modify the file name
YGG 西班牙 subDAO——Ola GG 正式成立
Friends of the week
【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
【唠嗑篇】普通人到底该怎么学技术啊?
Difference between char and varchar
【Veusz】导入CSV中的二维数据