查看: 3447|回复: 6
打印 上一主题 下一主题

脚本分享——一些小功能和我的理解

[复制链接]

12

主题

2

听众

231

积分

设计实习生

Rank: 2

纳金币
208
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2012-12-28 13:18:30 |只看该作者 |倒序浏览
这几天在做一些功能的实现,多半是一边查一遍自己实现,所以比较零散,在这里跟大家分析依稀,也算是自己总结了。ps(纳金网怎么就没有草稿箱的功能啊,那次我写了一般本想过下再写就暂时发布了,谁知道没有通过就给直接删除了~OTZ)没事,我有动力,所以,再写一篇。以模块的形式吧;1.  输入控制: Unity里面的输入控制主要分为 键盘 鼠标 Touch  或者你还可以扩展(比如传感器等)这里说一下前三种:所有的输入都在Input类里面,我们看一下帮助文档怎么说的。(其实学会用帮助文档,进步就非常快了,所以我也想能够教会大家用帮助文档)Class VariablesgyroReturns default gyroscope.mousePositionThe current mouse position in pixel coordinates. (Read Only)anyKeyIs any key or mouse button currently held down? (Read Only)anyKeyDownReturns ***e the first frame the user hits any key or mouse button. (Read Only)inputStringReturns the keyboard input entered this frame. (Read Only)accelerationLast measured linear acceleration of a device in three-dimensional space. (Read Only)accelerationEventsReturns list of acceleration measurements which occurred during the last frame. (Read Only) (Allocates temporary variables)accelerationEventCountNumber of acceleration measurements which occurred during last frame.touchesReturns list of objects representing status of all touches during last frame. (Read Only) (Allocates temporary variables)touchCountNumber of touches. Guaranteed not to change throughout the frame. (Read Only)multiTouchEnabledProperty indicating whether the system handles multiple touches.locationProperty for accessing device location (handheld devices only). (Read Only)compassProperty for accessing compass (handheld devices only). (Read Only)deviceOrientationDevice physical orientation as reported by OS. (Read Only)imeCompositionModeControls enabling and disabling of IME input composition.compositionStringThe current IME composition string being typed by the user.compositionCursorPosThe current text input position used by IMEs to open windows.imeIsSelectedDoes the user have an IME keyboard input source selected?Class FunctionsGetAxisReturns the value of the virtual axis identified by axisName.GetAxisRawReturns the value of the virtual axis identified by axisName with no smoothing filtering applied.GetButtonReturns ***e while the virtual button identified by buttonName is held down.GetButtonDownReturns ***e during the frame the user pressed down the virtual button identified by buttonName.GetButtonUpReturns ***e the first frame the user releases the virtual button identified by buttonName.GetKeyReturns ***e while the user holds down the key identified by name. Think auto fire.GetKeyDownReturns ***e during the frame the user starts pressing down the key identified by name.GetKeyUpReturns ***e during the frame the user releases the key identified by name.GetJoystickNamesReturns an array of strings describing the connected joysticks.GetMouseButtonReturns whether the given mouse button is held down.GetMouseButtonDownReturns ***e during the frame the user pressed the given mouse button.GetMouseButtonUpReturns ***e during the frame the user releases the given mouse button.ResetInputAxesResets all input. After ResetInputAxes all axes return to 0 and all buttons return to 0 for one frame.GetAccelerationEventReturns specific acceleration measurement which occurred during last frame. (Does not allocate temporary variables)GetTouchReturns object representing status of a specific touch. (Does not allocate temporary variables)其实在帮助文档的脚本参考里面都是这样一个结构 分为类变量和类的方法,然后就是继承而来的其他的方法和变量了。简单的实现案例:键盘:=====================================================================         if(Input .GetKey(KeyCode.A))        {            transform.Rotate( new Vector3 (0, rotateSpeed*Time.deltaTime, 0));        }        if (Input .GetKey(KeyCode.D))        {            transform.Rotate( new Vector3 (0, -rotateSpeed * Time.deltaTime, 0));        }
=======================================================================解释:GetKey() 方法表示的是获取按键 而KeyCode 是一个枚举类型,定义了所有的按键的数值可查阅帮助文档。当然还有其他的函数应用,官方都给了 案例了,比如:    var translation : float = Input.GetAxis ("Vertical") * speed;
    var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
