[unity3d]水果忍者-界面搭建

简介: 今天开始用Unity3D做一下水果忍者的游戏,Keep study very day!效果图:                                                                                    ...

今天开始用Unity3D做一下水果忍者的游戏,Keep study very day!

效果图:                                                                                                                 





实现步骤:                                                                                                              

1.贴图的创建


这里的Pixel Inset中X,Y,Width,Height是贴图的位置以及宽高属性,是只读的,可恨的只读,不然我就可以在后台代码更改贴图的宽高来自适应分辨率了,就是因为这不可修改,让我苦恼的没办法做分辨率的自适应,如果我是在OnGUI方法来时时绘制GUITexture,但我又觉得太耗资源,我写不了那样的代码。我具有强迫症,那种性能不好的代码不忍下手!!!
关于锚点问题:这里的X,Y是以图片的左下角为基准点,由于我之前是搞cocos2dx,对精灵的锚点等属性熟悉,但后来搞懂Unity里面图片的“锚点”默认是左下角的(0,0)点,并且貌似不可修改,cocos2dx默认是中心点(0.5,0.5),所以这里的X,Y就都设置为0,图片的左下角就跟屏幕的左下角对其,然后宽高设置为屏幕的分辨率,我的手机分辨率是800*480,所以我就设置了这么大。

2.声音的添加

创建一个空物体,点击菜单栏GameObject->Create Empty,命名为audio,然后给他添加一个Audio Source组件,Component->Add->Audio Source,然后设置AudioClip为背景声音,并且设置Loop循环播放和Play On Awake程序启动时自动播放。

   
关于脚本控制声音的开关,下面介绍。

3.手动“绘制”按钮

其他的菜单按钮,我们在GUI里面“绘制”,GUI方法是每帧都调用的方法,用于在屏幕上绘制东西,之所以说GUI效率没有NGUI好就是因为它每帧都不停的绘制,NGUI可能对这方面做了优化。所以现在界面的制作普遍都是倾向于用NGUI,并且后者对自适应做的非常好。
NGUI的自适应非常简单:

这里我还延续了之前cocos2dx的思想,先获取屏幕的宽高,然后计算按钮绘制的位置,这样就能适应不同的分辨率。
这里我就直接贴代码了,代码比较简单,通俗易懂!
using UnityEngine;
using System.Collections;

public class start : MonoBehaviour {


    public GUISkin mySkin;

    private bool isSound1Button = false;
	private bool isSound2Button = true;
    private AudioSource sound;
	
	
	private float screenwitdh;
	private float screenheight;
	
	public GUITexture background;
	public GUITexture title;
	
	void Awake()
	{
//		screenwitdh = Screen.width;
//		screenheight = Screen.height;
		
		//想改变背景的分辨率
//		background.guiTexture.pixelInset.x = 0;
//		background.guiTexture.pixelInset.y = 0;
//		background.guiTexture.pixelInset.width = screenwitdh;
//		background.guiTexture.pixelInset.height = screenheight;
	}

	// Use this for initialization
	void Start () 
	{
		sound = gameObject.GetComponent<AudioSource>();
	}
	
    void OnGUI()
    {
		
		screenwitdh = Screen.width;
		screenheight = Screen.height;
		
        GUI.skin = mySkin;

		//这里的GUI坐标是以左上角为原点跟GUITexture的坐标不同,GUITexture属性中是OpenGL坐标是从左下方开始为原点
        if (GUI.Button(new Rect(screenwitdh/2-110, screenheight -280, 220, 66), "", GUI.skin.GetStyle("playbutton")))
        {
            Application.LoadLevel(1);
        }
		if(GUI.Button (new Rect(screenwitdh/2-160,screenheight -200,320,66),"",GUI.skin.GetStyle("MoreButton")))
		{
			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013");	
		}
		if(GUI.Button(new Rect(screenwitdh/2-100,screenheight -120,200,66),"",GUI.skin.GetStyle("CreditButton")))
		{
			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013");
		}
		
		if(isSound1Button)
		{
			if(GUI.Button(new Rect(45,screenheight-45,20,31),"",GUI.skin.GetStyle("Sound1Button")))
			{
				sound.Play();
				isSound1Button = false;
				isSound2Button = true;
			}
		}
		
		if(isSound2Button)
		{
			if(GUI.Button(new Rect(45,screenheight-45,37,31),"",GUI.skin.GetStyle("Sound2Button")))
			{
				sound.Stop();
				isSound1Button = true;
				isSound2Button = false;
			}
		}
    }
}

不早了,早点去睡觉,后续的后面继续补上!

==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:375151422       cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

 

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/18376397

欢迎关注我的微博:http://weibo.com/u/2590571922

需要工程文件的请留言!


今天将昨天的版本做了一些自适应的修改:

GUITexture的屏幕自适应:

screenwitdh = Screen.currentResolution.width;
screenheight = Screen.currentResolution.height;
//改变背景的分辨率
background.transform.position = Vector3.zero;
background.transform.localScale = Vector3.zero;
background.guiTexture.pixelInset = new Rect(0, 0, screenwitdh, screenheight);
print("height" + screenheight);
print("width" + screenwitdh);

经过反复的编译apk,安装到手机,经过十几次的反复终于尝试出稍微完美的自适应,下面是完美的代码:

using UnityEngine;
using System.Collections;

public class start : MonoBehaviour {


