本文实例为大家分享了unity使用scrollrect制作摇杆的具体代码,供大家参考,具体内容如下
一. 前言
游戏开发中,摇杆功能是很常见的,unity的ugui提供了scrollrect组件,非常适合用来制作摇杆,效果如下:
二. 实现
1. 制作ui
如下,创建rocker节点和center节点,分别为摇杆的背景图和摇杆的手柄图。
rocker节点挂上rocker脚本(代码见文章最后),并赋值content对象。
设置movementtype为elastic。
2. 运行unity进行测试
运行unity即可测试摇杆功能。
3. rocker脚本代码
using unityengine; using unityengine.ui; public class rocker: scrollrect { protected float mradius = 0f; protected override void start() { base.start(); //计算摇杆块的半径 mradius = (transform as recttransform).sizedelta.x * 0.5f; } public override void ondrag(unityengine.eventsystems.pointereventdata eventdata) { base.ondrag(eventdata); var contentpostion = this.content.anchoredposition; if (contentpostion.magnitude > mradius) { contentpostion = contentpostion.normalized * mradius; setcontentanchoredposition(contentpostion); } debug.log(contentpostion); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论