【Unity】 DoTween对UI进行DoFade操作存在问题及解决办法

Unity DoTween对UI进行DoFade操作存在问题

Unity版本:5.2, 5.4

当使用this.GetComponent<Image>().material.DOFade(0, 2).SetEase(Ease.InBounce);来对UGUI的Image进行褪色操作的时候本质是对UI的Graphic对象(Text,Image等都为Graphic的子类)的material进行操作,下例是对Text组件进行褪色操作:Transform.GetComponent().material.DoFade(0,1)。虽然脚本只挂在一个Text组件的物体上,但1秒之后发现,整个UI界面全部变为透明。
(我也很纳闷,cube01.GetComponent<Renderer> ().material.color = Color.black;这样的代码照理说是这样执行的(http://www.jianshu.com/p/ababf547d992):

Material lastMat = cube01.GetComponent<Renderer> ().material;
Material m = Instantiate(lastMat) as Material;
cube01.GetComponent<Renderer> ().material = m;
m.color = Color.black;

应该是最自己持有的material进行操做。。。

经测试发现,所有使用缺省material的组件都是使用的默认的material,而这个material只存在一份,所有UI组件使用的默认material都只是该material的引用,在DoTween对其进行褪色操作之后,该material的alpha值保持为0不变,且游戏重新开始也不会将其alpha值重置为1。

解决办法:

  • 导入DoTween后请确保Setup DoTween, Tools/DoTween Utility Panel/Setup DoTween…。导入后就可以使用Image.DoFade了。
  • 使用Unity自带的Graphic.CrossFadeAlpha(float alpha, float duration, bool ignoreTimeScale)函数来操作
  • 自己扩展DoTween的方法,下面是我扩展的一个例子,可以参考DoTwen官网的Creating custom plugins example
////
////  DoFadeTest.cs
////  Project: GUITest
////
////  Created by zhiheng.shao
////  Copyright  2016年 zhiheng.shao. All rights reserved.
////
////  Description

using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;
using DG.Tweening.RickExtension;

public class DoFadeTest : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        this.GetComponent<Image>().DOFade(0, 2).SetEase(Ease.InBounce);
    }
}

namespace DG.Tweening.RickExtension
{
    public static class DOTweenExteion
    {
        public static Tweener DOFade(this Image image, float endValue, float duration)
        {
            Debug.Log("CustomDoFade");
            return DOTween.To(image.AlphaGetter, image.AlphaSetter, endValue, duration);
        }
        private static float AlphaGetter(this Image image)
        {
            return image.color.a;
        }

        private static void AlphaSetter(this Image image, float alpha)
        {
            Color oldColor = image.color;
            oldColor.a = alpha;
            image.color = oldColor;
        }
    }
}
附:DoTween官网
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值