美文网首页
unity学习总结知识大全

unity学习总结知识大全

作者: 幕飞 | 来源:发表于2017-07-24 21:22 被阅读0次

    ---date: 2017-07-17 13:01status: publictitle:

    ---游戏物体添加tag值,获取taggameObject.tag//设置游戏对象的tag值gameObject.tag="Player"

    调用transform组件的三种方式(1) gameObject.GetCompenent()(2) gameObject.transform(3) transformGameObject:

    游戏对象类两种获取游戏对象的方式(1) 通过名字获取:GameObject.Find(“”)(2) tag值来获取: GameObject.FindGameObjectWithTag(“”)//设置游戏物体的位置t1.position = new Vector3(2,3,4);

    获取一个组件:游戏物体.GetComponent<组件名>()

    脚本生命周期【1】 Awake: 加载的时候调用【2】 OnEnable: 激活的时候调用【3】 Start: 脚本开始运行的时候调用【4】 Update: 每帧都调用【5】 LateUpdate: Update执行结束之后调用【6】 OnDisable: 脚本不激活的时候调用【7】 OnDestory: 脚本销毁的时候调用

    Time.delteTime: 上一次LateUpdate结束到这次Update开始的时间间隔监听键盘的输入

    1 Input.GetKey(KeyCode):某个按键持续按下的时候出发

    2 Input.GetKeyDown(KeyCode):某个按键按下的时候触发

    3 Input.GetkeyUp(KeyCode):

    某个按键弹起的时候触发监听鼠标的输入1 Input.GetMouseButtonDown():

    鼠标按下2 Input.GetMouseButtonUp():

    鼠标弹起3 Inpup.GetMouseButton():

    鼠标持续点击字母数字小数字键盘鼠标KeyCode.WKeyCode.Alpha10:KeyCode.Keypad00:

    左键KeyCode.AKeyCode.Alpha21:KeyCode.Keypad11:

    右键KeyCode.SKeyCode.Alpha32:KeyCode.Keypad22:

    中键……………………小数字键盘为: KeyCode.Keypad1上下左右键为KeyCode.UpArrow/DownArrow/LeftArrow/RightArrow

    获取水平方向跟竖直方向的虚拟轴偏移量float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal");

    刚体组件RigidBody:刚体组件,用来模仿现实世界的物理效果(参数:质量,空气阻力,角阻力,约束(位置跟旋转))给刚体组件设置一个速度:velocity施加爆炸力作用:Rigidbody对象.AddExplosionForce(force,position,radius)

    预设体生成预设体:将场景的游戏物体拖拽到Asset当中即生成预设体根据预设体生成游戏对象:GameObject.Instantiate(prefab,position,rotattion)  Quaternion.identity:旋转角度都为

    碰撞事件发生条件:两个物体必须要有碰撞器至少其中要有一个刚体组件两个物体要有相对位移//碰撞事件刚发生的时候void OnCollisionEnter(Collision other){    print("OnCollisionEnter");}//碰撞事件持续的时候void OnCollisionStay(Collision other){    print("OnCollisionStay");}//碰撞事件结束的时候void OnCollisionExit(Collision other){    print("OnCollisionExit");}

    触发器触发事件:触发事件也是在触发的过程当中回调某些函数发生条件:两个物体必须有碰撞器,至少其中一个为触发器(勾选上IsTrigger)要有刚体组件相对位移

    //触发事件刚发生的时候void OnTriggerEnter(Collider other){    print("OnTriggerEnter");}

    //触发事件持续的时候void OnTriggerStay(Collider other){        print("OnTriggerStay"); }

    //触发事件结束的时候void OnTriggerExit(Collider other){        print("OnTriggerExit");}

    实现物体位移、旋转的两种方式:

    //获取水平方向跟竖直方向的虚拟轴偏移量float vertical = Input.GetAxis("Vertical");float horizontal = Input.GetAxis("Horizontal")

    ;//位移1transform.Translate(Vector3.forward*speed*vertical*Time.deltaTime);

    //位移2transform.position +=Vector3(x,y,z);

    //旋转transform.Rotate(Vector3.up*rotateSpeed*horizontal*Time.deltaTime);

    限制游戏物体的坐标范围例:x轴[-4.5,4.5] Y[-4.5,4.3]Vector3 tempPos = transform.position;tempPos.x = Mathf.Clamp(tempPos.x,-4.5f,4.5f);tempPos.y = Mathf.Clamp(tempPos.y,-4.5f,3.1f);transform.position = tempPos;

    销毁对象Destroy(this.gameObject);

    音频组件:AudioSourceAudioSource shootAS = gameObject.GetComponent();

    //播放

    shootAS.Play();

    UI界面

    //屏幕宽度:Screen.width

    //屏幕高度:Screen.height

    //在屏幕显示的区域:

    Rect scoreRect = new Rect(x,y,width,height);

    //文本

    GUI.Label(Rect,String);

    GUI.Label(Rect,String,GUIStyle);

    //文本样式

    GUIStyle textStyle = new GUIStyle();

    textStyle.normal.textColor = Color.white;

    textStyle.fontSize = 25;

    textStyle.alignment = TextAnchor.MiddleCenter;

    获取子物体的父物体对象

    子物体.tansform.parent.gameObject

    获得父物体的子物体对象

    父物体.transform.FindChild("子物体name");

    Unity UI控件

    //给按钮添加一个监听事件

    ReStartBtn.onClick.AddListener(ReStartBtnClickListener);

    //按钮点击响应的方法

    public void ReStartBtnClickListener(){

    //print("重新开始游戏");

    //重新加载整个场景

    SceneManager.LoadScene(0);

    }

    相关文章

      网友评论

          本文标题:unity学习总结知识大全

          本文链接:https://www.haomeiwen.com/subject/fbkikxtx.html