LoginSignup
0
1

More than 5 years have passed since last update.

Unity NGUIでUISprite等のMaterialのパラメータを動的に変更する方法

Posted at

動作環境

Unity5.5.0f3
NGUI 3.11.1

マテリアルパラメータ動的変更の方法

NGUIのUISprite、UITexture等でカスタムシェーダーを利用し、Materialのパラメータを動的に変更したい事があり、その方法を見つけたので忘備録として書いておく。

UIWidgetクラスにはUIDrawCall.OnRenderCallback onRenderというデリゲートがあり、これがOnWillRenderObjectの際に実行される。デリゲートは以下のように定義されているので、この引数で渡ってくるMaterialのパラメータを変更すればよい。

public delegate void OnRenderCallback (Material mat);

コード例を以下に示します。

Test.cs
public class Test : MonoBehaviour {
    void Awake()
    {
        var widget = GetComponent<UIWidget>();
        widget.onRender = SetMaterialParamCB;
    }

    void SetMaterialParamCB(Material mat)
    {
        mat.SetFloat("_xx",1.0f);
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1