2017-09-28 54 views
-1

我希望我的问题不重复; 我有一个统一的问题。 我写了一个透明的类(淡入/淡出循环)的对象。这是正确的。 我在场景中放了两个按钮,他们在画布的地方。 运行后,尽管我已经为画布中的透明对象写了一个命令,但里面的所有内容都变为透明(淡入和淡出循环)。 我不知道。 请帮忙。统一的透明3d

MyClass的

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using System.Threading; 
using System; 

public class Transparent : MonoBehaviour { 

private float duration = .7f; 
    public float waitTime; 
    IEnumerator co2; 
    // Update is called once per frame void 
    public void start_tranparecncy() 
    { 
     this.co2=this.blink(); 
     this.StartCoroutine (this.co2); 
    } 
    IEnumerator blink() { 

     //Color textureColor = this.transform.GetComponent<SpriteRenderer>().material.color; 
     Color textureColor = this.transform.GetComponent<Image>().material.color; 

     //textureColor.a = Mathf.PingPong(Time.time, duration)/duration; 
     //this.GetComponent<SpriteRenderer>().material.color = textureColor; 
     while (true) { // this could also be a condition indicating "alive or dead" 
      // we scale all axis, so they will have the same value, 
      // so we can work with a float instead of comparing vectors 
      textureColor.a=Mathf.PingPong (Time.time, duration)/duration; 
      this.transform.transform.GetComponent<Image>().material.color = textureColor; 

      // reset the timer 

      yield return new WaitForSeconds (waitTime); 



     } 
     //end of if(this.transform.childCount =0) 

    } 

    void stop_Transparency() 
    { 

     this.StopCoroutine (this.co2); 

    } 
} 

picture of my project

picture of my project

+0

请提供您的代码,如果可能的话,图片也 – Thalthanas

+0

目前还不清楚。请使用正确的命名约定。你可能是指函数而不是_command_?什么是_canvas place_?你的意思是屏幕空间 - 覆盖? “所有内部的东西都变得透明”,这不是你想要达到的目标吗? Unity中的透明度? – MrDos

+0

@EmreE哦,对不起 –

回答

0

好吧,我发现你的问题是主要的

this.transform.transform.GetComponent<Image>(); 

因为双变换,我t达到一个上层的父母,这是你的画布本身。所以你画布上的所有东西都会失去alpha值。

还编辑了一下你的代码。而不是使用材质颜色,直接更改了图像组件的Alpha颜色值。

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using System.Threading; 
using System; 

public class Transparent : MonoBehaviour 
{ 
    private float duration = .7f; 
    public float waitTime; 
    IEnumerator co2; 

    public void start_tranparecncy() 
    { 
     this.co2 = this.blink(); 
     this.StartCoroutine(this.co2); 
    } 

    IEnumerator blink() 
    { 
     //Color textureColor = this.transform.GetComponent<SpriteRenderer>().material.color; 
     Color textureColor = this.transform.GetComponent<Image>().color; 

     //textureColor.a = Mathf.PingPong(Time.time, duration)/duration; 
     //this.GetComponent<SpriteRenderer>().material.color = textureColor; 

     while (true) 
     { // this could also be a condition indicating "alive or dead" 
      // we scale all axis, so they will have the same value, 
      // so we can work with a float instead of comparing vectors 
      textureColor.a = Mathf.PingPong(Time.time, duration)/duration; 
      this.transform.GetComponent<Image>().color = textureColor; 

      // reset the timer 

      yield return new WaitForSeconds(waitTime); 
     } 
     //end of if(this.transform.childCount =0) 

    } 

    void stop_Transparency() 
    { 
     this.StopCoroutine(this.co2); 
    } 
} 

我已将此脚本附加到画布中的按钮。还可以在同一按钮的On Click()中运行start_tranparecncy()功能。

按下按钮后,只有按钮的alpha值正在改变。

如果您愿意,您可以更改On Click()并添加另一个按钮。

+0

我的朋友,我需要在开始场景中淡入/淡出...我运行start_transparency方法与脚本.......例如:throw_text_p1.gameObject。 GetComponent ().start_tranparecncy();命令。 –

+0

是的,这是....感谢你我的兄弟 –