UGUI 相关事件
1、射线检测
1: 判断是否点击 到UI上 :UI 上若无raycast target 也是未触摸到UI上
void Update()
{
if (Input.GetMouseButtonDown(0)||(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
#if IPHONE || ANDROID
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
if (EventSystem.current.IsPointerOverGameObject())
#endif
Debug.Log("当前触摸在UI上");
else
Debug.Log("当前没有触摸在UI上");
}
}
2、判断点击的UI对象
获取当前点击到的UI对象 这里使用遍历的方式 进行判断,
/// <summary>
/// 获得当前点击到的UI物体
/// </summary>
public GameObject Skode_GetCurrentSelect()
{
GameObject obj=null;
GraphicRaycaster[] graphicRaycasters = FindObjectsOfType<GraphicRaycaster>();
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.pressPosition = Input.mousePosition;
eventData.position = Input.mousePosition;
List<RaycastResult> list = new List<RaycastResult>();
foreach (var item in graphicRaycasters)
{
item.Raycast(eventData, list);
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
obj = list[i].gameObject;
}
}
}
return obj;
}
3、RectTransform进行设置
3.1改变RectTransform的top
3.2 改变RectTransfrom的PosX PosY PosZ
3.3 改变RectTransform的width,height
rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, top);
rectTransform.anchoredPosition3D = new Vector3(0, 0, 0); //如果是左右拉伸可能设置y值无效,我们使用上面的方法
rectTransform.sizeDelta = new Vector2(width, height);
4、 关于 ToggleGroupo的使用
4.1 当你动态生成Toggle 的时候,你会发现 默认最后一个toggle 的isOn 为true(即使你在一开始的时候将第一个isOn设置为true ,其他设置为false);
解决方法 :在动态生成所有的toggle物体后,再设置第一个物体的ison为true 就可以了
4.2 动态为toggle添加事件
toggle.onValueChanged.AddListener((isOn) =>
{
if(isOn)
{
currenturl = url;
Debug.Log(toggle+","+url);
//使用MediaPlayer 播放视频的时候 需要调用打开文件进行播放,不然无法博放
if (!mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, url, mediaPlayer.m_AutoStart))
{
Debug.LogError("Failed to open video!");
mediaPlayer.Play();
}else
mediaPlayer.Control.Play();
}
});
5、unity gridlayoutGroup 布局
5.1 unity一个页面只有一个物体,如何从左到右排布,调整了Startaxis 为水平,它仍然变成垂直,可以设置constraint 值
5.2 如何让对话自动到最底部:
我采用的是unity scrollview 组件
scrollRect.verticalNormalizedPosition = 0f;
这个可以直接设置对话到最底部