这部分用来标记基础的UI命令
(一)游戏物体的激活:
如果需要临时中途出现ui空间或者游戏物体,可以先把此物体取消勾选。在脚本中定义一个游戏物体public GameObject go;把相应游戏物体挂在此参数上,需要时用SetActive(true)激活即可。
image添加scroll组件即可拖动,添加scrollbar即可有拖动条;
(二)Scene调用:
SceneManager.LoadScene(sceneName);
(三)控件如 button slider等有图片属性,通过修改可以更改外观;同理image控件也存在button等不同的组件,通过添加可以实心相同的功能。
(四)技能冷却UI
通过image设计技能样式,复制此image同时颜色改为黑丝,透明度改为100左右,通过image的fill Amount属性来实现技能的CD。点击应用技能,快捷键为A,也可以通过改变Fill的方向来实现左右刷新代码如下:
public float coldtime = 2f; public float timer = 0f; private Image coldImage; public KeyCode skillKey=KeyCode.A; private bool isPressed = false; //获取相关游戏物体(100透明度的image)的image组件 void Start () { coldImage = GameObject.Find("/Canvas/Image/Skills/ColdImage").GetComponent<Image>(); } // 可点击实现技能 public void OnSkillClick() { isPressed = true; } //实现相关刷新 void Update () { if(Input.GetKeyDown(skillKey)) { isPressed = true; } if (isPressed) { timer += Time.deltaTime; coldImage.fillAmount =(coldtime- timer) / coldtime; Debug.Log(timer); if (timer >= coldtime) { coldImage.fillAmount = 0; isPressed = false; timer = 0; } } }
小技巧:具体的实现不一定非要在button的方法中,可以通过一个参数在其他位置实现。
(五)通过toggle实现tabcontrol
unity通过toggle实现tabcontrol:首先拖几个toggle,去掉对勾等无用的元素(也可以通过Image添加toggle组件),建立togglegroup。然后建立相应的panel或者gameobject与toggle对应,并设定toggle valuechange时的方法为自带的setactive即可。
UI小技巧:在进行大量UI控件并需要阵列分布是可以使用compent中layouts中的gridlayerout group。
(六)左右滑动窗口
单独实现左右或者上下滑动窗口可以在一个image控件下添加要滑动的控件,同时给image添加scrollRect组件即可 。如果要实现翻页效果,则需要添加脚本实现。所以需要实时获取鼠标按下时的坐标以及松开时的坐标。所以需要继承IBeginDragHandler,IEndDragHandler者两个接口,并实现接口中的两个方法,者两个 方法的入口参数即为鼠标拖动开始和结束时的坐标。如果采用给image添加scrollRect组件的方法则可以通过控制scrollRect组件的位置即可。
在此说明一下,可以通过相应位置与特定坐标点的差值判断进行设置。
public class NewMove : MonoBehaviour,IBeginDragHandler,IEndDragHandler { private ScrollRect scrollRect; public Toggle[] toggleArr; //存储特定的位置坐标 private float[] pageArr=new float[]{0,0.5f,1.0f}; public void OnBeginDrag(PointerEventData eventData) { // Debug.Log("Begin:"); // Debug.Log(eventData.position); //获取rect的初始坐标值 Vector2 pos = scrollRect.normalizedPosition; Debug.Log("Begin:"+pos); } public void OnEndDrag(PointerEventData eventData) { //Debug.Log("End:"); //Debug.Log(eventData.position); //获取rect的拖动后的坐标值 Vector2 pos = scrollRect.normalizedPosition; Debug.Log("End:"+pos); //获取rect拖动后的水平坐标值 float posX = scrollRect.horizontalNormalizedPosition; int index = 0; //计算与特定点的偏差 float offset = Math.Abs(pageArr[index] - posX); //与每一个坐标点进行比较,确定应该存在的位置 //偏差最小的位置即为rect的位置点 for(int i=0;i<pageArr.Length;i++) { float newOffset = Math.Abs(pageArr[i] - posX); if(newOffset<= offset) { index = i; offset = newOffset; } } scrollRect.horizontalNormalizedPosition = pageArr[index]; toggleArr[index].isOn = true; Debug.Log("End:" + scrollRect.horizontalNormalizedPosition); } public void MoveToPage1(bool isOn) { if(isOn) { scrollRect.horizontalNormalizedPosition = pageArr[0]; } } public void MoveToPage2(bool isOn) { if (isOn) { scrollRect.horizontalNormalizedPosition = pageArr[1]; } } public void MoveToPage3(bool isOn) { if (isOn) { scrollRect.horizontalNormalizedPosition = pageArr[2]; } } // Use this for initialization void Start () { scrollRect = this.GetComponent<ScrollRect>(); } // Update is called once per frame void Update () { }
也可以通过if语句判断,以中间页为例,要想把rect的位置定位到中间点即
scrollRect.horizontalNormalizedPosition=0.5位置,则只要鼠标拖动后的rect位置在(0.25-0.75)之间,则把0.5赋值为新位置点。