其中,GetAxis 表示获取水平轴  Vertical   表示水平  Horizontal  表示竖直还有一个值得一提就是   GetTouch  这个是在移动设备上来获取点击的一个方法。具体实现就不贴了,帮助文档上有。(别喷,学会帮助文档真的很有用~)鼠标:鼠标一般有三个按键(左键 右键 滚轮)那些GetMouse××的都是关于Mouse的操作,基本就是字面意思,看看就会了,这里方向一下滚轮的控制脚本;我的是放在了相机上的实现视野的缩放。===================================================================================================         if (Input .GetAxis("Mouse ScrollWheel")>0)        {            //theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ScrollKeySpeed;            transform.Translate( new Vector3 (0,0,-ScrollKeySpeed*Time.deltaTime));        }        if (Input .GetAxis("Mouse ScrollWheel") < 0)        {            //theDistance = theDistance - Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ScrollKeySpeed;            transform.Translate( new Vector3 (0,0,ScrollKeySpeed*Time.deltaTime));        }================================================================================滚轮的前滚和后滚是通过返回值设置的。点击是0 ;双击:双击判断没有直接的响应事件所以实现方式很多,我查到了一个,貌似很不错=========================================================================================================     Event mouse = Event .current;         if (mouse.isMouse&&(mouse.type==EventType.mouseDown)&&mouse.clickCount==2)        {            print( "Double Click~");        }==================================================================================推荐了。2. 组件的访问unity3d对于组件的访问是重点介绍的,我这边也稍提一下和分享自己的实现。其对于组件访问意思就是 在运行是,游戏物体身上的组件属性被其他的游戏物体获取。我主要说一下脚本的获取。 比如我有一个脚本有一个变量是 bool类型的,用来判断是否执行一段代码,而这个bool类型的变量由其他的游戏物体控制,这个时候,我就准备用访问它的脚本控制了。就是下面这段代码:     currGameObject.GetComponent< OptimizeWall>().wallActive = ***e ;currGameObject又是谁呢? 我们可以通过多种方式将他定位,比如游戏中谁碰到了他的剑 谁就是currGameObject。当然,有GetComponent()就有 AddGetComponent() 方法,道理类似,是用来添加组件的,包括脚本,和Uniy内置的组件,比如灯光,相机之类的。3. 射线都是简单的用了一下,还没来得及深入研究暂且分享浅层次的吧。 射线就是用来在场景中做游戏物体之间的交互用的,就象蝙蝠的超声波一样的定位功能。我这里简单的实现了一个:=============================================================================================================================         if (Input .GetMouseButtonDown(0))        {            Ray ray = Camera .main.ScreenPointToRay(Input.mousePosition);            RaycastHit hit;            if (Physics .Raycast(ray, out hit))            {                currGameObject = hit.collider.gameObject;                Debug.Log(currGameObject.name);                if (currGameObject.name=="wall(Clone)" )                {                    Debug.Log("当前的墙已激活~" );                    currGameObject.GetComponent< OptimizeWall>().wallActive = ***e ;                      }            }        }=============================================================================================================================4. 预置体的加载PreFeb的初步使用:1.创建2.在Scene中设置好像添加到PreFeb中的GameObject之后拖到之前创建的PreFeb上。3.RunTime Load     我使用的方法:                   GameObject  newWall =(GameObject ) Instantiate(Resources.Load( "wall"));            newWall.transform.parent = Wall.transform;首先将wall加载进来 Resources.Load( "wall" )   之后实例化: Instantiate( Resources.Load( "wall" ));我的第二句是实现了 将加载进来的wall的父节点设置为Wall。今天这些总结了一些,希望对初学者有帮助! 加油吧,大家。




分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-12-31 03:52:36 |只看该作者
谢谢楼主的帖子分享,学习了
回复

使用道具 举报

733

主题

5

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
6520
精华
14

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-12-31 03:53:24 |只看该作者
顶,楼主的分享很不错,学习了
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
回复

使用道具 举报

955

主题

164

听众

7万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
59338
精华
28

活跃会员 荣誉管理 突出贡献 优秀版主 论坛元老

地板
发表于 2012-12-31 03:57:21 |只看该作者
感谢提供精彩的教程
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-12-31 16:51:05 |只看该作者
里面有些错字啊,,,还有的字突然变小。。不知是否用firebox的原因
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
回复

使用道具 举报

733

主题

5

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
6520
精华
14

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-12-31 17:41:25 |只看该作者
楼主是否能在代码后面加上中文的注释啊,这样看起来会比较了解用法。先谢谢了
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
回复

使用道具 举报

955

主题

164

听众

7万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
59338
精华
28

活跃会员 荣誉管理 突出贡献 优秀版主 论坛元老

7#
发表于 2013-1-2 15:41:30 |只看该作者
草稿箱的事情会反应给相关人员,这个问题提的很好,谢谢!
var __chd__ = {'aid':11079,'chaid':'www_objectify_ca'};(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = ***e;c.src = ( 'https:' == document.location.protocol ? 'https://z': 'http://p') + '.chango.com/static/c.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(c, s);})();
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-27 09:01 , Processed in 0.097739 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部