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);
}
如果你觉得这篇文章对你有帮助,可以顺手点个顶,不但不会喜当爹,还能让更多人能看到它...