三種布局
1. Horizontal Layout Group水平布局
2. Vertical Layout Group垂直布局
3. Grid Layout Group網(wǎng)格布局
Scroll View控件
Scroll Rect組件
實例:
實現(xiàn)點(diǎn)擊button按鈕,獲取到對應(yīng)的下拉列表,在點(diǎn)擊一次會清除列表,列表內(nèi)的各項數(shù)據(jù)都是經(jīng)過獲取對應(yīng)的值
mainUI類
public class mainUI : MonoBehaviour {? ??
GameObject _serverItem;? ?
?Transform _parent;? ?
?Button _button;??
? ListitemGroup = new List();? ?
?//數(shù)據(jù)(實際開發(fā)中數(shù)據(jù)從服務(wù)器獲取)
? ? ListinfoGroup = new List();? ?
?void CreatTemDate()? ? {??
? ? ? infoGroup.Clear();? ??
? ? Rankinginfo _tempRInfo = new Rankinginfo();? ??
? ? _tempRInfo._id = 100;
? ? ? ? _tempRInfo._name = "張三豐";? ?
?? ? _tempRInfo._lv = 80;? ? ? ?
?_tempRInfo._iconRes = "main_btn_gohome01";??
? ? ? _tempRInfo._job = "狂戰(zhàn)士";?
?? ? ? infoGroup.Add(_tempRInfo);? ??
? ? Rankinginfo _tempRInfo_a = new Rankinginfo();? ? ?
?? _tempRInfo_a._id = 101;? ? ?
?? _tempRInfo_a._name = "李連杰";??
? ? ? _tempRInfo_a._lv = 70;? ? ?
?? _tempRInfo_a._iconRes = "main_btn_next02";?
?? ? ? _tempRInfo_a._job = "魔法師";? ??
? ? infoGroup.Add(_tempRInfo_a);?
?? ? ? Rankinginfo _tempRInfo_b = new Rankinginfo();? ?
?? ? _tempRInfo_b._id = 102;??
? ? ? _tempRInfo_b._name = "甄子丹";? ?
?? ? _tempRInfo_b._lv = 60;? ? ? ?
?_tempRInfo_b._iconRes = "main_btn_rock_1";? ??
? ? _tempRInfo_b._job = "弓箭手";? ? ?
?? infoGroup.Add(_tempRInfo_b);??
? }? ??
void Awake()? ? {? ?
?? ? CreatTemDate();? ? ? ?
?_serverItem = transform.Find("Scroll View/item").gameObject;? ? ? ?
?_parent = transform.Find("Scroll View/Viewport/Content");? ?
?? ? _button = transform.Find("Button").GetComponent();? ?
?? ? _button.onClick.AddListener(ButtonFun);?
?? }? ??
bool isshow = false;?
?? void ButtonFun()? ? {? ?
?? ? if (!isshow)? ? ? ? {? ? ??
? ? ? CreatItem();? ? ? ? ?
?? isshow = true;? ?
?? ? }? ? ? ?
?else? ? ??
? {? ? ? ? ? ??
for (int i = 0; i < itemGroup.Count; i++)? ? ? ??
? ? {? ? ? ? ? ? ? ?
?Destroy(itemGroup[i]);? ??
? ? ? ? }? ? ? ? ? ??
isshow = false;? ??
? ? }? ??
}? ? ??
? //根據(jù)數(shù)據(jù)創(chuàng)建UI??
? GameObject _tempItem;??
? void CreatItem()? ? {? ??
? ? if (infoGroup.Count > 0)? ??
? ? {? ? ? ? ? ?
?for (int i = 0; i < infoGroup.Count; i++)? ? ?
?? ? ? {? ? ? ? ? ? ??
? _tempItem = Instantiate(_serverItem) as GameObject;??
? ? ? ? ? ? ? _tempItem.transform.parent = _parent;? ? ? ? ??
? ? ? _tempItem.transform.localPosition = Vector3.zero;??
? ? ? ? ? ? ? _tempItem.transform.localScale = Vector3.one;? ?
?? ? ? ? ? ? _tempItem.transform.localRotation = new Quaternion();? ? ??
? ? ? ? ? severitem _serItem=_tempItem.AddComponent();?
?? ? ? ? ? ? ? _serItem._name = _tempItem.transform.Find("name").GetComponent();? ? ? ? ? ? ? ? _serItem._name.text = infoGroup[i]._name;? ? ?
?? ? ? ? ? _serItem._LV = _tempItem.transform.Find("Lv").GetComponent();?
?? ? ? ? ? ? ? _serItem._LV.text = infoGroup[i]._lv.ToString();??
? ? ? ? ? ? ? _serItem._job = _tempItem.transform.Find("job").GetComponent();?
?? ? ? ? ? ? ? _serItem._job.text = infoGroup[i]._job;? ? ? ??
? ? ? ? _serItem._icon = _tempItem.transform.Find("Icon").GetComponent();? ? ? ? ??
? ? ? _serItem._icon.sprite = Resources.Load(infoGroup[i]._iconRes, typeof(Sprite))as Sprite;? ? ? ? ? ? ? ? itemGroup.Add(_tempItem);? ? ? ? ?
?? }?
?? ? ? }? ? ? ??
else? ? ? ? {? ? ? ? ?
?? Debug.Log("Way?");
//網(wǎng)絡(luò)層 查看數(shù)據(jù),詢問后端是否發(fā)送了數(shù)據(jù)? ?
?? ? }?
?? }
Rankinginfo類
public class Rankinginfo : MonoBehaviour {
public? uint _id;
public string _name;
public uint _lv;
//頭像
//? int _headIconId;//100 101? config(配置文件)通過ID 讀取本地配置表 獲取頭像的資源名
public string _iconRes;//頭像名
public string _job;
severitem類
public class severitem : MonoBehaviour {
public Text _name;
public Image _icon;
public Text _LV;
public Text _job;
實現(xiàn)搖桿控制物體移動效果如圖(1-6)
為Cube添加組件Character Controller(角色控制器)
Event Trigger組件(添加在移動的中心圓點(diǎn))
為該對象添加事件
BeginDrag()按下的那一幀執(zhí)行
Drag()拖動時執(zhí)行
EndDrag()拖動結(jié)束時執(zhí)行
實現(xiàn)代碼
float radius = 50;//搖桿面板的一半
? public GameObject _player;//搖桿控制的角色
? ? float _speed = 6f; //角色移動的速度
? RectTransform _selfTransform; //搖桿的當(dāng)前位置
? Vector3 _selfStarPos;? //搖桿的起始位置
? public void OnBeginDrag()? ? {? ??
? ? Debug.Log("AAA");? ?
?}? ?
?public void Drag()? ? {??
? ? ? if (Vector3.Distance(Input.mousePosition, _selfStarPos) <= radius)? //如果當(dāng)前位置與原始位置的距離小于半徑,就可以直接拖拽
? ? ? {? ? ? ? ? ?
_selfTransform.position = Input.mousePosition;? ? ? //當(dāng)前位置即是鼠標(biāo)移動的位置
?}? ? ? ??
else?
?? ? ? {? ? ? ? ? ??
Vector3 dir = Input.mousePosition - _selfStarPos; //得到方向向量
? ? ? ? ? _selfTransform.position = dir.normalized * radius + _selfStarPos;? ? //把方向轉(zhuǎn)化為單位向量,然后再乘以半徑,這樣搖桿就不會跑到外面了
?? }? ??
}? ?
?public void EndDrag()? ? {? ?
? ? _selfTransform.position = _selfStarPos;? //搖桿回到原始位置
?}? ??
public void FixedUpdate()? ? {? ?
?? ? if (_selfTransform.position!=_selfStarPos)? ?
?? ? {? ? ? ? ??
? if (Vector3.Distance(Input.mousePosition, _selfStarPos) <= radius)??
? ? ? ? ? {? ? ? ? ? ? ? ?
?float x = (Input.mousePosition - _selfStarPos).x;? ? ? ??
? ? ? ? float y = (Input.mousePosition - _selfStarPos).y;? ??
? ? ? ? ? ? Vector3 dir = new Vector3(x, 0, y);? ? ? ??
? ? ? ? _player.GetComponent().SimpleMove((dir * Time.deltaTime * _speed)); //SimpleMove以一定的速度移動角色。
? ? ? ? ? }? ? ? ??
? ? else? ? ? ??
? ? {? ? ? ? ? ? ??
? float x = (Input.mousePosition - _selfTransform.position).x;? ??
? ? ? ? ? ? float y = (Input.mousePosition - _selfTransform.position).y;? ? ?
?? ? ? ? ? Vector3 dir = new Vector3(x, 0, y);? ? ? ? ?
?? ? ? _player.GetComponent().SimpleMove((dir * Time.deltaTime * _speed));? ? ?
?? ? ? }? ? ?
?? }??
? }? ? // Use this for initialization? ?
?void Start () {? ? ??
? _selfTransform = GetComponent();//得到搖桿的transform組件
_selfStarPos = transform.position;//通過組件得到搖桿開始拖拽的位置
}