<pre name="code" class="csharp">using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ExpSlider : MonoBehaviour
{
private bool DisplayText { get; set; }
private UISlider mExpSlider;
private UILabel mExp_lbl_text;
private float m_fCurExp = 0f;//当前经验
private float m_fNextExp = 0f;//升级经验
private float m_fTempExp = 0f;
private float m_fTempAddExp = 0f;
private float m_fSliderValueSpeed = 0.02f;//Slider值增长的速度
private float m_fTempAddExpdif = 10;//Label经验值位数
void Start()
{
//InitExpSlider();//初始化经验条相关UI UISlider UILabel
//InitExpInfo();//初始化经验条数值 CurExp NextExp
}
void Update()
{
UpdateExp();
}
public void InitExpSlider(UISlider ExpSlider, UILabel Exp_lbl)
{
DisplayText = true;
if (ExpSlider)
{
mExpSlider = ExpSlider;
mExpSlider.sliderValue = 0;
}
if (Exp_lbl)
{
mExp_lbl_text = Exp_lbl;
}
}
public void InitExpInfo(float fCurExp,float fNextExp)
{
m_fCurExp = fCurExp;//当前经验
m_fNextExp = fNextExp;//
if (CompuletCount((int)m_fCurExp) >= 5)
m_fTempAddExpdif = Mathf.Pow(10, CompuletCount((int)m_fCurExp) - 3);//为控制经验其增长速度(十百千万)
else if (CompuletCount((int)m_fCurExp) > 2)
m_fTempAddExpdif = Mathf.Pow(10, CompuletCount((int)m_fCurExp) - 2);
m_fTempAddExp = m_fCurExp / (int)m_fTempAddExpdif;
ReSetSliderSteps();
}
private int CompuletCount(int num)//计算当前经验值位数
{
int i = 1;
int count = 0;
do
{
++count;
i *= 10;
} while (i < num);
return count;
}
private void UpdateExpLabel(float exp)//经验数值显示
{
if (mExpSlider == null || exp <= 0)
return;
if (exp > m_fCurExp)
exp = m_fCurExp;
if (DisplayText)
mExp_lbl_text.text = string.Format("{0}", (int)exp);//当前经验
//mExp_lbl_text.text = string.Format("{0}/{1}", (int)exp, (int)m_fNextExp);
else
mExp_lbl_text.text = "";
}
private void UpdateExpSlider()//经验条动态显示
{
if (mExpSlider == null)
return;
if (mExpSlider.sliderValue >= m_fCurExp / m_fNextExp)
return;
mExpSlider.sliderValue += m_fSliderValueSpeed;
// if (mExpSlider.sliderValue >= 1)
// mExpSlider.sliderValue = 0;
}
private void ReSetSliderSteps()
{
mExpSlider.numberOfSteps = (int)m_fNextExp;
if (m_fCurExp >= 0)
mExpSlider.sliderValue = 0;
}
public void UpdateExp()
{
if (null != mExpSlider)
{
UpdateExpSlider();
if (null != mExp_lbl_text)
{
if (m_fTempExp <= m_fCurExp)
{
m_fTempExp += m_fTempAddExp;
UpdateExpLabel(m_fTempExp);
}
}
}
}
}