質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

235閲覧

Imegeの写真を徐々に透明にしたい

退会済みユーザー

退会済みユーザー

総合スコア0

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2021/01/25 12:20

前提・実現したいこと

Imegeの写真を徐々に透明にしたい

発生している問題・エラーメッセージ

なぜか急にに写真が消える

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using DG.Tweening; 6 7public class canvas : MonoBehaviour 8{ 9 10 private RectTransform rectTransform; 11 12 private GameObject imageObject; 13 14 private Image imageComponent; 15 16 private Text Scoretext; 17 18 private RectTransform fadeRectT; 19 20 private GameObject fadeObject; 21 22 public GameObject textObject; 23 24 public int Score = 0; 25 26 public float speed = 0.01f; 27 28 float color1; 29 30 bool flag; 31 32 33 34 // Start is called before the first frame update 35 void Start() 36 { 37 imageObject = GameObject.Find("Image"); 38 imageComponent = imageObject.GetComponent<Image>(); 39 imageComponent.enabled = false;          //宣言 40 41 42 textObject = GameObject.Find("Text"); 43 Scoretext = textObject.GetComponent<Text>(); 44 Scoretext.text = "000000"; 45 46 47 fadeObject = GameObject.Find("fade"); 48 fadeRectT = fadeObject.GetComponent<RectTransform>(); 49 50 color1 = 0; 51 52 53 fadeIn(); 54 } 55 56 void displayTelop( string telopName )//表示するところ 57 { 58 imageComponent.color = new Color(1, 1, 1, 1); 59 60 imageComponent.enabled = true; 61 62 Texture2D telopImage = Resources.Load(telopName) as Texture2D; 63 64 imageComponent.sprite = Sprite.Create(telopImage, 65 new Rect(0, 0, telopImage.width, telopImage.height), 66 Vector2.zero); 67 68 imageComponent.SetNativeSize(); 69 70 71 } 72 73 void Image1( string telopName )//消すところ 74 { 75 flag = true; 76 77 color1 = 1; 78 79 80 81 while (flag) 82 { 83 GetComponent<Image>().color = new Color(1f, 1f, 1f, color1); 84 85 color1 -= Time.deltaTime; 86 87 if(color1 < 0) 88 { 89 color1 = 0; 90 flag = false; 91 } 92 93 } 94 95 imageComponent.enabled = false; 96 97 }


これを、SendMessageで呼び出している

canvas.SendMessage("displayTelop", "clear"); ↑ 表示する方 canvas.SendMessage("Image1"); ↑ 消す方

試したこと

インスペクターのColorのRGB 0-255 RGB 0-1

Unity 2020.2.1f1 Visual Studio 2019

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

while (flag) { GetComponent<Image>().color = new Color(1f, 1f, 1f, color1); color1 -= Time.deltaTime; if(color1 < 0) { color1 = 0; flag = false; } }

上記のように、ループ内でアルファ値を変更していくと
1フレームで1ー>0まで値が遷移し、Imageの描画に反映されるアルファ値は変更前の"1"と、変更後の"0"のみになると思います。
1フレーム毎に、少しずつ値を変化させたい場合は、while文なので繰り返し処理するのではなく、まUpdate()やFixedUpdate()を使えばいいと思います。

投稿2021/01/26 00:31

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/01/26 08:06

なるほど! Update()に入れたら直りました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問