Unity3d 杂七杂八小技巧


文章作者:松阳

本文出自 阿修罗道,禁止用于商业用途,转载请注明出处。  

原文链接:http://blog.csdn.net/fansongy/article/details/43987981

width="150" height="210" frameborder="0" scrolling="no" src="http://widget.weibo.com/relationship/bulkfollow.php?language=zh_cn&uids=2080045857&wide=1&color=FFFFFF,FFFFFF,0082CB,666666&showtitle=0&showinfo=1&sense=0&verified=1&count=1&refer=http%3A%2F%2Fwww.himigame.com%2Fandroid-game%2F1521.html&dpc=1" style="font-size: 14px; font-weight: bold; border-width: 0px; margin: 0px; padding: 0px; font-family: arial, helvetica, clean, sans-serif; line-height: 16px;">








用了半年多的Unity3d,零零碎碎的记了点东西,先前记在个人博客里 ,后来网站改版给删了,现在发这吧 都是我实际用过的,至少在某个版本力是杠杠给力的 ,谁用谁知道

1、自定义编辑器

1.1、自定义菜单栏

using UnityEditor

[MenuItem ("aaa/bbb")] //必须有子菜单
static void function_called()
{

}

[MenuItem ("aaa/bbb"),true] //已经选中目标为true
static void called_again()
{

}

1.2、自定义组件菜单

using UnityEngine;
using UnityEditor;

//添加该脚本至组件菜单栏中
[AddComponentMenu("aaa/bbb")]
class menuExt : MonoBehaviour {
    
    void Update()
    {
        //自身旋转
        transform.Rotate(0.0f,Time.deltaTime * 200,0.0f);   
    }
}

2、角色控制器

第一人称 & 第三人称 import Package->Character Controller

控制器组件 component->physics->Character Controller然后使用GetComponent<CharacterController>()

控制器方法:

//平面移动
controller.SimpleMove();
//立体移动
controller.Move();

//碰撞中的状态
controller.collisionFlgas == CollisionFlags.Sides

//碰撞会回调
void OnControllerColliderHit(ControllerColliderHit hit)
{
    //碰撞的游戏对象为 hit.gameObject
}

3、工具类

3.1时间

void OnGUI()
{
    GUILayout.Label("当前游戏时间:" + Time.time);
    GUILayout.Label("上一帧所消耗的时间:" + Time.deltaTime);
    GUILayout.Label("固定增量时间:" + Time.fixedTime);
    GUILayout.Label("上一帧所消耗固定时间:" + Time.fixedDeltaTime);
}

3.2等待

waitForScends()可以直接让游戏主线程等待,不过需要修改返回值为IEnumerator。这岂不是很烂?

3.3随机数

Random.Range(startNum,endNum);

4、射线

Ray ray = new Ray(startPos,endPos)
//从摄像机到鼠标之间的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePositon)

RaycastHit hit;

//与游戏中的对象相交返回true
if(Physics.Raycast(ray,out hit,num))
{
}

5、点击事件

鼠标键位:左键0,右键1,中建2

//按下
Input.GetMouseButtonDown(num)
//取位置
Input.mousePosition
//抬起
Input.GetMouseButtonUp(num)
//长按
Input.GetMouseButton(num)

6、自定义按键事件

按键事件 Input.GetAxis(InputName)

Edit->Project Settings->Input更改数量Input.GetButton(SelfInputName),返回同点击事件。

7、取子节点

foreach (Transform child in obj.transform) 
{
    child.gameObject.SetActive(b);
}

如果你觉得这篇文章对你有帮助,可以顺手点个,不但不会喜当爹,还能让更多人能看到它... 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值