当前位置:网站首页>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 可视化
边栏推荐
- MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
- Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
- HCIP之路
- 如何优雅的快速下载谷歌云盘的大文件 (二)
- Qt 使用QDomDocument读取xml文件
- How to tag and label naming before the project release
- Decoding and practice of cmaf Technology
- Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family
- Live broadcast review | how can the container transformation of traditional applications be fast and stable?
- C WPF additional attribute implementation interface defines decorator
猜你喜欢
![[game theory] basic knowledge](/img/eb/08b1ce5106e574dc42be58f72fbab9.jpg)
[game theory] basic knowledge

Principle of skip table

Chain tour airship development farmers' world chain tour development land chain tour development

How MySQL converts a date to a number
![[veusz] import 2D data in CSV](/img/22/467139f5a83ce9e88a57ced732d4d6.png)
[veusz] import 2D data in CSV

Vs problems when connecting to SQL myconn OPen(); cannot execute

在线文本过滤小于指定长度工具

3dmax插件开发环境配置及FileExport和Utilities模板测试

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

在kubernetes中部署kubersphere
随机推荐
YGG 西班牙 subDAO——Ola GG 正式成立
MySQL (IV) - MySQL storage engine
Both are hard disk partitions. What is the difference between C disk and D disk?
MySQL transaction isolation level
MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
Ldconfig command
[pit stepping record] a pit where the database connection is not closed and resources are released
279. perfect square
Ffplay realizes user-defined input stream playback
电脑如何安装MySQL?
three. Solution to stripe shadow and grid shadow in JS
leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs
Heterogeneous transaction scenario interaction process and consistency assurance
Heuristic search strategy
Tp6 installation expansion
干货来了|《PaaS》合辑抢先看~
Abnormal logic reasoning problem of Huawei software test written test
分布式ID生成
Principle of skip table
[Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?