IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原](二十九)unity4.6学习Ugui中文文档-------运行时创建Scroll-lists

    u010019717发表于 2015-05-11 09:04:48
    love 0
    


    孙广东 2015.5.10

    首先要滚动和list形式存储内容。 使用Scroll Rect组件。就可以滚动了,但是有时候我们不希望,item超过区域还显示:就要使用Mask组件,说明一下,Mask组件组好配合Image一起使用。

    下面的图:显示了的情况【头像是子对象Image】。1、父对象只有Mask组件;2、有Mask和Image组件【图片为none】;3、有Mask和Image组件【图片有Alpha透明区域】

    在list容器中的item 我们怎么管理呢?VerticalLayout Group组件来实现自动的布局【其中可以设置每个item间的间距等】。还没完呢,每个item都要添加:Layout Element组件【可以设置每个item的最想要的宽度和高度等设置。】

    还没完,我们要做的是在运行会list容器中动态的添加内容,所以我们希望的是:每次添加从尾部添加,如果item很少的,也要上对其【list容器设置为】。

    这样我们就在为list容器添加ContextSizeFitter 组件,有什么用?

    从下面的对应关系应该就明白了!

    强调一下: 使用ALT和Shift 键是不同的呦! 这个选择很重要,会节省很多人为的设置!

    涉及到的代码:

    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;
    using System.Collections;
    using System.Collections.Generic;
    
    [System.Serializable]
    public class Item {
    	public string name;
    	public Sprite icon;
    	public string type;
    	public string rarity;
    	public bool isChampion;
    	public Button.ButtonClickedEvent thingToDo;
    }
    
    public class CreateScrollList : MonoBehaviour {
    	
    	public GameObject sampleButton;
    	public List itemList;
    	
    	public Transform contentPanel;
    	
    	void Start () {
    		PopulateList ();
    	}
    	
    	void PopulateList () {
    		foreach (var item in itemList) {
    			GameObject newButton = Instantiate (sampleButton) as GameObject;
    			SampleButton button = newButton.GetComponent  ();
    			button.nameLabel.text = item.name;
    			button.icon.sprite = item.icon;
    			button.typeLabel.text = item.type;
    			button.rarityLabel.text = item.rarity;
    			button.championIcon.SetActive (item.isChampion);
    			//          button.button.onClick = item.thingToDo;
    			newButton.transform.SetParent (contentPanel);
    		}
    	}
    
    
    	/// 
    	/// 这两个函数 通过Inspector面板 赋值给 thingToDo变量就OK
    	/// 
    	public void SomethingToDo () {
    		Debug.Log ("I done did something!");
    	}
    	
    	public void SomethingElseToDo (GameObject item) {
    		Debug.Log (item.name);
    	}
    }

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class SampleButton : MonoBehaviour {
    	
    	public Button button;
    	public Text nameLabel;
    	public Image icon;
    	public Text typeLabel;
    	public Text rarityLabel;
    	public GameObject championIcon;
    }

    using UnityEngine;
    using System.Collections;
    
    public class StandaloneScriptExample : MonoBehaviour {
    	// 这两个函数 通过Inspector面板 赋值给 thingToDo变量就OK
    	public void DoANewThing () {
    		Debug.Log ("Stand alone script example!");
    	}
    }





沪ICP备19023445号-2号
友情链接