UGUI 学习
1. Grid Layout Group(网格布局)
Hierachy:
Game:
属性和功能:
2. 根据鼠标位置旋转界面实现:
public class TiltWindow : MonoBehaviour
{
public Vector2 range = new Vector2(5f, 3f); Transform mTrans;
Quaternion mStart;
Vector2 mRot = Vector2.zero; void Start ()
{
mTrans = transform;
mStart = mTrans.localRotation;
} void Update ()
{
Vector3 pos = Input.mousePosition; float halfWidth = Screen.width * 0.5f;
float halfHeight = Screen.height * 0.5f;
float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f); mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
}
}
3. 按钮动画实现:
修改 Transisition 为 Animation,设置触发器
在 Animator 设计状态机
4. HorizontalLayoutGroup(垂直布局)
Hierachy:
Game:
属性和功能:
5. 拖拽功能实现
Hierarchy(其中 Panel 中 Image 绑定 DragMe,Panel2,Panel3 中 Image 绑定 DropMe):
public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public bool dragOnSurfaces = true; private Dictionary<int,GameObject> m_DraggingIcons = new Dictionary<int, GameObject>();
private Dictionary<int, RectTransform> m_DraggingPlanes = new Dictionary<int, RectTransform>(); // 开始拖拽
// 创建拖拽图片的 clone 体
public void OnBeginDrag(PointerEventData eventData)
{
var canvas = FindInParents<Canvas>(gameObject);
if (canvas == null)
return; // We have clicked something that can be dragged.
// What we want to do is create an icon for this.
m_DraggingIcons[eventData.pointerId] = new GameObject("icon"); m_DraggingIcons[eventData.pointerId].transform.SetParent (canvas.transform, false);
m_DraggingIcons[eventData.pointerId].transform.SetAsLastSibling(); var image = m_DraggingIcons[eventData.pointerId].AddComponent<Image>();
// The icon will be under the cursor.
// We want it to be ignored by the event system.
var group = m_DraggingIcons[eventData.pointerId].AddComponent<CanvasGroup>();
group.blocksRaycasts = false; image.sprite = GetComponent<Image>().sprite;
image.SetNativeSize(); if (dragOnSurfaces)
m_DraggingPlanes[eventData.pointerId] = transform as RectTransform;
else
m_DraggingPlanes[eventData.pointerId] = canvas.transform as RectTransform; SetDraggedPosition(eventData);
} // 正在拖拽
// 更新拖拽物位置
public void OnDrag(PointerEventData eventData)
{
if (m_DraggingIcons[eventData.pointerId] != null)
SetDraggedPosition(eventData);
} // 根据鼠标位置更新拖拽物位置和旋转
private void SetDraggedPosition(PointerEventData eventData)
{
if (dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null)
m_DraggingPlanes[eventData.pointerId] = eventData.pointerEnter.transform as RectTransform; var rt = m_DraggingIcons[eventData.pointerId].GetComponent<RectTransform>();
Vector3 globalMousePos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlanes[eventData.pointerId], eventData.position, eventData.pressEventCamera, out globalMousePos))
{
rt.position = globalMousePos;
rt.rotation = m_DraggingPlanes[eventData.pointerId].rotation;
}
} // 结束拖拽
// 销毁克隆体
public void OnEndDrag(PointerEventData eventData)
{
if (m_DraggingIcons[eventData.pointerId] != null)
Destroy(m_DraggingIcons[eventData.pointerId]); m_DraggingIcons[eventData.pointerId] = null;
} // 在父亲节点中寻找 T 组件
static public T FindInParents<T>(GameObject go) where T : Component
{
if (go == null) return null;
var comp = go.GetComponent<T>(); if (comp != null)
return comp; var t = go.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
return comp;
}
}
public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
public Image containerImage;
public Image receivingImage;
private Color normalColor;
public Color highlightColor = Color.yellow; public void OnEnable ()
{
if (containerImage != null)
normalColor = containerImage.color;
} // 放置图片
public void OnDrop(PointerEventData data)
{
containerImage.color = normalColor; if (receivingImage == null)
return; Sprite dropSprite = GetDropSprite (data);
if (dropSprite != null)
receivingImage.overrideSprite = dropSprite;
} // 鼠标移入
// 背景高亮
public void OnPointerEnter(PointerEventData data)
{
if (containerImage == null)
return; Sprite dropSprite = GetDropSprite (data);
if (dropSprite != null)
containerImage.color = highlightColor;
} // 鼠标移出
public void OnPointerExit(PointerEventData data)
{
if (containerImage == null)
return; containerImage.color = normalColor;
} // 得到拖拽图片
private Sprite GetDropSprite(PointerEventData data)
{
var originalObj = data.pointerDrag;
if (originalObj == null)
return null; var dragMe = originalObj.GetComponent<DragMe>();
if (dragMe == null)
return null; var srcImage = originalObj.GetComponent<Image>();
if (srcImage == null)
return null; return srcImage.sprite;
}
}
6. 拖拽改变面板位置(屏幕坐标到本地坐标的转换)
public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler { private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
private RectTransform panelRectTransform;
private RectTransform parentRectTransform; void Awake () {
panelRectTransform = transform.parent as RectTransform;
parentRectTransform = panelRectTransform.parent as RectTransform;
} public void OnPointerDown (PointerEventData data) {
originalPanelLocalPosition = panelRectTransform.localPosition;
// 屏幕坐标转成本地坐标
RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
} public void OnDrag (PointerEventData data) {
if (panelRectTransform == null || parentRectTransform == null)
return; Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition)) {
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
} ClampToWindow ();
} // 将面板限制在父组件之内
// Clamp panel to area of parent
void ClampToWindow () {
Vector3 pos = panelRectTransform.localPosition; Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;
Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max; pos.x = Mathf.Clamp (panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
pos.y = Mathf.Clamp (panelRectTransform.localPosition.y, minPosition.y, maxPosition.y); panelRectTransform.localPosition = pos;
}
}
7. 拖拽改变面板大小
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler { public Vector2 minSize = new Vector2 (, );
public Vector2 maxSize = new Vector2 (, ); private RectTransform panelRectTransform;
private Vector2 originalLocalPointerPosition;
private Vector2 originalSizeDelta; void Awake () {
panelRectTransform = transform.parent.GetComponent<RectTransform> ();
} public void OnPointerDown (PointerEventData data) {
originalSizeDelta = panelRectTransform.sizeDelta;
RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
} public void OnDrag (PointerEventData data) {
if (panelRectTransform == null)
return; Vector2 localPointerPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out localPointerPosition);
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
// 根据鼠标偏移改变窗口大小
Vector2 sizeDelta = originalSizeDelta + new Vector2 (offsetToOriginal.x, -offsetToOriginal.y);
sizeDelta = new Vector2 (
Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),
Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y)
); panelRectTransform.sizeDelta = sizeDelta;
}
}
8. 根据滑动条调节光源颜色
效果图:
先设置 Slider 的 OnValueChange(其他两个同理) :
public class ChangeColor : MonoBehaviour, IPointerClickHandler
{
public void SetRed(float value)
{
OnValueChanged(value, );
} public void SetGreen(float value)
{
OnValueChanged(value, );
} public void SetBlue(float value)
{
OnValueChanged(value, );
} public void OnValueChanged(float value, int channel)
{
Color c = Color.white; if (GetComponent<Renderer>() != null)
c = GetComponent<Renderer>().material.color;
else if (GetComponent<Light>() != null)
c = GetComponent<Light>().color; c[channel] = value; if (GetComponent<Renderer>() != null)
GetComponent<Renderer>().material.color = c;
else if (GetComponent<Light>() != null)
GetComponent<Light>().color = c;
}
}
9. 面板切换
效果图:
面板管理代码:
public class PanelManager : MonoBehaviour { public Animator initiallyOpen; private int m_OpenParameterId;
private Animator m_Open;
private GameObject m_PreviouslySelected; const string k_OpenTransitionName = "Open";
const string k_ClosedStateName = "Closed"; public void OnEnable()
{
// 根据参数名字得到id
m_OpenParameterId = Animator.StringToHash (k_OpenTransitionName); if (initiallyOpen == null)
return;
// 打开初始面板
OpenPanel(initiallyOpen);
} // 打开面板
public void OpenPanel (Animator anim)
{
if (m_Open == anim)
return; anim.gameObject.SetActive(true);
var newPreviouslySelected = EventSystem.current.currentSelectedGameObject; // 最先显示
anim.transform.SetAsLastSibling(); CloseCurrent(); m_PreviouslySelected = newPreviouslySelected; m_Open = anim;
m_Open.SetBool(m_OpenParameterId, true); GameObject go = FindFirstEnabledSelectable(anim.gameObject); SetSelected(go);
} static GameObject FindFirstEnabledSelectable (GameObject gameObject)
{
GameObject go = null;
var selectables = gameObject.GetComponentsInChildren<Selectable> (true);
foreach (var selectable in selectables) {
if (selectable.IsActive () && selectable.IsInteractable ()) {
go = selectable.gameObject;
break;
}
}
return go;
} // 关闭当前面板
public void CloseCurrent()
{
if (m_Open == null)
return; m_Open.SetBool(m_OpenParameterId, false);
SetSelected(m_PreviouslySelected);
StartCoroutine(DisablePanelDeleyed(m_Open));
m_Open = null;
} // 关闭面板
IEnumerator DisablePanelDeleyed(Animator anim)
{
bool closedStateReached = false;
bool wantToClose = true;
while (!closedStateReached && wantToClose)
{
if (!anim.IsInTransition())
closedStateReached = anim.GetCurrentAnimatorStateInfo().IsName(k_ClosedStateName); wantToClose = !anim.GetBool(m_OpenParameterId); // 关闭的时候可打断 yield return new WaitForEndOfFrame(); // 等待关闭动画播放完
} if (wantToClose)
anim.gameObject.SetActive(false);
} private void SetSelected(GameObject go)
{
EventSystem.current.SetSelectedGameObject(go);
}
}
10. RenderTexure 在 UI 上显示模型
设置 Camera TargetTexture,将 相机内容渲染到 RenderTexure 中。
添加 RawImage,设置 Texture 为之前的 RenderTexture。
效果图:
11. 图片遮挡(Mask,ScrollRect)
Hierarchy:
在 GameObject 中添加组件:
效果图:
UGUI 学习的更多相关文章
- Unity3D UGUI学习系列索引(暂未完成)
U3D UGUI学习1 - 层级环境 U3D UGUI学习2 - Canvas U3D UGUI学习3 - RectTransform U3D UGUI学习4 - Text U3D UGUI学习5 - ...
- Unity3D之UGUI学习笔记(三):EventSystem
在UGUI中,EventSystem实现了所有关于交互方面的功能,和NGUI不一样的地方是,我们终于可以摆脱添加Box Collider了! 下面我们来学习一下. 对于按钮来说,直接有onClick的 ...
- Unity3D之UGUI学习笔记(一):UGUI介绍以及Canvas
UGUI是Unity3D4.6官方提供的UI系统,支持2D和3D UI的开发. Unity3D UI史 OnGUI 在Unity4.6之前,官方提供的是OnGUI函数来开发UI界面,当然问题也比较多, ...
- UGUI 学习笔记
1.UGUI中是没有depth的概念,那要怎么在脚本中动态的改变一个UI元素在hierarchy中的排序位置呢? 放到最上面 Transform.SetAsFirstSibling最下面Transfo ...
- U3D UGUI学习1 - 层级环境
就像主动碰撞检测需要刚体,刚体需要Mesh Filter.Unity的一些组件都需要基础配置. UGUI的基础配置和NGUI差不多,把NGUI的那些中间件全部削减干净,甚至连快捷键也没有. 1.基础层 ...
- U3D UGUI学习2 - Canvas
Canvas Render Mode - UGUI不像NGUI,它没有UI摄像机.但有时候需要做相机动画就要调出来. 在Canvas里设置第二个选项即可调出. Pixel Perferct - 这个似 ...
- U3D UGUI学习3 - RectTransform
总的来说整合了NGUI很多零散功能,比如NGUI2.X处理拉伸要额外套脚本,NGUI3.X开始引入新的锚点.再加上依赖BoxCollider使得整个HUD显示非常乱 而UGUI很清晰明了,你也能看清楚 ...
- U3D UGUI学习4 - Text
1.对应NGUI的四种文字显示模式 Shrink Content 对应NGUI第一种模式 勾选Best Fit 但似乎有一个Bug,文字过多的时候会爆框.解决方法是改变Line Spacing ...
- U3D UGUI学习5 - Layout和文字适配
Layout这部分UGUI算是比NGUI做的到位 之前遇到了一个问题,NGUI做文字和背景框适配和容易,绑定一下就好了.UGUI你得弄Layout才可以,而且还需要配置. 但这个Layout使用场合是 ...
- Unity3D之UGUI学习笔记(二):Rect Transform与Anchor
Rect Transform 我们都知道,Unity3D中所有的GameObject都必须要携带一个Transform组件,且该组件无法移除,那么作为UI显示的GameObject则不是携带Trans ...
随机推荐
- IntelliJ-IDEA中mybatis三剑客
一.mybatis-generator的使用 作用:根据数据库自动生成pojo.dao和xml文件. 1.引入mybatis-generator pom.xml中引入配置:
- spring加载jar包中多个配置文件(转载)
本文转载自:http://www.cnblogs.com/GarfieldTom/p/3723915.html
- 使用 ExceptionDispatchInfo 捕捉并重新抛出异常
当你跑起了一个异步线程,并用 await 异步等待时,有没有好奇为什么能够在主线程 catch 到异步线程的异常? 当你希望在代码中提前收集好异常,最后一并把收集到的异常抛出的时候,能不能做到就像在原 ...
- python(十)、进程
一.基本概念 进程和线程是编程中非常重要的概念,它是并发和异步的基础性知识. 1.进程 概念:程序在并发环境中的执行过程.进程作为系统资源分配.调度.管理和独立运行的基本单位,决定了操作系统的四大特性 ...
- 前端(四):JavaScript面向对象之自定义对象
一.对象 1.字面量创建对象 var person = { name: "sun", age: 18, work: function () { console.log(this.n ...
- linux python 图形编程 qt开发环境搭建
我的系统是 ubuntu14.04 我们使用的是python2.7,建议安装qt4+pyqt4+eric4 eric是pyqt的界面设计器的代码生成软件. 1.安装sip 这个是python和qt之间 ...
- SQL中char、varchar、nchar、nvarchar 详解
char char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符. varchar[ ...
- fpga pll重配置实验总结
今天做了pll重配置的实验,输入时钟50m初始配置输出75m经重配置后输出100m,带宽为low,使用的ip:rom,altpll_reconfig ,altpll,将altpll配置为可重配置模式, ...
- mock的使名用一(生成随机数据)
Mock.Random 是一个工具类,用于生成各种随机数据. Mock.Random 的方法在数据模板中称为『占位符』,书写格式为 @占位符(参数 [, 参数]) . var Random = Moc ...
- tomcat深入学习
总体结构:https://www.jianshu.com/p/d74eef07487f servlet相关:https://www.ibm.com/developerworks/cn/java/j-l ...