2012年4月1日 星期日

自訂聲音節點

UE的SoundCue提供混音、變調、衰減等多種音效功能,它可以讓開發者自訂節點來擴充新的功能。

聲音節點的基礎類別叫做SoundNode,它有一個陣列存放下一層子節點。多次播放同一個SoundCue並不會產生多份SoundNode來播放,所以像SoundNodeLooping這種需要額外記錄目前播放次數的情況,會把額外資料放在AudioComponent::SoundNodeData裡,它是一個位元陣列,由 AudioComponent::SoundNodeOffsetMap記錄不同SoundNode資料在該位元陣列的起始位置。為了方便寫碼,UE提供以下三個巨集:
  • RETRIEVE_SOUNDNODE_PAYLOAD(size):用來宣告此SoundNode使用的資料大小。
  • DECLARE_SOUNDNODE_ELEMENT(type, name):用來宣告在位元陣列裡的變數。
  • DECLARE_SOUNDNODE_ELEMENT_PTR(type, name):用來宣告在位元陣列裡的指標變數。

範例


以下程式碼展示如何自訂一個切換型的聲音節點:

class MySoundNodeSwitch extends SoundNode
    native( Sound )
    hidecategories( Object )
    editinlinenew;

var() int ActiveChildIndex;

cpptext
{
    virtual void ParsetNodes( UAudioDevice* AudioDevice, USoundNode* Parent, INT ChildIndex, class UAudioComponent* AudioComponent, TArray<FWaveInstance*>& WaveInstances )
    {
        if( ChildNodes.IsValidIndex(ActiveChildIndex) &&  ChildNodes(ActiveChildIndex) )
        {
             ChildNodes(ActiveChildIndex)->ParseNodes( AudioDevice, this, ActiveChildIndex, AudioComponent, WaveInstances );
        }
    }

    virtual INT GetMaxChildNodes()
    {
        return -1;
    }

    virtual INT RemoveChildNodes( INT Index )
    {
        Super::RemoveChildNodes( Index );

        if(! ChildNodes.IsValidIndex(ActiveChildIndex))
        {
            ActiveChildIndex = 0;
        }
    }

    virtual FString GetUniqueString()
    {
        FString Unique = FString::Printf( TEXT("Switch %d/" ), ActiveChildIndex );
        return Unique;
    }
}

沒有留言:

張貼留言