背包系统(2)
显示物品的描述信息
用一个单例存放信息
usingUnityEngine;
usingSystem.Collections;
publicclassUIManager:MonoBehaviour{
public Gameobject panel;
private UIManager( ) { }
private static UIManager instance;
public static UIManager Instance{
get{
if(instance==null){
instance =new UIManager ( );
instance.panel =Gameobject.Find("Canvas").transform.FindChild("panel").gameobject;
}
return instance;
}
}
}
显示panel(//挂在预设体item上)
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
usingUnityEngine.EventSystems;
publicclassItemDescriptionScript:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler{
//private Gameobject panel;
public BaseScript goods;
voidStart( ){
// panel = transform.root.FindChild ("Panel").gameObject ;//item最底层的父物体(Canvas)和panel的父物体相同
}
voidUpdate( ){
}
#regionIPointerEnterHandlerimplementation
publicvoidOnPointerEnter(PointerEventDataeventData)
{
if(!UIManager.Instance.panel.activeSelf){
UIManager.Instance.panel.SetActive(true);
UIManager.Instance.panel.GetComponent<ShowDetalScript>.Goods=goods;
}
}
#endregion
#regionIPointerExitHandlerimplementation
publicvoidOnPointerExit(PointerEventDataeventData)
{
if(UIManager.Instance.panel.activeSelf){
UIManager.Instance.panel.SetActive(false);
}
}
#endregion
}
将物品加载到panel上(//描述,挂在Panel上)
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
public classShowDetalScript:MonoBehaviour{
private BaseScript goods;
public BaseScript Goods{
get{
returngoods;
}
set{
goods=value;
headImage.overrideSprite=Resources.Load("Texture/"+goods.iconName);
goodsName.text=goods.name;
description.text=goods.description;
}
}
private Image headImage;
private Text goodsName;
private Text description;
voidAwake( ){
headImage=transform.GetChild(0).gameObject.GetComponent<Image>( );
description=transform.GetChild(1).gameObject.GetComponent<Text>( );
goodsName=transform.GetChild(2).gameObject.GetComponent<Text>( );
}
voidUpdate( ){
}
}
翻页(挂载在Canvas上)
usingUnityEngine;
usingSystem.Collections;
publicclassNextPageScript:MonoBehaviour{
publicGameObjectcontent;
voidStart(){
}
voidUpdate(){
}
publicvoidButtonPressed( ){
content.transform.localPosition=newVector3(content.transform.localPosition.x,content.transform.localPosition.y+400f,0);
}
publicvoidButtonPressed_1( ){
content.transform.localPosition=newVector3(content.transform.localPosition.x,content.transform.localPosition.y-400f,0);
}
}