UGUI布局組件與Scroll View控件(下拉)搖桿控制物體移動

三種布局

1. Horizontal Layout Group水平布局


圖1-1

2. Vertical Layout Group垂直布局


圖1-2

3. Grid Layout Group網格布局


圖1-3

Scroll View控件

Scroll Rect組件


圖1-4

實例:

實現點擊button按鈕,獲取到對應的下拉列表,在點擊一次會清除列表,列表內的各項數據都是經過獲取對應的值


圖1-5

mainUI類

public class mainUI : MonoBehaviour {? ??

GameObject _serverItem;? ?

?Transform _parent;? ?

?Button _button;??

? ListitemGroup = new List();? ?

?//數據(實際開發中數據從服務器獲取)

? ? 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 = "狂戰士";?

?? ? ? 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;? ??

? ? }? ??

}? ? ??

? //根據數據創建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?");

//網絡層 查看數據,詢問后端是否發送了數據? ?

?? ? }?

?? }

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;

實現搖桿控制物體移動效果如圖(1-6)


圖1-6

為Cube添加組件Character Controller(角色控制器)


圖1-7

Event Trigger組件(添加在移動的中心圓點)

為該對象添加事件

BeginDrag()按下的那一幀執行

Drag()拖動時執行

EndDrag()拖動結束時執行

圖1-8

實現代碼

float radius = 50;//搖桿面板的一半

? public GameObject _player;//搖桿控制的角色

? ? float _speed = 6f; //角色移動的速度

? RectTransform _selfTransform; //搖桿的當前位置

? Vector3 _selfStarPos;? //搖桿的起始位置

? public void OnBeginDrag()? ? {? ??

? ? Debug.Log("AAA");? ?

?}? ?

?public void Drag()? ? {??

? ? ? if (Vector3.Distance(Input.mousePosition, _selfStarPos) <= radius)? //如果當前位置與原始位置的距離小于半徑,就可以直接拖拽

? ? ? {? ? ? ? ? ?

_selfTransform.position = Input.mousePosition;? ? ? //當前位置即是鼠標移動的位置

?}? ? ? ??

else?

?? ? ? {? ? ? ? ? ??

Vector3 dir = Input.mousePosition - _selfStarPos; //得到方向向量

? ? ? ? ? _selfTransform.position = dir.normalized * radius + _selfStarPos;? ? //把方向轉化為單位向量,然后再乘以半徑,這樣搖桿就不會跑到外面了

?? }? ??

}? ?

?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;//通過組件得到搖桿開始拖拽的位置

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容