155 lines
4.1 KiB
C#
155 lines
4.1 KiB
C#
using CriWare;
|
|
using CriWare.Assets;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
public class AnalyerTest : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
|
|
private CriAtomExOutputAnalyzer analyzer;
|
|
float playtime;
|
|
private List<float[]> list = new List<float[]>();
|
|
CriAtomSourceForAsset atomAsset;
|
|
public Action<List<float[]>> onPlay;
|
|
bool islist = false;
|
|
void Start()
|
|
{
|
|
analyzer = new CriAtomExOutputAnalyzer(NewConfig());
|
|
atomAsset = GameObject.Find("CriAtomSource").GetComponent<CriAtomSourceForAsset>();
|
|
atomAsset.AttachToAnalyzer(analyzer);
|
|
//playtime = atomAsset.time / 1000;
|
|
playtime = 5;
|
|
DebugUtil.Log("onPlay是否为空" + (onPlay == null));
|
|
}
|
|
public void SetOnPlay(Action<List<float[]>> action)
|
|
{
|
|
onPlay = action;
|
|
atomAsset.Play();
|
|
islist = true;
|
|
}
|
|
|
|
|
|
CriWare.CriAtomExOutputAnalyzer.Config NewConfig()
|
|
{
|
|
CriWare.CriAtomExOutputAnalyzer.Config config = new CriAtomExOutputAnalyzer.Config();
|
|
config.enableSpectrumAnalyzer = true;
|
|
config.numSpectrumAnalyzerBands = 16;
|
|
return config;
|
|
}
|
|
float[] levels = new float[16];
|
|
float nowtime;
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (!islist)
|
|
{
|
|
return;
|
|
}
|
|
if (nowtime <= playtime)
|
|
{
|
|
analyzer.GetSpectrumLevels(ref levels);
|
|
for (int i = 0; i < levels.Length; i++)
|
|
{
|
|
if (levels[i] * 100 > 10)
|
|
{
|
|
levels[i] = (2 + levels[i] * 100);
|
|
}
|
|
else
|
|
{
|
|
levels[i] = (2 - levels[i] * 100);
|
|
|
|
}
|
|
}
|
|
list.Add(levels);
|
|
nowtime += Time.deltaTime;
|
|
DebugUtil.Log(nowtime);
|
|
}
|
|
else
|
|
{
|
|
if (islist)
|
|
{
|
|
_SaveConfig(new OneMusicData(list));
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
public class OneMusicData
|
|
{
|
|
public string name;
|
|
public List<OneFrameData> frameDatas = new List<OneFrameData>();
|
|
public OneMusicData()
|
|
{
|
|
name = "fff";
|
|
|
|
for (int i = 0; i < 60; i++)
|
|
{
|
|
frameDatas.Add(new OneFrameData());
|
|
}
|
|
}
|
|
public OneMusicData(List<float[]> list)
|
|
{
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
frameDatas.Add(new OneFrameData(list[i]));
|
|
}
|
|
}
|
|
}
|
|
[Serializable]
|
|
|
|
public class OneFrameData
|
|
{
|
|
public List<int> ColData = new List<int>();
|
|
public OneFrameData()
|
|
{
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
ColData.Add(0);
|
|
}
|
|
}
|
|
public OneFrameData(float[] s)
|
|
{
|
|
for (int i = 0; i < s.Length; i++)
|
|
{
|
|
ColData.Add(Convert.ToInt16(s[i]));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private void _SaveConfig(OneMusicData saveData)
|
|
{
|
|
// 保存timeline
|
|
//var director = _cmp.gameObject.GetComponent<PlayableDirector>();
|
|
//var playAsset = director.playableAsset as TimelineAsset;
|
|
// 保存json到本地
|
|
{
|
|
var json = JsonUtility.ToJson(saveData);
|
|
var savePath = "Assets/Config/UISomeAbout/MainPanelMusicCube/";
|
|
var saveName = saveData + ".json";
|
|
var fullPath = savePath + "/" + saveName;
|
|
var realPath = Application.dataPath + fullPath.Substring(6);
|
|
if (File.Exists(realPath))
|
|
{
|
|
//if (VersionControlUtils.IsPathVersioned(fullPath))
|
|
//{
|
|
// Provider.Checkout(fullPath, CheckoutMode.Both).Wait();
|
|
//}
|
|
File.Delete(realPath);
|
|
}
|
|
File.WriteAllText(realPath, json);
|
|
Debug.Log("json保存成功:" + fullPath);
|
|
}
|
|
|
|
AssetDatabase.Refresh();
|
|
// 弹出对话框提示
|
|
EditorUtility.DisplayDialog("提示", "保存成功", "确定");
|
|
}
|
|
|
|
|
|
}
|