UE4 Change MaterialInstance's Parent At Runtime

最近一個需求,需要在Runtime下 更改一個MaterialInstance的Parent材質,並且將修改後的MaterialInstance應用到所有用到該MaterialInstance的對象上。

聽到這個需求 第一反應還是遍歷場景所有Actor,當然 遍歷不太好, 於是就開始翻源碼,找到MaterialInstance的一個protected函數void SetParentInternal(class UMaterialInterface* NewParent, bool RecacheShaders); 然後根據這個函數折騰了一下子沒有達到我的需求,於是 又開始讀源碼, 終於找到了一條捷徑!

三行代碼搞定:

    MatInst->Parent = OtherMatInst->Parent;
    FMaterialUpdateContext MaterialUpdateContext;
    MaterialUpdateContext.AddMaterialInstance(MatInst);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章