【问题标题】:Change width parameter of Trail Renderer with audio spectrum C# Script in Unity在 Unity 中使用音频频谱 C# 脚本更改 Trail Renderer 的宽度参数
【发布时间】:2017-12-12 03:24:37
【问题描述】:

我正在尝试找到一种方法,使用音频文件中的音频频谱动态更改轨迹渲染器的宽度参数。我已经成功地改变了游戏对象的比例,但不能改变轨迹渲染器本身的实际宽度参数。这是用于动态更改使用音频频谱的比例的代码。任何帮助或建议将不胜感激!谢谢

使用音频频谱更改比例的代码:

using UnityEngine;
using System.Collections;

public class trailTest : MonoBehaviour {
public float width = 5.0f;
public bool useCurve = true;
private TrailRenderer tr;
public int numberOfObjects = 1;


void Start ()
{
    tr = GetComponent<TrailRenderer> ();
    tr.material = new Material (Shader.Find ("Sprites/Default"));
}

void Update ()
{
    AnimationCurve curve = new AnimationCurve ();
    if (useCurve) {
        curve.AddKey (0.0f, 0.0f);
        curve.AddKey (1.0f, 1.0f);
    } else {
        curve.AddKey (0.0f, 1.0f);
        curve.AddKey (1.0f, 1.0f);
    }

    float [] spectrum = AudioListener.GetSpectrumData (1024, 0, FFTWindow.Hamming);
    for (int i = 0; i < numberOfObjects; i++) {
        Vector3 previousScale = tr.transform.localScale;
        previousScale.y = Mathf.Lerp (previousScale.y, spectrum [i] * 80, Time.deltaTime * 30);
        tr.transform.localScale = previousScale;

      }
   }
 }

【问题讨论】:

  • 修改tr.width不是tr.transform....
  • 我可以使用参数startWidthendWidth
  • 这就是我的意思。 XXXWidth 属性。
  • 我现在收到错误Type float does not contain a definition for localScale and no extension method localScale of type float could be found. Are you missing an assembly reference? C# 脚本的新手,所以我正在学习如何调试。感谢您的帮助!

标签: c# unity3d audio


【解决方案1】:

已解决:

using UnityEngine;
using System.Collections;

public class trailTest : MonoBehaviour
{
    private TrailRenderer trailRenderer;
    public GameObject [] trailerbaby;
    public int numberOfObjects = 1;
    void Start ()
    {
        trailRenderer = gameObject.GetComponent<TrailRenderer> ();
        float startWidth = trailRenderer.startWidth;
        float endWidth = trailRenderer.endWidth;
        trailRenderer.startWidth = 1f;
        trailRenderer.endWidth = 1f;
        Debug.Log ("Start width: " + startWidth + '\t' + "End width: " + endWidth);

        trailerbaby = GameObject.FindGameObjectsWithTag ("trailerbaby");

    }

    void Update ()
    {
        trailRenderer = gameObject.GetComponent<TrailRenderer> ();
        float startWidth = trailRenderer.startWidth;
        float endWidth = trailRenderer.endWidth;
        Debug.Log ("Start width: " + startWidth + '\t' + "End width: " + endWidth);
        float [] spectrum = AudioListener.GetSpectrumData (1024, 0, FFTWindow.Hamming);
        for (int i = 0; i < numberOfObjects; i++) {
            //Vector3 previousPosition = trailerbaby [i].transform.localPosition;
            trailRenderer.startWidth = Mathf.Lerp (trailRenderer.startWidth, spectrum [i] * 2000, Time.deltaTime * 13000 * 10);
            trailRenderer.endWidth = Mathf.Lerp (trailRenderer.startWidth, spectrum [i] * 2000, Time.deltaTime * 13000 * 10);
            //previousPosition.y = Mathf.Lerp (previousPosition.y, spectrum [i] * 2000, Time.deltaTime * 13000);
            //trailerbaby [i].transform.localPosition = previousPosition;

        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多