    public GUISkin mySkin;

    private bool isSound1Button = false;
	private bool isSound2Button = true;
    private AudioSource sound;
	
	
	private float screenwitdh;
	private float screenheight;
	
	public GUITexture background;
	public GUITexture title;
	
	void Awake()
	{
        screenwitdh = Screen.currentResolution.width;
        screenheight = Screen.currentResolution.height;
        //改变背景的分辨率
        background.transform.position = Vector3.zero;
        background.transform.localScale = Vector3.zero;
        background.guiTexture.pixelInset = new Rect(0, 0, screenwitdh, screenheight);


        //title.transform.position = Vector3.zero;
        //title.transform.localScale = Vector3.zero;
        //坐标是以左下角为原点开始的,锚点也是左下角
        float width = 400;
        float height = 200;
        title.guiTexture.pixelInset = new Rect(screenwitdh/2-width/2, screenheight-30-height, width, height);

        print("height" + screenheight);
        print("width:" + screenwitdh);
	}

	// Use this for initialization
	void Start () 
	{
		sound = gameObject.GetComponent<AudioSource>();
	}
	
    void OnGUI()
    {
        //screenwitdh = Screen.width;
        //screenheight = Screen.height;
        GUI.Label((new Rect(10, 10, 100, 20)), "作者:丁小未");
        GUI.Label((new Rect(10, 30, 100, 20)), "height:"+screenheight.ToString());
        GUI.Label((new Rect(10, 50, 100, 20)), "widht:"+screenwitdh.ToString());

		
        GUI.skin = mySkin;

		//这里的GUI坐标是以左上角为原点跟GUITexture的坐标不同,GUITexture属性中是OpenGL坐标是从左下方开始为原点
        if (GUI.Button(new Rect(screenwitdh/2-110, screenheight -250, 220, 66), "", GUI.skin.GetStyle("playbutton")))
        {
            Application.LoadLevel(1);
        }
		if(GUI.Button (new Rect(screenwitdh/2-160,screenheight -185,320,66),"",GUI.skin.GetStyle("MoreButton")))
		{
			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013");	
		}
		if(GUI.Button(new Rect(screenwitdh/2-100,screenheight -120,200,66),"",GUI.skin.GetStyle("CreditButton")))
		{
			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013");
		}
		
		if(isSound1Button)
		{
			if(GUI.Button(new Rect(60,screenheight-60,30,41),"",GUI.skin.GetStyle("Sound1Button")))
			{
				sound.Play();
				isSound1Button = false;
				isSound2Button = true;
			}
		}
		
		if(isSound2Button)
		{
			if(GUI.Button(new Rect(60,screenheight-60,45,41),"",GUI.skin.GetStyle("Sound2Button")))
			{
				sound.Stop();
				isSound1Button = true;
				isSound2Button = false;
			}
		}
    }
}


效果图


相关文章
|
7月前
|
C#
C#之四十六 迷你贪吃蛇项目
C#之四十六 迷你贪吃蛇项目
53 0
|
图形学
Unity 3D游戏-消消乐(三消类)教程和源码
Unity 消消乐教程和源码 本文提供全流程,中文翻译。Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) ...
5343 0
|
5月前
|
图形学
【Unity3D开发小游戏】Unity3D零基础一步一步教你制作跑酷类游戏
【Unity3D开发小游戏】Unity3D零基础一步一步教你制作跑酷类游戏
|
5月前
|
图形学
Unity小游戏——迷你拼图
Unity小游戏——迷你拼图
115 1
|
9月前
|
图形学
【Unity实战系列】如何把你的二次元老婆/老公导入Unity进行二创并且进行二次元渲染?(附模型网站分享)
【Unity实战系列】如何把你的二次元老婆/老公导入Unity进行二创并且进行二次元渲染?(附模型网站分享)
372 0
|
11月前
|
API 图形学
【2023unity游戏制作-mango的冒险】-1.场景搭建
【2023unity游戏制作-mango的冒险】-1.场景搭建
76 0
|
数据可视化 iOS开发 MacOS
FLStudio2024中文完整版水果音乐制作编曲软件
最近,网上算是“风言风语”吧,关于FL Studio是否出21版的说法各异。首先呢,这里先肯定一点,FL Studio即将出FL Studio 21版本,但是正式版已经出来。FL Studio21绿色版本下载如下:http://t.csdn.cn/8vUrw
152 0
Qt-网易云音乐界面实现-6 迷你个人中心实现
这个界面除了麻烦耗时,没有啥技术含量。暂时我也就把它称为迷你个人中心,因为后面还有一个个人中心了。 先看下完成品
109 0
Qt-网易云音乐界面实现-6 迷你个人中心实现
|
API 图形学 Android开发
创作 【Unity使用UGUI实现王者荣耀UI界面(四)】游戏开始界面
创作 【Unity使用UGUI实现王者荣耀UI界面(四)】游戏开始界面
395 0
创作 【Unity使用UGUI实现王者荣耀UI界面(四)】游戏开始界面
|
图形学
Unity制作即时战略游戏毕设
创建项目 双击Unity,选择New Project:我们将它命名为rts,悬着其他本地磁盘例如C:,选择3D然后点击Create Project: 然后我们通过File->Save Scene,保存当前的场景为“scene”(不需要“”) 摄像机 Unity会自动添加相机到工程中。
1752 0