代码大体是这样的:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonWeapSet : MonoBehaviour
{
private WEAPON_SET_TYPE m_eWeaponSet;//本按钮对应的.
private Image m_IconMain;
private Image m_IconOff;
private Image m_HighLight;
private float m_fHighLightBlinkDuration = 2.0f;
private float m_fHighLightTimer = 0;
public void SetWeaponSet( WEAPON_SET_TYPE eWeaponSet )
{
m_eWeaponSet = eWeaponSet;
}
public void SetIconSprite( Sprite spriteMain, Sprite spriteOff=null )
{
if( spriteMain != null && m_IconMain != null )
m_IconMain.overrideSprite = spriteMain;
if( spriteOff != null && m_IconOff != null )
m_IconOff.overrideSprite = spriteOff;
}
public void SetButtonEnable( bool bEnabled=true )
{
gameObject.SetActive( bEnabled );
}
public void SetHighLight( bool bEnabled=true )
{
if( m_HighLight != null )
m_HighLight.enabled = bEnabled;
}
void Start()
{
Button compBtn = gameObject.GetComponent<Button>();
compBtn.onClick.AddListener( OnButtonClick );
Image[] aImages = gameObject.GetComponentsInChildren<Image>();
if( aImages != null && aImages.Length > 0 )
{
for( int i = 0; i < aImages.Length; i++ )
{
if( aImages[i].name.CompareTo( "main" ) == 0 )
m_IconMain = aImages[i];
else if( aImages[i].name.CompareTo( "off" ) == 0 )
m_IconOff = aImages[i];
else if( aImages[i].name.CompareTo( "highlit" ) == 0 )
m_HighLight = aImages[i];
}
}
Debug.Log( "OnStart" );
}
private void OnButtonClick()
{
Debug.Log( "OnButtonClick" );
m_HighLight.enabled = true;
CharacterInterface character = CombatLogic.s_Singleton.GetCurrentActiveCharacter();
if( character != null )
character.SetActiveWeaponSet( m_eWeaponSet );
}
}
运行时没有抱任何警告或者错误,可是上面那个void Start()函数就是没有被调用。
而如果我把那个函数名改成void Awake()就会被调用了。这个很是奇怪,因为相同的工程里面之前已经做过很多类似的UI按钮了,没有出过这样的问题。求教大神们这个是什么情况啊?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonWeapSet : MonoBehaviour
{
private WEAPON_SET_TYPE m_eWeaponSet;//本按钮对应的.
private Image m_IconMain;
private Image m_IconOff;
private Image m_HighLight;
private float m_fHighLightBlinkDuration = 2.0f;
private float m_fHighLightTimer = 0;
public void SetWeaponSet( WEAPON_SET_TYPE eWeaponSet )
{
m_eWeaponSet = eWeaponSet;
}
public void SetIconSprite( Sprite spriteMain, Sprite spriteOff=null )
{
if( spriteMain != null && m_IconMain != null )
m_IconMain.overrideSprite = spriteMain;
if( spriteOff != null && m_IconOff != null )
m_IconOff.overrideSprite = spriteOff;
}
public void SetButtonEnable( bool bEnabled=true )
{
gameObject.SetActive( bEnabled );
}
public void SetHighLight( bool bEnabled=true )
{
if( m_HighLight != null )
m_HighLight.enabled = bEnabled;
}
void Start()
{
Button compBtn = gameObject.GetComponent<Button>();
compBtn.onClick.AddListener( OnButtonClick );
Image[] aImages = gameObject.GetComponentsInChildren<Image>();
if( aImages != null && aImages.Length > 0 )
{
for( int i = 0; i < aImages.Length; i++ )
{
if( aImages[i].name.CompareTo( "main" ) == 0 )
m_IconMain = aImages[i];
else if( aImages[i].name.CompareTo( "off" ) == 0 )
m_IconOff = aImages[i];
else if( aImages[i].name.CompareTo( "highlit" ) == 0 )
m_HighLight = aImages[i];
}
}
Debug.Log( "OnStart" );
}
private void OnButtonClick()
{
Debug.Log( "OnButtonClick" );
m_HighLight.enabled = true;
CharacterInterface character = CombatLogic.s_Singleton.GetCurrentActiveCharacter();
if( character != null )
character.SetActiveWeaponSet( m_eWeaponSet );
}
}
运行时没有抱任何警告或者错误,可是上面那个void Start()函数就是没有被调用。
而如果我把那个函数名改成void Awake()就会被调用了。这个很是奇怪,因为相同的工程里面之前已经做过很多类似的UI按钮了,没有出过这样的问题。求教大神们这个是什么情况啊?