Unity | スクリプトからImageコンポーネントのColorを変更

ImageコンポーネントのColor変更例です。

スポンサーリンク

Color変更の例

using UnityEngine.UI;
namespace BlueBreath.Practice
{
    [RequireComponent(typeof(Image))]
    public class Demo : MonoBehaviour
    {
        float r,g,b,a = 1f;
        private Image image = null;
        private void Awake(){
            this.TryGetComponent(out image);
        }
        public void SetColor()
        {
            r = 1f;
            g = 0.5f;
            b = 0.5f;
            this.image.color = new Color(r,g,b,a);
        }
 
    }
}

反映されない場合

Inspectorに反映されて、実際の画像には反映されない

誤:0f ~ 255fの範囲で代入している

this.image.color = new Color(255f, 255f, 255f, 255f);

正:0f ~ 1fの間で代入する

this.image.color = new Color(1f, 1f, 1f, 1f);

関連・参考

単色変更するための拡張

Imageコンポーネントの色をそれぞれ変更したい場合に使用できる拡張です。

参考

Color

(多分また忘れた頃に255基準で代入してしまう…。)

コメント

タイトルとURLをコピーしました