【问题标题】:Trancparency in unity 3dunity 3d 中的透明度
【发布时间】:2017-09-28 11:27:15
【问题描述】:

我希望我的问题没有重复; 我的团结有问题。 我为对象的透明度(淡入/淡出循环)编写了一个类。它工作正常。 我在场景中放了两个按钮。它们在画布位置。 运行后,即使我在画布中编写了一个透明对象的命令,里面的所有东西都进入透明状态(淡入淡出循环)。 我不知道。 请帮忙。

我的班级

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);

    }
}

【问题讨论】:

  • 请提供您的代码,如果可能还有图片
  • 不清楚。请使用正确的命名约定。您可能是指函数而不是 command?什么是画布位置?你的意思是屏幕空间 - 覆盖? '里面的所有东西都变得透明'这不是你想要达到的吗? Unity 中的透明度?
  • @EmreE 哦,对不起
  • @MrDos 是的兄弟
  • 您将此脚本附加到何处?在画布本身或画布的子对象上?

标签: unity3d


【解决方案1】:

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

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

由于双重变换,它到达了父级的上一层,即您的画布本身。所以你画布中的所有东西都会失去 alpha 值。

还稍微编辑了您的代码。不使用材质颜色,而是直接改变Image Component的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() 并添加另一个按钮。

【讨论】:

  • 我的朋友我需要在开始场景中运行淡入/淡出...我用脚本运行 start_transparency 方法......例如:throw_text_p1.gameObject.GetComponent ( ).start_tranparecency ();命令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多