Unity3d 在NGUI中实现 图片的切换 求代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3d 在NGUI中实现 图片的切换 求代码相关的知识,希望对你有一定的参考价值。

场景中间有个sprite 两边分别是上一张 和下一张两个按钮 点击按钮 切换图片 图片的加载方式是本地加载就好

参考技术A 修改UISprite.spriteName

unity中实现三个Logo图片进行若隐若现的切换

public GameObject canvas;
private Transform logoParent;
private Transform Logo_logo;  //logo一
private Transform Logo_qmtgTrans;  //logo二
private Transform Logo_brand;  //logo三
private float Timer;  //计时器
private float time = 5f;  //延迟的总时长
private float Alpha = 1f;
private float AlphaSpeed = 3f;  //Alpha值渐变的时间,1就是对应渐变需要1秒,2就是2秒
DisPlayState disPlayState = DisPlayState.start;
public void Start(GameObject cav)
{
canvas = cav;
disPlayState = DisPlayState.start;
var logoParentPrafab = Resources.Load<Transform>("UIPrefabs/LogoParent");
logoParent = GameObject.Instantiate(logoParentPrafab);
logoParent.SetParent(canvas.transform, false);
Logo_logo = logoParent.transform.Find("Logo_logo");
Logo_qmtgTrans = logoParent.transform.Find("Logo_qmtg");
Logo_brand = logoParent.transform.Find("Logo_brand");
}

public void Update()
{
LogoDisPlay();
}
//Logo显示状态切换
private void LogoDisPlay()
{
if (disPlayState == DisPlayState.start)
{
Timer = time;
disPlayState = DisPlayState.delay;
}
if (disPlayState == DisPlayState.delay)
{
DelayTime(DisPlayState.minusAlpha);
}
if (disPlayState == DisPlayState.minusAlpha)
{
if (Logo_logo != null)
AlphaControl(true, DisPlayState.addAlpha, Logo_logo.gameObject);
}
if (disPlayState == DisPlayState.addAlpha)
{
if (Logo_qmtgTrans != null)
AlphaControl(false, DisPlayState.delay2, Logo_qmtgTrans.gameObject);
}
if (disPlayState == DisPlayState.delay2)
{
DelayTime(DisPlayState.minusAlpha2);
}
if (disPlayState == DisPlayState.minusAlpha2)
{
if (Logo_qmtgTrans != null)
AlphaControl(true, DisPlayState.addAlpha2, Logo_qmtgTrans.gameObject);
}
if (disPlayState == DisPlayState.addAlpha2)
{
if (Logo_brand != null)
AlphaControl(false, DisPlayState.delay3, Logo_brand.gameObject);
}
if (disPlayState == DisPlayState.delay3)
{
DelayTime(DisPlayState.minusAlpha3);
}
if (disPlayState == DisPlayState.minusAlpha3)
{
if (Logo_brand != null)
AlphaControl(true, DisPlayState.start, Logo_brand.gameObject);
}
}
//控制Alpha值减小和增加
private void AlphaControl(bool minus, DisPlayState nextState, GameObject _logo)
{
if (minus)
{
Alpha -= Time.deltaTime * 1 / AlphaSpeed;
if (Alpha <= 0)
{
disPlayState = nextState;
//if (_logo.name == "Logo_logo")
//{
// Logo_logo.gameObject.SetActive(true);
// Logo_qmtgTrans.gameObject.SetActive(false);
// Logo_brand.gameObject.SetActive(false);
//}
//else if (_logo.name == "Logo_qmtg")
//{
// Logo_logo.gameObject.SetActive(false);
// Logo_qmtgTrans.gameObject.SetActive(true);
// Logo_brand.gameObject.SetActive(false);
//}
//else if (_logo.name == "Logo_brand")
//{
// Logo_logo.gameObject.SetActive(false);
// Logo_qmtgTrans.gameObject.SetActive(false);
// Logo_brand.gameObject.SetActive(true);
//}
//else
//{
// Debug.LogError("Logo消失了");
//}
}
_logo.GetComponent<RawImage>().color = new Color(1, 1, 1, Alpha);
}
else
{
Alpha += Time.deltaTime * 1 / AlphaSpeed;
if (Alpha >= 1)
{
disPlayState = nextState;
}
_logo.GetComponent<RawImage>().color = new Color(1, 1, 1, Alpha);
}
}
//延时并切换状态
private void DelayTime(DisPlayState _state)
{
Timer -= Time.deltaTime;
if (Timer <= 0)
{
disPlayState = _state;
Timer = time;
}
}
//显示Logo的各种状态枚举
private enum DisPlayState
{
start,
delay,
minusAlpha,
addAlpha,
delay2,
minusAlpha2,
addAlpha2,
delay3,
minusAlpha3,
end
}

以上是关于Unity3d 在NGUI中实现 图片的切换 求代码的主要内容,如果未能解决你的问题,请参考以下文章

在Unity3D中实现高效的战斗飘字

在Unity3D中实现高效的战斗飘字

如何在Unity3d开发中实现文字的渐隐效果

Unity3D日常开发Unity3D中实现向Web服务器上传图片以及下载图片功能

unity3d、ngui插件做界面图片模糊

Unity3D日常开发Unity3D中实现向Web服务器上传图片以及下载图片功能