unity 改变鼠标样式的两种方法
方法 Unity 改变 两种 样式 鼠标
2023-09-14 08:57:45 时间
1、第一个直接改变鼠标样式
1 public var cursorTexture:Texture2D; 2 private var changeFlag = false; 3 function Update(){ 4 if(changeFlag){ 5 Cursor.SetCursor(cursorTexture,Vector2.zero,CursorMode.Auto); 6 }else{ 7 Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto); 8 } 9 } 10 11 function OnGUI(){ 12 if(GUI.Button(Rect(10,10,100,50),"hand")){ 13 changeFlag = true; 14 } 15 if(GUI.Button(Rect(120,10,100,50),"arrow")){ 16 changeFlag = false; 17 } 18 }
2、隐藏鼠标,在鼠标位置放一新图片
1 public var cursorTexture:Texture2D; 2 private var changeFlag = false; 3 4 function OnGUI(){ 5 if(GUI.Button(Rect(10,10,100,50),"hand")){ 6 changeFlag = true; 7 Screen.showCursor = false; 8 } 9 if(GUI.Button(Rect(120,10,100,50),"arrow")){ 10 changeFlag = false; 11 Screen.showCursor = true; 12 } 13 if(changeFlag){ 14 var mousePos = Input.mousePosition; 15 GUI.DrawTexture(Rect(mousePos.x,Screen.height - mousePos.y,cursorTexture.width,cursorTexture.height),cursorTexture); 16 } 17 18 }
需要注意几点的是:用第一种方法中的鼠标图片要修改图片导入的属性,即在inspector中的Texture Type要改为Cursor,否则鼠标会显示不正常。
第二种方法中,在unity编辑器中运行的时候,当改变鼠标样式后,默认的鼠标箭头样式不会消失,但是发布之后就没有什么问题了
相关文章
- Unity编译至Xcode工程后自动添加文件和库的方法
- Could not set limit for ‘nofile’: Operation not permitted的处理方法
- unity web项目发布服务器Data file is corrupt (not a Unity W
- Windows Server 2012启用Windows功能NetFx3时出错解决方法
- 为IEnumerable类型添加Add方法
- 使用Visual Studio将C#生成DLL文件的方法
- 数组2(添加和删除数组元素的方法)
- Oracle导入excel数据快速方法
- (5/8)枚举的错误用法 之 方法体内部
- 股票软件、股票分析、股票推荐、股票监控提醒、股票策略、股票盈利方法
- 联想拯救者Y9000K用U盘重装系统的方法分享
- 修改MySQL密码的四种方法(适合初学者)