unity3d培训_005

预设体和操作游戏物体的基本函数

using UnityEngine;
using System.Collections;

public class yusheti : MonoBehaviour {

	void Start () {
	
	}

    public GameObject a;//预设体。

	void Update () {

        if(Input.GetKeyDown(KeyCode.R))
        {
            Vector3 pos = new Vector3();
            //pos.y = 5.6f;//高度为定值 5.6。
            pos.y = Random.Range(0f, 100f);
            pos.x = Random.Range(150, 320);// x 位置在 150 到 320 之间的整数。
            pos.z = Random.Range(150f, 320f);// y 位置在 150 到320 之间的浮点数。

            Instantiate(a, pos,Quaternion.identity);//第一个参数为 预设体, 第二个为 坐标,第三个为旋转度,Quaternion.Identity 为不旋转。
            //Instantiate();//会返回一个 Object 的值。
            //float b=Random.Range(0f,360f);//产生 0 到 360 之间的浮点数 表示随机度数。
            //Quaternion.AngleAxis(b,Vector3.up);//表示 绕 Vector3.up 轴 旋转 b 度。
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            Vector3 pos = new Vector3();
            pos.y = Random.Range(0f, 100f);//高度为 0 到 100 的浮点随机值
            pos.x = Random.Range(150, 320);// x 位置在 150 到 320 之间的整数。
            pos.z = Random.Range(150f, 320f);// y 位置在 150 到320 之间的浮点数。
            float b = Random.Range(0f, 360f);//产生 0 到 360 之间的浮点数 表示随机度数。

            //GameObject p = Instantiate(a, pos, Quaternion.AngleAxis(b, Vector3.up)) as GameObject;// as 类型转化。把Instantiate()返回的 Object 转化为 GameObject。
            //p.AddComponent<yusheti>();//给对象增加 一个组件。
            //p.AddComponent<Light>();//添加灯光组件。

        }
    }
}

鼠标事件

using UnityEngine;
using System.Collections;

public class shubiao : MonoBehaviour {
    void OnMouseDown()//点在游戏对象身上时有效。与Input不同。
    {
        print("鼠标点击");
    }
    void OnMouseUp()//鼠标松开,无论在不在游戏对象身上,但是点的时候要在游戏对象身上。
    {
        print("鼠标松开");
    }
    void OnMouseDrag()//鼠标没松开时候。可以不在游戏对象身上,但是要先点在游戏对象身上。
    {
        print("鼠标拖拽");
    }
    void OnMouseEnter()
    {
        print("鼠标移动到游戏对象身上");
    }
    void OnMouseExit()
    {
        print("鼠标移出对象时候");
    }
    void OnMouseOver()
    {
        print("鼠标持续停留在游戏对象身上");
    }
    void OnMouseUpAsButton()//在游戏对象身上松开,就像点击按钮。
    {
        print("AsButton");
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值